hemanth
hemanth

Reputation: 191

creating a button inside onclick listener?

I want to dynamically create a set of buttons when my program runs.I want these buttons to be created when i create a generate button.Is it possible to call Button but=new Button(this); inside OnClickListener of another Button?

Upvotes: 0

Views: 277

Answers (3)

lrAndroid
lrAndroid

Reputation: 2854

Yes -- just be sure to change new Button(this) to new Button(mClass.this).

Upvotes: 1

cstrutton
cstrutton

Reputation: 6157

This does work. I have an activity that creates a button in the same way. To set onClickListener to a method in the outer class, you need to use (mClass.clicklistener) to set the listener methods.

Upvotes: 0

ngesh
ngesh

Reputation: 13501

Yes.. say Button b = new Button(yourAct.this);..

and do not forget to add it to your parent layout by saying addView(b)

Upvotes: 1

Related Questions