2

I'm currently trying to inflate a layout that I'll be eventually adding to a vertical LinearLayout container as part of a bigger dynamic form creation module.

layout_checkbox.xml is below:

<TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="left"
    android:text="sample text" />

<LinearLayout
    android:id="@+id/checkboxGroup"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="sample" />
</LinearLayout>

Now what I want to do if possible is to get hold of the Checkbox (@+id/checkbox) and create copies of it, all of them under the LinearLayout @+id/checkboxGroup.

layout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout checkboxGroup = (LinearLayout) layout.findViewById(R.id.checkboxGroup);
CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);
// code that supposedly clones/duplicates checkbox.

I'm aware that I can easily inflate Checkbox from another xml definition to create multiple instances of it, then add them to the group. The reason I'm trying to do it this way is because I'm creating a library of some sort, and I'm trying to enforce some sort of convention for it to be easier to use.

Edit:

This worked, but it easily smells of performance hit:

layout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
LinearLayout originalCheckboxGroup = (LinearLayout) layout.findViewById(R.id.checkboxGroup);
for (String entryItem : entry.fieldEntries) {
    LinearLayout tempCheckboxLayout = (LinearLayout) inflater.inflate(R.layout.layout_checkbox, container, false);
    LinearLayout checkboxGroup = (LinearLayout) tempCheckboxLayout.findViewById(R.id.checkboxGroup);
    CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);
    checkbox.setText(entryItem);
    checkboxGroup.removeView(checkbox);
    originalCheckboxGroup.addView(checkbox);
}
josephus
  • 8,284
  • 1
  • 37
  • 57

2 Answers2

1
what I want to do if possible is to get hold of the Checkbox (@+id/checkbox) and create copies of it

No, you can not duplicate/clone views because you will always and will have the same reference to the view and when one changes all will change, the only way to do it is to inflate a view each time if you want to use the same view.

Edit:

There is not way to clone/duplicate the checkbox but you can inflate a View (Linearlayout) each time you want to use the checkboxes. and use the checkbox that was inflated. do this everytime you are going to use/clone a checkbox.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
  • that's kind of the reason i wanted a CLONE, because I don't want another Checkbox that references the same object that I defined in the xml. I'm looking for something that can give me a new instance of a Checkbox, with all the same formatting, etc. of the Checkbox inside my xml, but not attached to the container. – josephus Jul 25 '14 at 04:55
  • "but not attached to the container"?? Just inflate it each time you want a new instance of it. – Rod_Algonquin Jul 25 '14 at 04:57
  • As mentioned in my question, I know I can inflate another xml and attach it to the layout. That would require me to define another maybe checkbox_item.xml, which I'm trying to avoid to minimize complexity (not for myself, but for the user of the library I'm trying to create). – josephus Jul 25 '14 at 05:01
  • @josephus see edit above. and BTW you cant do what you are trying to do, the only solution is to inflate LinearLayout everytime you want a clone to cehckbox – Rod_Algonquin Jul 25 '14 at 05:06
  • I might have misunderstood your answer initially, but I get it now. The trick is to inflate the same LinearLayout for each checkbox I want to add, get hold of the Checkbox inside it, remove it from the parent, then add it to my first LinearLayout. If you edit your answer I'll accept. :) Thanks! – josephus Jul 25 '14 at 06:42
  • @josephus yea that was I was saying. :)) editted agina – Rod_Algonquin Jul 25 '14 at 07:00
1

Probably add a list for the text of checkbox

  private LinearLayout checkboxGroup;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.test);

    checkboxGroup = (LinearLayout) findViewById(R.id.checkboxGroup);
    CheckBox checkbox = (CheckBox) checkboxGroup.findViewById(R.id.checkbox);

    List<String> list = null;  //set it 
    addCheckBox(checkbox, list);

}

private void addCheckBox(CheckBox checkbox, List<String> checkBoxTextList) {

    for (int index = 0; index < checkBoxTextList.size(); index++) {
        CheckBox checkboxClone = checkbox;
        checkboxClone.setText(checkBoxTextList.get(index));
        checkboxClone.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub

            }
        });

        checkboxGroup.addView(checkboxClone);
    }

}
Deejaygeekout
  • 232
  • 2
  • 5
  • the problem with 'CheckBox checkboxClone = checkbox;' is it's still the same object being referenced. Also, calling addView to attach a view that already has a parent will cause an error. – josephus Jul 25 '14 at 06:40