How do I put event on buttons to call another screen on Android? I'm using Intent , but I'm not getting it.
How do I put event on buttons to call another screen on Android? I'm using Intent , but I'm not getting it.
Suppose you have two Activities
, AtividadeA
and AtividadeB
, and that AtividadeA
will have a button that will call AtividadeB
.
It works like this:
1) In the layout of your first activity (which possibly has the name of activity_main.xml
) you include a button ( Button
) with a id
...
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/meuBotao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clique em mim"/>
</LinearLayout>
2) In your ActivityA you declare a button ...
private Button botao;
3) In the onCreate(Bundle savedInstanceState)
method you initialize the button and associate it with View.OnClickListener
...
setContentView(R.layout.activity_main);
botao = (Button)findViewById(R.id.meuBotao);
botao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(AtividadeA.this, AtividadeB.class);
startActivity(intent);
}
});
4) Do not forget to declare the two activities in AndroidManifest.xml:
<application>
...
<activity android:name="AtividadeA">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="AtividadeB" />
...
</application>
Got it? Any questions just comment. If you are satisfied with the answer, you can accept it by clicking on the icon to the left of it.