Reputation: 13
I write code to generate dynamic buttons , but I don't know how to implement click event for each button dynamically. I found some answers but not work with my code... Please help me.. This is my code
public class dynamicbuttion extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for(int i = 0; i < 5; i++) {
Button btn = new Button(this);
btn.setId(i);
btn.setText("dynamic buttion " + i);
ll.addView(btn);
}
this.setContentView(sv);
}
}
Upvotes: 1
Views: 10004
Reputation: 113
I know the post is very old, but I end up here looking for answers and I end up reading the setTag()
method and it solved my problem. Now, I want to add an answer that I feel is cleaner than adding the setOnClickListener()
it looks like this:
public void createButtonList(HashMap<String, Integer> buttonList){
LinearLayout buttonLayout = findViewById(R.id.btnlyt);
for (Map.Entry<String, Integer> entry : buttonList.entrySet()) {
String key = entry.getKey();
Button button = new Button(this);
button.setText(key);
button.setTextSize(27);
button.setTextColor(Color.rgb(150, 190, 200));
button.setTypeface(Typeface.MONOSPACE);
button.setTag(key);
buttonLayout.addView(button);
button.setOnClickListener(this);
}
}
@Override
public void onClick(View view) {
String clickedButton = view.getTag().toString();
}
So in my case I have a HashMap with info about the text I want on the Buttons, important thing here is the button.setOnClickListener(this);
and also VERY important, add at the class creation implements View.OnClickListener
it looks like this:
public class ClassName extends AppCompatActivity implements View.OnClickListener
Upvotes: 0
Reputation: 1172
For dynamic requirements list will be the best option,Also you can inflate XML view for dynamic drawing
try {
int brotherCount =5;
List<View> dynamicView = new ArrayList<>();
List<TextInputEditText> dynamicBrotherText = new ArrayList<>();
List<TextInputLayout> dynamicLayoutBrother = new ArrayList<>();
for(int i=1;i<=brotherCount;i++){
View family_static = getLayoutInflater()
.inflate(R.layout.my_family_static, mLayoutBrother, false);
TextInputEditText inputEditText=family_static.findViewById(R.id.TextInputEditTextFather);
TextInputLayout inputLayout=family_static.findViewById(R.id.TextInputLayoutFamily);
inputEditText.setText("name"+i);
inputLayout.setHint(getResources().getString(R.string.about_my_brother));
dynamicBrotherText.add(inputEditText);
dynamicLayoutBrother.add(inputLayout);
dynamicView.add(family_static);
mLayoutBrother.addView(family_static);
}
for(View view :dynamicBrotherText){
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showToast(((TextInputEditText)view).getText().toString());
}
});
}
} catch (Exception e) {
e.printStackTrace();
Log.d("", "createBrotherLayout: ");
}
Upvotes: 0
Reputation: 52810
I have done by using Custom Layout:
private LinearLayout linearLayoutParent;
onCreate:
linearLayoutParent = (LinearLayout)findViewById(R.id.linearLayoutParent);
Now whenever need to create dynamic textviews, simply we will add to Parent Linear Layout:
TextView[] name = new TextView[10];
for (int i = 0; i < 10; i++) {
View view = getLayoutInflater().inflate(R.layout.child_view, linearLayoutParent, false);
name[i] = (TextView) view.findViewById(R.id.child_name);
name[i].setText("Dynamic Textxview " + i);
name[i].setId(i);
name[i].setTag(String.valueOf(i));
ll.addView(view);
name[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Clicked", ""+v.getTag());
}
});
}
child_view.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/child_name"
android:layout_width="120dp"
android:layout_height="50dp"
android:background="#969696"
android:gravity="center"
android:text=""
android:textColor="#fff"
android:textSize="16sp" />
Hope this will help you.
Upvotes: 4
Reputation: 52810
public class DynamicButton extends Activity {
private LinearLayout ll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dynic_button);
ll = (LinearLayout) findViewById(R.id.llContent);
Button[] dynamic_button = new Button[10];
for (int i = 0; i < 10; i++) {
dynamic_button[i] = new Button(this);
dynamic_button[i].setId(i);
dynamic_button[i].setTag("" + i);
dynamic_button[i].setText("My Dynamic Button No: " + i);
ll.addView(dynamic_button[i]);
dynamic_button[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("Clicked", "" + v.getTag());
}
});
}
}
}
Upvotes: 0
Reputation: 3147
Button[] btn = new Button[5];
for(int i = 0; i < 5; i++) {
btn[i] = new Button(this);
btn[i].setId(i);
btn[i].setText("dynamic buttion " + i);
ll.addView(btn[i]);
btn[i].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//your desired functionality
}
});
}
Upvotes: 5
Reputation: 16651
Use an anonymous OnClickListener implementation. Like this:
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
doThisWhenClicked();
}
});
Upvotes: 0