I have tried to make a xml selector with the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shuffleon" android:state_checked="true" />
<item android:drawable="#drawable/shuffleoff" android:state_selected="false" />
</selector>
and when I try to set the backgroundDrawable to the checkBox the checkbox doesn't replace the CheckBox style too:
shuffle.setBackground(android.support.v4.content.res.ResourcesCompat.getDrawable(getResources(), R.drawable.shuffle, null));
Following this question: Change icons of checked and unchecked for Checkbox for Android I need to set the button drawable with my xml: android:button="#drawable/checkbox" but I can't do this because I'm creating the CheckBox programmatically.
Is there a way how to achieve this?
Simple way
Create a new layout in drawable folder and name it custom_checkbox (You can rename also)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/Checked_image_name"android:state_checked="true"/>
<item android:drawable="#drawable/Unchecked_image_name" android:state_checked="false"/>
</selector>
Use this in your layout activity
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_checkbox"/>
Use below line of code, i think it will work
yourcheckbox.setButtonDrawable(yourdrawable);
If you use sdk 28 (androidx) and above try to use
androidx.appcompat.widget.AppCompatCheckBox
instead of
Checkbox
Use This
shuffle.setButtonDrawable(R.drawable.custom_checkbox_selector);
XML code;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radiobutton_checked" android:state_checked="true"/>
<item android:drawable="#drawable/radiobutton_unchecked" android:state_checked="false"/>
</selector>
This works for me:
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_checkbox"
android:button="#null"/>
drawable/checkbox_custome.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/checkbox_empty" />
<item android:state_checked="true"
android:drawable="#drawable/checkbox_check" />
</selector>
Related
So, I've done selectors in the past and I have no idea why it doesn't work this time. I have 2 vector drawables and the plan is to change between them on click.
I've created the following selector file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_vector_favorite"
android:state_checked="true"
android:state_selected="true"/>
<item android:drawable="#drawable/ic_vector_favorite_border"
android:state_checked="false"/>
</selector>
And this is my checkbox:
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/ic_phone_margin"
android:button="#drawable/ic_favorite_selector"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/iv_call"
app:layout_constraintTop_toTopOf="parent"/>
But, whenever I click it, nothing changes. I've tried with android:checked="true" but it's still the same.
You just need to remove android:state_selected="true" from your selector. It should just be
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_vector_favorite"
android:state_checked="true"/>
<item android:drawable="#drawable/ic_vector_favorite_border"
android:state_checked="false"/>
</selector>
Use this attribute
app:useMaterialThemeColors="false"
Add this in your module-level build.gradle
vectorDrawables.useSupportLibrary = true
Add this in your Activity/Fragment
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
Remove Vector drawable from XML
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/ic_phone_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/iv_call"
app:layout_constraintTop_toTopOf="parent"/>
Set CheckBox custom drawable programmaticully like this
CheckBox cb = findViewById(R.id.checkBox);
cb.setButtonDrawable(AppCompatResources.getDrawable(getContext(), R.drawable.ic_favorite_selector));
I have
Checkbox.setButtonDrawable(R.drawable.ic_checkbox_disabled);
Here i am setting to custom image
How to set to default check box background
I tried:
Checkbox.setButtonDrawable(null);
But entire box diseappered
So I need the resource name of the checkbox android uses
Something like:
Checkbox.setButtonDrawable(R.android.resourcename);
Any solution for this
Create a new selector file in drawable folder with name checkbox_selection :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/checked"
android:state_checked="false"/>
<item android:drawable="#drawable/uncheck"
android:state_checked="true"/>
<item android:drawable="#drawable/checkbox"/>
</selector>
Modify your checkbox view in xml file as show below :
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selection"
android:text="CheckBox"
android:textColor="#color/Black" >
</CheckBox>
In drawable folder, make one file and name anything you want. Then paste this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_checked_icon" android:state_checked="true"/>
<item android:drawable="#drawable/radio_unchecked_icon" android:state_checked="false"/>
</selector>
radio_checked_icon and
radio_unchecked_icon, these are the custom images you want to put, when your checkbox is clicked, then radio_checked_icon, else radio_unchecked_icon
and then in layout use like this:
<CheckBox
android:id="#+id/tiny_radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:button="#drawable/name_of_your_file_present_in_Drawable"
android:padding="#dimen/padding_short" />
I want to make a custom RadioGroup with shapes of my own.
I want the android:checked="true" to look like this:
I think you should use a selector in that case. I use this for CheckBoxes, simply put the resulting XML file as android:background. This whould be an example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/radio_checked" />
<item android:state_checked="false" android:drawable="#drawable/radio_unchecked" />
</selector>
I have developed an application that has one preferences activity for application setting so I want know about can we set custom graphics layout (XML) on preferences layout like CheckBox has own color Button while check and unchecked and list has own color if possible then how?
create XML in Drawable with button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="false" android:drawable="#drawable/radio_on"/>
<item android:state_checked="false" android:state_pressed="false" android:drawable="#drawable/radio_off"/>
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/radio_on"/>
<item android:state_checked="false" android:state_pressed="true" android:drawable="#drawable/radio_off"/>
</selector>
radio_on
radio_off
Put both image in drawable folder radio_on.png and radio_off.png
Now at Your Radio Button Write This
android:button="#drawable/button"
custom_checkbox_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+android:id/checkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"
android:clickable="false" android:button="#drawable/button"/>
After that in the CheckBoxPreference set android:widgetLayout="#layout/custom_checkbox_layout"
I've been trying to customize the toggle button look but with no success.
Here is how I want it to look like:
Can someone give me a tutorial?
create toggle_selector.xml in res/drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
</selector>
apply the selector to your toggle button
<ToggleButton
android:id="#+id/chkState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/toggle_selector"
android:textOff=""
android:textOn=""/>
Note: for removing the text i used following in above code
textOff=""
textOn=""
I don't know if this is the best solution but it worked fine for me:
1.- Decide how big do you want the toggle button. In my case width 56dp and height 76dp.
2.- Create the icon set 56px-76px for mdpi, 84px-113px hdpi, same for xhdpi and xxhdpi
3.- Move the icons in the corresponding drawable folder. In my case 20 icons 5 in each folder, named ic_name1_on, ic_name1_off [...] ic_name5_off
4.- Create the following xml files in a new folder called drawable (if it does not exist yet):
ic_name1_toggle.xml
ic_name1_toggle_bg.xml
ic_name2_toggle.xml
(...)
ic_name5_toggle_bg.xml
5.- In ic_name1_toggle.xml the code must be:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="false"
android:drawable="#drawable/ic_name1_off" />
<item
android:state_checked="true"
android:drawable="#drawable/ic_name1_on" />
</selector>
6.- In ic_name1_toggle_bg.xml the code must be:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/ic_name1_toggle" />
</layer-list>
7.- Finally in your layout.xml:
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="56dp"
android:layout_height="76dp"
android:background="#android:color/transparent"
android:button="#drawable/ic_name1_toggle_bg"
android:textOff=""
android:textOn="" />
I think you need to define a custom background for your button.
Take a look at the Developer Guide on customizing a Button's background.
However, in step Three, Create a new XML file in the res/drawable/ directory Use this Xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_da"
android:state_checked="true" />
<item android:drawable="#drawable/button_nu" />
</selector>
The element android:state_checked="true" is what defines that state as the checked background.
Let me know if this works for you.
Create selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_da" android:state_checked="true"/>
<item android:drawable="#drawable/btn_nu"/>
</selector>
and use it as background for your ToggleButton.