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