user1767260
user1767260

Reputation: 1561

Dynamicaly create checkboxes in Android App

I have to create checkboxes in my app dynamicaly.The number of checkboxes depends on the response from the server.I can create the checkboxes dynamically as per the response from the server using list view.But the requirement is that the checkboxes should be loaded in two columns. Please check the image attached.Please help me.Thanks in advanceRequired Gui

Upvotes: 0

Views: 3749

Answers (3)

Naruto
Naruto

Reputation: 9634

i hope this post will be useful

How to horizontally align some programmatically added views?

let me know, if u face any issue

Upvotes: 0

Matthieu
Matthieu

Reputation: 16397

Dynamically create a TableLayout and fill it up with Checkboxes.

Something like that:

            TableLayout tl = new TableLayout(getActivity());
            int offset_in_column=0, table_size=/*the size of your answer from the server*/;
            TableRow tr=null;
            for (int offset_in_table=0; offset_in_table < table_size; offset_in_table++) {
                /* maybe you want to do something special with the data from the server here ? */

                if (offset_in_column == 0) {
                    tr = new TableRow(getActivity());
                    tr.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                }
                CheckBox check = new CheckBox(getActivity());
                check.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        /* add your code here */
                    }
                });
                check.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
                tr.addView(check);

                offset_in_column++;
                if (offset_in_column == 2) {

                    tl.addView(tr);
                    offset_in_column = 0;
                }
            }
            if (offset_in_column != 0)
               tl.addView(tr);

Upvotes: 1

Chintan Khetiya
Chintan Khetiya

Reputation: 16142

You have to create Table Layout and add Check Box Dynamically in that.

Show my this answer you can do by this way.

You must edit this code as per your requirement.

Upvotes: 0

Related Questions