Android provides facility to customize the UI of view elements rather than default.
You are able to create custom CheckBox in android. So, you can add some different images of checkbox on the layout.
Example of Custom CheckBox
In this example, we create both default as well as custom checkbox. Add the following code in activity_main.xml file.
activity_main.xml
File: activity_main.xml
android:id=“@+id/checkBox4”
android:button=“@drawable/customcheckbox”
android:layout_below=“@+id/checkBox3”
android:layout_alignLeft=“@+id/checkBox3”
android:layout_alignStart=“@+id/checkBox3” />
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:textAppearance=“?android:attr/textAppearanceSmall”
android:textSize=“25dp”
android:text=“Custom Check Box”
android:id=“@+id/textView”
android:layout_alignTop=“@+id/viewStub”
android:layout_centerHorizontal=“true” />
<Button
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“Show Checked”
android:id=“@+id/button”
android:layout_alignParentBottom=“true”
android:layout_centerHorizontal=“true” />
</RelativeLayout>
Now implement a selector in another file (checkbox.xml) under drawable folder which customizes the checkbox.
checkbox.xml
File: checkbox.xml
Activity class
File: MainActivity.java
sb.append(s1);
}
if(cb2.isChecked()){
String s2=cb2.getText().toString();
sb.append(“\n”+s2);
}
if(sb!=null && !sb.toString().equals(“”)){
Toast.makeText(getApplicationContext(), sb, Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(),“Nothing Selected”, Toast.LENGTH_LONG).show();
}
}
});
}
}
Output
Leave A Comment