Events on buttons

Asked

Viewed 218 times

0

How do I place event in buttons to call another screen on Android? I’m using the Intent, but I’m not getting it.

  • 2

    Edit the question and show the code you tried, please.

  • 1

    Be a little clearer, your question is on the part of creating an event or specifically on how to call another screen?

1 answer

6


Suppose you have two Activities, to AtividadeA and the AtividadeB, and that the AtividadeA will have a button that will call the AtividadeB.

It works that way:

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 Activity you declare a button...

private Button botao;

3) In the method onCreate(Bundle savedInstanceState) you initialize the button and associate with it a 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) Don’t forget to declare both activities on 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.