-1

The problem:

Add dynamically checkboxes to the LinearLayout. Current range of child views 2-7, but quantity is not limited.

My first approach was pure in the code:

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
lp.gravity = Gravity.CENTER;

for (String answer : generateAnswers()) {
    CheckBox checkBox = new CheckBox(getContext());
    checkBox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
    checkBox.setGravity(Gravity.CENTER);
    checkBox.setText(answer);
    mvAnswersLayout.addView(checkBox, lp);
}

Feels like CheckBox item should live in a xml, so second variant:

LayoutInflater inflater = LayoutInflater.from(getActivity());
for (String answer : generateAnswers()) {
    View v = inflater.inflate(R.layout.answer_item, mvAnswersLayout, true);
    CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkbox_id);
    checkBox.setText(answer);
    checkBox.setId(General.generateViewId());
}

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBox
        android:id="@+id/checkbox_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="17sp"
        android:padding="4dp"
        />
</merge>

To eliminate redundant viewgroup I use tag <merge> and utility View.generateViewId().

Seems the second approach is the way every android developer would expect. But what bothers me is hitting findViewById() multiple times (every next one will be a bit slower).

Any pros and cons?

Community
  • 1
  • 1
Maxim G
  • 1,479
  • 1
  • 15
  • 23

2 Answers2

1

Better use Recyclerview or Listview with each row as checkbox for better performance if you have many checkboxes. Also maintaining the state is also easy in recylerview or listview.

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34
  • For truly unlimited list with the need of recycling Recyclerview shines. But I'm interested in mechanics, and the next question would be why Recyclerview doesn't use `` tag? – Maxim G May 18 '16 at 09:15
0

If you want to add dynamically without using RecycleView or ListView, you can create check box like below

 ViewGroup.LayoutParams checkbox_relativeParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            checkbox_relativeParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            checkbox_relativeParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            CheckBox cb = new CheckBox(getActivity());
            cb.setText("I'm dynamic!");
            cb.setGravity(Gravity.CENTER);
            cb.setTextSize(17);
            cb.setPadding(4,4,4,4);
            cb.setLayoutParams(checkbox_relativeParams);

and add this checkbox to the layout dynamically.

 this.addView(cb, checkbox_relativeParams);

with out request for findViewbyID(id) always as there is only one widget in the layout.

pokuri
  • 156
  • 7