Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add checkboxes dynamically in android

Tags:

android

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);

   }
 }
like image 831
hemanth kumar Avatar asked Oct 01 '11 06:10

hemanth kumar


People also ask

How to add Check boxes in Android studio?

To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

What is CheckBox in Android studio?

Selection controls allow the user to select options. Use checkboxes to: Select one or more options from a list.

Which xml element is used to design a custom CheckBox?

To customize the checkbox we will implement a selector in the checkbox. xml file under the drawable folder.


2 Answers

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:

  1. View.OnClickListener to OnClickListener
  2. new CheckBox(this) to new CheckBox(getApplicationContext())
like image 84
Hanry Avatar answered Oct 08 '22 03:10

Hanry


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();

            }
        });
    }
like image 22
CodeCobbler Avatar answered Oct 08 '22 03:10

CodeCobbler