Android Studio

How to display a message using a button

1. create a button activity-main.xml file

<Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click button"
        android:onClick="gotonewActivity"
        android:layout_marginTop="50dp"/>

2. now create a method in MainActivity.java file named “gotonewActivity” and write below code

public void gotonewActivity(View view) {
        Toast.makeText(MainActivity.this, "I like you",
                Toast.LENGTH_LONG).show();
    }

How to display a new page using a button

1. create a button activity-main.xml file

<Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click button"
        android:onClick="gotonewActivity"
        android:layout_marginTop="50dp"/>

2. now create a method in the MainActivity.java file named “gotonewActivity”

3. Create a new activity named “SecondActivity.java” and write the below code

public void gotonewActivity(View view) {
    
        Intent ob1 = new Intent(MainActivity.this,SecondActivity.class);
        startActivity(ob1);
    }