Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating Buttons and setting onClickListener

I have problem with handling dynamically created Buttons on Android. I'm creating N buttons and I have to do the same method when button is clicked but I have to know which button is clicked.

for (int i = 0; i < NO_BUTTONS; i++){         Button btn = new Button(this);         btn.setId(2000+i);          ...          btn.setOnClickListener((OnClickListener) this);         buttonList.addView(btn);         list.add(btn); 

Cucurrently I'm adding ID to every button and I'm using the method below to see which button was clicked. (line btn.setId(2000+i); and btn.setOnClickListener((OnClickListener) this);). This method is also implemented in the activity.

@Override public void onClick(View v) {     switch (v.getId()){         case 2000: selectButton(0);         break;          ...          case 2007: selectButton(7);         break;     }  } 

This doesn't look good to me so i'm asking is there some better way to do this? or how to send some information to onclick event? any suggestions?

like image 863
Ante Avatar asked Dec 09 '10 17:12

Ante


People also ask

How do you create a dynamic button?

This example demonstrates how do I add a button dynamically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How many ways can you attach OnClickListener to button layout?

Using an OnClickListener There are two ways to do this event handler programmatically : Implementing View. OnClickListener in your Activity or fragment.

Which are the ways to set OnClickListener for button in Android?

You've got the onClick property set in your xml file to a method called setLogin. For clarity, I'd delete this line android:onClick="setLogin" and call the method directly from inside your onClick() method. Then set the new Activity to have the new layout. Show activity on this post.

How do I use OnClickListener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.


1 Answers

You could create a method that returns an onclickListener and takes a button as a parameter. And then use that method to set the onClicklistener in the first loop you have..

Update: code could be soemthing along these lines:

View.OnClickListener getOnClickDoSomething(final Button button)  {     return new View.OnClickListener() {         public void onClick(View v) {             button.setText("text now set.. ");             }     }; } 

as a method in the activity and then use it in the loop like this

button.setOnClickListener(getOnClickDoSomething(button)); 
like image 175
Manfred Moser Avatar answered Sep 30 '22 12:09

Manfred Moser