How to open a second activity on click of button in android app

You can move to desired activity on button click. just add this line.

android:onClick="sendMessage"

xml:

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendMessage"
        android:text="@string/button" />

In your main activity just add this method:

public void sendMessage(View view) {
    Intent intent = new Intent(FromActivity.this, ToActivity.class);
    startActivity(intent);
}

And the most important thing: don’t forget to define your activity in manifest.xml

 <activity>
      android:name=".ToActivity"
      android:label="@string/app_name">          
 </activity>

Leave a Comment