Here we learn , How to Create a button Activity in Android studio Project. Follow this steps
Here we use intent function to move another activity
Step 1:- open xml file and write code
activity.xml code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/bu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="click here"/>
</RelativeLayout>
Step 2:- create another Activity(which activity you want to go when button clicked)
path :- go res folder --> laylout folder then right click---> New--->Activity ---> empty Activity
Step 3:- go main activity .java File
package com.ehowsongs.newapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button bu2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bu2 = findViewById(R.id.bu2);
bu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openactivity2();
}
});
}
private void openactivity2() {
Intent intent = new Intent(this,Activity2.class);
startActivity(intent);
}
}
This the code you need to write to button Activity:-
bu2 = findViewById(R.id.bu2);
bu2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openactivity2();
}
});
}
private void openactivity2() {
Intent intent = new Intent(this,Activity2.class);
startActivity(intent);
}
OUTPUT:-