Change icons of checked and unchecked for Checkbox for Android
Kind of a mix:
Set it in your layout file :-
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox" />
where the @drawable/checkbox will look like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/checkbox_on_background_focus_yellow" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/checkbox_off_background_focus_yellow" />
<item android:state_checked="false"
android:drawable="@drawable/checkbox_off_background" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_on_background" />
</selector>
How to Change checkBox button icon in Android?
Try this ,
Make this xml your drawable folder
custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checkbox_inactive" android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_active" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_inactive"/>
</selector>
and used this for programatically add drawable
yourcheckbox.setButtonDrawable(R.drawable.custom_checkbox);
and used this for xml
android:button="@drawable/custom_checkbox"
Android: Change CheckBoxPreference icon for checked/unchecked programmaticaly
Found the solution:
I used the method found in the link that I'd provided in the question to set a selector so that I could use that with all of my CheckBoxPreferences for the different images based on the current state of the checkbox:-
<?xml version="1.0" encoding="utf-8"?>
//This is the xml selector "@drawable/checkbox"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/checkbox_off_background" />
<item android:state_checked="true"
android:drawable="@drawable/checkbox_on_background" />
</selector>
Accordingly, the CheckBoxPreference code changes as well:-
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:button="@drawable/checkbox"
android:clickable="true"
android:focusable="false" />
Now, I can call Checkbox.setChecked() to check whether it's enabled or not, and also to change it according to the values by setting it to true or false. So, according to the xml properties this will automatically change the icon as well.
Android replace checkbox style with custom drawable selector
Use below line of code, i think it will work
yourcheckbox.setButtonDrawable(yourdrawable);
Check box change background icons in Android
try this selector .
if I put drawable wrong then please change it.
it's work in my app.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
<item android:state_checked="false" android:state_window_focused="false"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important" />
<item android:state_checked="true" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
<item android:state_checked="false" android:state_focused="true"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important" />
<item android:state_checked="false"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important" />
<item android:state_checked="true"
android:state_enabled="true"
android:drawable="@drawable/ic_action_important_holo_dark" />
</selector>
Checkbox bad display when checked or unchecked
Update
This problem was fixed in com.android.support
v.28.0.0
, but if you don't want to upgrade, you can look at the solution below.
Since I didn't manage to fix my problem, I created my own selector for checkboxes.
1. In res/drawable/cbx_selector
:
<?xml version="1.0" encoding="utf-8"?> <!--Used for cbx check problem-->
<!--layer-list needed for paddings-->
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--Initial state-->
<item android:drawable="@drawable/ic_baseline_check_box_outline_blank_24px"
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp"/>
<!--Paddings to look similar to default Android checkboxes-->
<item
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp">
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="@android:integer/config_shortAnimTime"
android:exitFadeDuration="@android:integer/config_shortAnimTime"> <!--Used for animation-->
<item android:state_checked="true" android:drawable="@drawable/ic_baseline_check_box_24px"/>
<item android:state_checked="false" android:drawable="@drawable/ic_baseline_check_box_outline_blank_24px"/>
</selector>
</item>
</layer-list>
2. My checkbox drawables:
-> state checked: res/drawable/ic_baseline_check_box_24px
<vector android:autoMirrored="true" android:height="24dp"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
android:tintMode="multiply"
android:tint="@color/cbx_color_checked_selector">
<!--fillColor MUST be set to white-->
<path android:fillColor="@android:color/white" android:pathData="M19,3L5,3c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.11,0 2,-0.9 2,-2L21,5c0,-1.1 -0.89,-2 -2,-2zM10,17l-5,-5 1.41,-1.41L10,14.17l7.59,-7.59L19,8l-9,9z"/>
</vector>
-> state unchecked: res/drawable/ic_baseline_check_box_outline_blank_24px
<vector android:autoMirrored="true" android:height="24dp"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"
android:tintMode="multiply"
android:tint="@color/cbx_color_checked_selector">
<!--fillColor MUST be set to white-->
<path android:fillColor="@android:color/white" android:pathData="M19,5v14H5V5h14m0,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5c0,-1.1 -0.9,-2 -2,-2z"/>
</vector>
3. In res/color/cbx_color_checked_selector
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/colorAccent" android:state_checked="true" android:state_enabled="true" />
<item android:color="@color/cbxBorderColorUnchecked" android:state_checked="false" android:state_enabled="true" />
<item android:color="@color/colorAccentDisabled" android:state_checked="true" android:state_enabled="false" />
<item android:color="@color/cbxBorderColorUncheckedDisabled" android:state_checked="false" android:state_enabled="false" />
</selector>
Additional information:
- Checkbox drawables come from here: https://material.io/tools/icons/?style=baseline
- Thanks to this and this answer for their help.
Custom checkbox image android
Checkboxes being children of Button you can just give your checkbox a background image with several states as described here, under "Button style":
...and exemplified here:
How to change the color of a CheckBox?
If your minSdkVersion
is 21+ use android:buttonTint
attribute to update the color of a checkbox:
<CheckBox
...
android:buttonTint="@color/tint_color" />
In projects that use AppCompat library and support Android versions below 21 you can use a compat version of the buttonTint
attribute:
<CheckBox
...
app:buttonTint="@color/tint_color" />
In this case if you want to subclass a CheckBox
don't forget to use AppCompatCheckBox
instead.
PREVIOUS ANSWER:
You can change CheckBox
s drawable using android:button="@drawable/your_check_drawable"
attribute.
(Android) On checkbox unchecked/checked, the cursor in edit text shifts to initial position when used to hide/show password
You just need to use this line :
binding.etPassword.setSelection(binding.etPassword.text.length) as the below code.
import android.os.Bundle
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.testapp.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
initUI()
}
private fun initUI() {
binding.imgTogglePassword.setOnCheckedChangeListener { _, isChecked ->
// checkbox status is changed from uncheck to checked.
if (!isChecked) {
// hide password
binding.etPassword.transformationMethod = PasswordTransformationMethod.getInstance()
} else {
// show password
binding.etPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
}
binding.etPassword.setSelection(binding.etPassword.text.length)
}
}
}
Related Topics
Screen Video Record of Current Activity Android
Java.Lang.Illegalmonitorstateexception: Object Not Locked by Thread Before Wait()
Error Launching Android Studio: Failed to Create Jvm: Error Code-6
Android Firestore Convert Array of Document References to List<Pojo>
Dateformat Parse - Not Return Date in Utc
Getstring Outside of a Context or Activity
How to Execute Dex: Gc Overhead Limit Exceeded in Eclipse
Android Resource - Array of Arrays
Intellij Idea with Junit 4.7 "!!! Junit Version 3.8 or Later Expected:"
Elegant Way to Read File into Byte[] Array in Java
How to Use Okhttp to Upload a File
How to Know Whether I am in a Call on Android
What's a Good Library for Parsing Mathematical Expressions in Java
Convert Object to JSON in Android
How to Properly Use Backwards Compatible Vector Drawable with the Latest Android Support Library