Reputation: 27
Create a button by this code is possible but I want click on a button and create new button(or anything else) again and again. How can do it?
Button b = new Button();
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(b,index);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
Upvotes: 1
Views: 104
Reputation: 188
public class test extends Activity {
private List<Button> mButtons = new ArrayList<Button>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teeest);
final LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayout);
Button b = new Button(this);
b.setText("Add Dynamic Button");
b.setBackgroundColor(Color.GREEN);
b.setVisibility(View.VISIBLE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Button b1 = new Button(teeest.this);
b1.setText("DYNAMIC BUTTON" + mButtons.size() + "");
b1.setWidth(100);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(teeest.this, "New Dynamic Button clicked", Toast.LENGTH_SHORT).show();
}
});
mButtons.add(b1);
ll.addView(b1);
}
});
Button bb = new Button(this);
bb.setText("Remove Dynamic Button");
bb.setBackgroundColor(Color.RED);
bb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mButtons.size()>0) {
ll.removeViewAt(mButtons.size() + 1);
mButtons.remove(mButtons.size() - 1);
}
}
});
ll.addView(b);
ll.addView(bb);
}
}
Upvotes: 0
Reputation: 3555
Button b = new Button();
...
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
})
Upvotes: 0
Reputation: 28823
If you want to create a new Button every time when any button is clicked, you can implement OnClickListener
in your activity, and override onClick
method.
Example snippet:
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
//your code
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//create a new button or textview
}
}
Upvotes: 0
Reputation: 381
Add Onclick listener to the button and add the button to the existing table
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// add button in table
tr.addView(b,index);
}
})
Upvotes: 1
Reputation: 388
Button newButton(){
Button b = new Button();
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
b.setText("Dynamic Button");
b.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tr.addView(b,index);
tl.addView(tr, new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
}
newButton().setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
newButton();
}
})
You can write code like this.... Try yourself...
Upvotes: 0