Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

generic listener for buttons in a layout of android?

Tags:

android

Scenario: I have three buttons defined in xml

<button android:id="@+id/firstbtn" 
    ...
/>
<button android:id="@+id/secbtn" 
    ...
/>
<button android:id="@+id/thirdbtn" 
    ...
/>
In Java one way to  listen to them is  
Button firstbtn = (Button) findViewById(R.id.firstbtn);  
    firstbtn.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) {  
                Toast.makeText(getBaseContext(),   
                        "You have clicked first button",   
                        Toast.LENGTH_SHORT).show();  
            }  
        });  

for second btn , same code has to be repeated with different id ??
How can I make it generic enough that , it can listen to all buttons (say in for loop) and while handling I should be able to differentiate different btns. (may be get elements id)

like image 304
sat Avatar asked Oct 12 '10 10:10

sat


People also ask

How can we set click listener to a button?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

When a button click which listener you can use?

If you have more than one button click event, you can use switch case to identify which button is clicked. Link the button from the XML by calling findViewById() method and set the onClick listener by using setOnClickListener() method. setOnClickListener takes an OnClickListener object as the parameter.

When you clicked on button which type of listener is called in android?

OnLongClickListener . This is called when the user either touches and holds the item (when in touch mode), or focuses upon the item with the navigation-keys or trackball and presses and holds the suitable "enter" key or presses and holds down on the trackball (for one second).

What are the types of buttons in android?

There are different types of buttons in android such as RadioButton, ToggleButton, CompoundButton etc.


2 Answers

Instead of a huge set of findViewById calls I rather like to use the onClick="methodName" xml attribute. For example:

<LinearLayout ...>
  <Button android:text="1" onClick="onButtonClicked" clickable="true" />
  <Button android:text="2" onClick="onButtonClicked" clickable="true" />
  <Button android:text="3" onClick="onButtonClicked" clickable="true" />
  <Button android:text="4" onClick="onButtonClicked" clickable="true" />
</LinearLayout>

In the activity where the layout is shown just add a method

public void onButtonClicked(View v){
     // do whatever needs to be done. For example:
     Toast.makeText(getApplicationContext(), ((Button) v).getText() + " clicked", Toast.LENGTH_SHORT).show(); 
}

You can also put the onClick and clickable attributes into the res/styles.xml file to save even more typing:

<?xml version="1.0" encoding="UTF-8" ?>
<resources>
  <style name="clickable_button" >
    <item name="android:onClick" >onButtonClicked</item>
    <item name="android:clickable" >true</item>
  </style>
</resources>

Your layout is then simplified to

<LinearLayout ...>
  <Button android:text="1" style="@style/clickable_button" />
  <Button android:text="2" style="@style/clickable_button" />
  <Button android:text="3" style="@style/clickable_button" />
  <Button android:text="4" style="@style/clickable_button" />
</LinearLayout>
like image 80
Rodja Avatar answered Nov 12 '22 04:11

Rodja


You need not repeat same code for all. You can try out a generic listener, like :

private OnClickListener mCorkyListener = new OnClickListener() { 
    public void Click(View v) {
            // do something
    } 
}; 

Then all you have to do is register all three buttons to use this mCorkyListener. That is, inside onCreate(),

Button firstbtn  = (Button) findViewById(R.id.firstbtn); 
Button secondbtn = (Button) findViewById(R.id.secondbtn); 
Button thirdbtn  = (Button) findViewById(R.id.thirdbtn); 

firstbtn.setOnClickListener(mCorkyListener);
secondbtn.setOnClickListener(mCorkyListener);
thirdbtn.setOnClickListener(mCorkyListener);
like image 20
kiki Avatar answered Nov 12 '22 04:11

kiki