Moving from one activity to another in ANDROID!

Moving from one activity to Another on button Click! Its way to easy to do....




on activity_main.xml lets create a button:

<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/clickBtn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="click"/>

</RelativeLayout>


After creating a button on MainActivity.class

*to start the activity we have to call an intent.

public class MainActivity extends AppCompatActivity {

    Button clickBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        clickBtn = findViewById(R.id.clickBtn);

        clickBtn.setOnClickListener(v->{

            startActivity(new Intent(getApplicationContext(),secondActivity.class));

        });
    }
}

secondActivity.class is an empty activity which we call on the button click.



And thats how we start an activity from a button click :)







Comments