Reputation: 1345
I would like to programatically style my dynamically created checkboxes as radio buttons as I prefer the look.
To do this I have created a checkboxStyle.xml and put it in values section of the res folder.
checkboxStyle.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="checkboxStyle">
<item name="android:checkboxStyle">@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton</item>
</style>
</resources>
I am then calling this in my fragment as I create my checkboxes but R.id.checkboxStyle
is coming up as not found, not really sure where to put this id though or if I need to refer to it in another way
CheckBox checkBox;
ArrayList<String> list = new ArrayList<>();
list.add("test");
list.add("this");
list.add("thing");
final ArrayList<CheckBox> checkBoxArray = new ArrayList<>();
for(int i = 0; i < list.size(); i++){
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
checkBox.setId(i);
checkBox.setText(list.get(i));
checkBox.setTag(list.get(i));
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
Log.v("CheckBox - checked", buttonView.getTag().toString());
}else{
Log.v("CheckBox - unchecked", buttonView.getTag().toString());
}
}
});
checkBoxArray.add(checkBox);
checkboxContainer.addView(checkBox);
}
testing_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/checks"
android:orientation="vertical">
</LinearLayout>
<Button
android:id="@+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
Upvotes: 1
Views: 102
Reputation: 2238
checkBox = new CheckBox(getActivity(), null, R.id.checkboxStyle);
it's not an id
, it's an style
and you need to access it like this
R.style.checkboxStyle;
Upvotes: 3