How to open new activity on button click in android studio



First drag a button in MainActivity.xml or in some other xml file , or use below code

<Button    android:id="@+id/button"
    android:layout_width="match_parent"    
    android:layout_height="wrap_content
    android:text="Open Next Activity"    
    android:textColor="#ffffff"    
    android:textSize="22sp" />




Then Place below code in .java File

button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                startActivity(new Intent(MainActivity.this, nextactivity.class));
            }


        });

Then Go to File > New > Activity > Empty Activity and name this activity as nextactivity or some other name of your choice

Now When Your Press button then nextactivity will open in App

Post a Comment

5 Comments