1

I am just starting to learn android application development. I was looking through the checkbox and probing on various possibilities of dynamically creating it and much more. Now, I have a small doubt out of curiosity. QUESTION : Say I have 10 items in a string array with each one having their own names. Now , all I needed to know is.. I can implement checkbox manually for these 10 items. But my question is , Say I have 100 items listed with each one having unique name.I want to create checkbox dynamically on reading the number of items in my string array and also to set the text of each checkbox to the correspondingly read item in my string array.

Example: say I have 4 names. Apple, Google, Microsoft, Linux. Now in my activity i wanted to set 4 checkbox dynamically and set the text of each checkbox to these four values. A generalized solution (i.e. to expand your code for any number of items) for this question will be appreciated .

Any help on this is highly appreciated. Thanks to those genius developers out there and I am so happy to be a part of this forum as well. This forum has taught me so many things with those incredible developers out there.

0

1 Answer 1

1

Activity:

public class CheckboxActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_checkbox);

        String[] array = new String[]{"Apple", "Google"};
        ViewGroup checkboxContainer = (ViewGroup) findViewById(R.id.checkbox_container);

        for (int i = 0; i < array.length; i++) {
            CheckBox checkBox = new CheckBox(this);
            checkBox.setText(array[i]);
            checkboxContainer.addView(checkBox);
        }
    }
}

layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/checkbox_container"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

</LinearLayout>
Sign up to request clarification or add additional context in comments.

6 Comments

@Fox in socks: Great man ! But I have a small doubt. Where did we declare the layout in the above code ? R.layout.act_checkbox... Does that mean we are saving the linear layout as act_checkbox ? Anyways, thanks a lot bro. Appreciate your help :) Will try this out and let ya know if its working. And one more thing ... Will I able to scroll through the checkbox list if I have say 100 items in the array ?? Please help me on this ..
@Sreekanth: Bro.. I have some basic knowledge on C and C++. So thought of implementing a for loop like the one which was given in the code by Fox in socks. But I wanted to make that list scrollable as well. So had a few doubts and thought of consulting you developers out here hoping that you guys would help me out on the same.
@Krish3090 you need to save layout files in the res/layout directory. To get scrollable items you need to use ListView or ScrollView. And please read the documentation because it's quite trivial questions.
@Foxinsocks : Will I be able to use the code snippet that you had given in the ListView ? or should I create a Linear Layout and attach a vertical scrollview for it to work ? Which option is better from your perspective?
@Krish3090 The best option in your case is a ListView. And ArrayAdapter implementation. Sample could be found here: vogella.com/tutorials/AndroidListView/article.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.