I need to create edittext fields dynamically in android. I have gone through the link and had written the button click action for it. That is when I click on the button the check boxes has to display. But when I am creating the checkbox object in the onclick action it is showing error.
Can someone please tell me why is it showing error?
My code :
public class InflationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this);
et.setText("weeeeeeeeeee~!");
ll.addView(et);
Button b = new Button(this);
b.setText("I don't do anything, but I was added dynamically. :)");
ll.addView(b);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(int i = 0; i < 20; i++) {
CheckBox ch = new CheckBox(this);
ch.setText("I'm dynamic!");
ll.addView(ch);
}
}
});
this.setContentView(sv);
}
}
Just change your listener to(perfectly working,I have tried):
Note the two changes:
View.OnClickListenertoOnClickListenernew CheckBox(this)tonew CheckBox(getApplicationContext())