Clararhea Clararhea - 5 months ago 14
Android Question

Button on click listener

I come from the iOS world and just started with Android. I followed a java training so I know how to create a method, but I don't get the action one.

What I would like to do is to create a method action from a button and I can't find anywhere how to do that (obviously looking in the wrong place). When I click on button 1, I want to create a method with inside only a=1 and when I click on button 2, I want a=2 (then I can decide what to do when a is 1 or 2).

I created my buttons in Acitivity_main.xml and gave a method name in OnClick, but that's as far as I went.

It seems to be so basic to do when I compare with iOS that I don't understand why I can't find how to do it.

Would a nice soul point me in the right direction for creating an action please?

Answer

You have three options:

common in 1,2) You need to assign an id to each of your buttons in the layout XML file

<Button android:id="@+id/my_button1"
..........
/>
<Button android:id="@+id/my_button2"
..........
/>

1) In the activity's onCreate() method after setContentView() you need to set a new OnClickListener for each button.

 public class MyActivity extends Activity  {
    int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button myButton1 = (Button) findViewById(R.id.my_button1);
        Button myButton2 = (Button) findViewById(R.id.my_button2);

        myButton1.setOnClickListener( new OnClickListener() {
          @Override
            public void onClick(View v) {
                // Do what you want here
                a = 1;
            }
        });

        myButton2.setOnClickListener( new OnClickListener() {
           @Override
            public void onClick(View v) {
                // Do what you want here
                a = 2;
            }
        });
      }

2) As you see in the first approach, we need to make a new Object from the OnClickListener for each button. We can mix all that into one OnClickListener for performance and readability reasons.

public class MyActivity extends Activity implements View.OnClickListener {
    int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button myButton1 = (Button) findViewById(R.id.my_button1);
        myButton1.setOnClickListener(this);
        Button myButton2 = (Button) findViewById(R.id.my_button2);
        myButton2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_button_1:
                a = 1;
                break;
            case R.id.my_button_2:
                a = 2;
                break;
        }
    }
    ...
}

3) You don't need to assign an id for this option you just need to assign the method name in the XML and then implement the same method in the activity with the exact same name but it must take a View object as an argument.

<Button 
...
android:onClick="button1Click" />

<Button 
...
android:onClick="button2Click" />

and then in your activity just write the methods.

public class MyActivity extends Activity implements View.OnClickListener {
    int a;

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

    public void button1Click(View v) {
       a = 1;
    }


    public void button2Click(View v) {
       a = 2;
    }
}

That's all your options. I personally prefer number 2.

Comments