How to add checkboxes dynamically in android
Just change your listener to(perfectly working,I have tried):
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
}
});
Note the two changes:
View.OnClickListener
toOnClickListener
new CheckBox(this)
tonew CheckBox(getApplicationContext())
CheckBox ch = new CheckBox(this);
change (this) to new CheckBox(your activity name.this); because if you pass this it will not take the Context of your activity because you code this line inside the click listener.
It's a few years on from this question but the requirement has not changed... here is how I made it work on API 22. The slight variation is I have a floating action button (fab) on a main layout and a content layout which is where I dynamically create the LinearLayout.
Note:
b.setOnClickListener(new View.OnClickListener()
and
CheckBox ch = new CheckBox(v.getContext());
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ViewGroup layout = (ViewGroup) findViewById(R.id.layout_id);
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! Plain Text");
ll.addView(tv);
EditText et = new EditText(this);
et.setText("This is edit text");
ll.addView(et);
Button b = new Button(this);
b.setText("This is a dynamically added button");
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(v.getContext());
ch.setText("I'm dynamic! "+i);
ll.addView(ch);
}
}
});
//this.setContentView(sv); // this causes the fab to fail
layout.addView(sv);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}