Click the button and open another Activity

8

I'm developing an application in JAVA, and will have 2 options for the person to choose, I want it when I press 1 of the 2 buttons open a new Activity in the application, Can someone please tell me the code for this? I'm using Android Studio.

    
asked by anonymous 15.03.2017 / 22:29

1 answer

11

For this you can use the startActivity ( Documentation )

This method requests a Intent

Here you should inform yourself of a Context (in this case your Activity) and class of the Activity you want to open:

Intent intent = new Intent(this, SegundaActivity.class);
startActivity(intent);

ADD THE CLICK ON A BUTTON THROUGH XML:

In Your% w_that% in the first element add the following line:

xmlns:tools="http://schemas.android.com/tools"

With this you can add the context class of this xml:

 tools:context="seu.pacote.app.SuaActivity"

Next, let's say on the button which method it will call:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button"
<!-- ira chamar o metodo proximaTela -->
    android:onClick="proximaTela"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="-2dp" />

In Java add the method a public method xml and receive a View as parameter:

public class SuaTela extends AppCompatActivity {

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

    public void proximaTela(View view){

        Intent intent = new Intent(this, SegundaActivity.class);
        startActivity(intent);
    }
}
    
15.03.2017 / 22:53