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

Try this

  Button button;

public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

button=(Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(getApplicationContext(),SendPhotos.class);
                        startActivity(i);
                    }
                });

 }

From Activity : where you are currently ?

To Activity : where you want to go ?

Intent i = new Intent( MainActivity.this, SendPhotos.class); startActivity(i);

Both Activity must be included in manifest file otherwise it will not found the class file and throw Force close.

Edit your Mainactivity.java

crate button's object;

now Write code for click event.

        Button btn = (button)findViewById(R.id.button1);
         btn.LoginButton.setOnClickListener(new View.OnClickListener() 
       {

                @Override
                public void onClick(View v) 
                {
                 //put your intent code here
                }
   });

Hope it will work for you.


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>