I'm trying to apply custom image to checkbox in android, for this I create an check_custom.xml file in which I define custom image for different states of check box like:
<item android:state_checked="true"
android:drawable="#drawable/btn_check_on" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#drawable/btn_check_off" /> <!-- unchecked -->
<item android:state_focused="true"
android:drawable="#drawable/btn_check_onfocus" /> <!--on focus-->
<item android:drawable="#drawable/btn_check_off" /> <!-- default -->
Three different images on three states on checked,on focus and on unchecked,
and then I assign this xml file to background attribute of check boxes,but I'm not getting required result, this technique apply the custom image as well as default image both.
Salman, set android:button instead of android:background. See also Custom checkbox image android
I didn´t like the appearance of the checkbox default image when the background layout was dark, so I changed it using a xml selector in order to have a white background in the button.
The selector is needed in android:button="#drawable/selector_check"
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Show password"
android:textColor="#color/white"
android:button="#drawable/selector_check"
>
And the following is the content of "drawable/selector_check.xml":
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#android:drawable/checkbox_on_background" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#android:drawable/checkbox_off_background" /> <!-- unchecked-->
</selector>
And this is the result:
Copy the btn_check.xml from android-sdk/platforms/android-#/data/res/drawable to your project's drawable folder and change the 'on' and 'off' image states to your custom images.
Then your xml will just need android:button="#drawable/btn_check"
<CheckBox
android:button="#drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
If you want to use different default android icons, you can use android:button="#android:drawable/..."
<item android:drawable="#drawable/ON" android:state_checked="false"/>
<item android:drawable="#drawable/OFF" android:state_checked="true"/>
<item android:drawable="#drawable/ON"/>
Related
I want to change drawable of predefined checked in
android.R.layout.simple_list_item_checked it is possible?
From Checked
and Unchecked
To Checked
and Unchecked
And I don't want to create custom list item.
This is possible but only by using a custom layout with a standard adapter, you can do this by defining the same ids as the predefined layout with your custom layout but only changing the ones you want. Unfortunately in this case, the source code for android.R.layout.simple_list_item_checked doesn't have a drawable by default. (https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_checked.xml) it uses the checkmark attribute
android:checkMark="?android:attr/textCheckMark"
I'm unsure whether you can change this, but i still reccomend using a custom layout with a standard adapter, just compare the original with your current layout and define all the same attributes with a drawable instead of a checkmark, like in the example below: It's up to you though.
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="#drawable/checkbox_background"
android:button="#drawable/checkbox" />
This is how you assign custom images saved in the #drawable directly depending on the state of the checkbox.
<?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/first" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/second" />
<item android:state_checked="false"
android:drawable="#drawable/third" />
<item android:state_checked="true"
android:drawable="#drawable/fourth" />
</selector>
further reading: http://developer.android.com/reference/android/widget/CompoundButton.html#setChecked%28boolean%29
more here: http://www.android-ios-tutorials.com/android/custom-android-checkbox-radiobutton/
I would like to adding some effects like color change to my android button when user focus/click the button
My android button layout is here :
<Button
android:id="#+id/btnUpload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/btn_bg"
android:text="தகவல் அனுப்புக"
android:textColor="#color/white"
android:padding="20dp"
android:margin="20dp"
android:layout_alignParentBottom="true" />
You should check out the documentation regarding color state lists.
Create an XML file in your drawable folder with the name of btn_background and place the following code in the file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
After you have created this file, change the background attribute of your Button to point to the new background:
android:background="#color/btn_background"
Good luck and happy coding!
Android Studio 0.3.7
Hello,
I have created 2 buttons png and patched using draw9patch.
The buttons will indicate whether the buttons is pressed or unpressed.
I have the following buttons_colours.xml in my values directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/rd_btn_press"/> <!-- pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/rd_btn"/> <!-- unpressed -->
<!-- Default -->
<item android:drawable="#drawable/rd_btn"/>
</selector>
In my layout for the button I have this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/rd_btn"/>
Problem 1: I get an element selector must be declared in my buttons_colours.xml
Problem 2: Not sure if this is related to problem 1, but the button never changes to my rd_btn_press state when I press it.
Many thanks for any suggestions,
you have set wrong background for the button. Replace #drawable/rd_btn with `buttons_colours :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/buttons_colours"/>
also, I don't know why you have two states in your items in the selector. Here an example of working selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rd_btn_press"
android:state_pressed="true" />
<item android:drawable="#drawable/rd_btn"
android:state_focused="true" />
<item android:drawable="#drawable/rd_btn" />
</selector>
I'm developing an Android application and I use this to customize RadioButtons:
<!-- Radio button style -->
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/checkbox_unchecked</item>
</style>
But the image #drawable/checkbox_unchecked is very big.
Do you know how can I make it smaller?
I know you can achieve what you want by reducing the size of your image. But I would recommend using the following code. This will help you set images for both the unselected and selected modes.
This is how you can achieve it, in a few steps:
-Create Image Drawables for their two states (checked/unchecked).
-Create selector for this two drawables. The sample content should be something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/unchecked" />
<item android:state_checked="false" android:drawable="#drawable/checked" />
</selector>
-Add this selector as a android:button attribute to RadioButton
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:text="Custom RadioButton" />
</LinearLayout>
I the following inside the xml layout for my ListView row:
<CheckBox
android:id="#+id/favbutton"
style="?android:attr/starStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
And it shows a blue star which can be checked to select a favourite. This is exactly the functionality that I need, but I just would like to customize the star icon that is shown. I would like to change the colour to yellow instead of blue and also make the star smaller if possible. Is it possible to make these changes by making changes to the theme applied to the application? What would I need to edit to change the look of the star icon?
I resolved this as follows. I created a file in res/drawable called btn_star_selector.xml and defined the selector as follows:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_pressed="true" android:drawable="#drawable/btn_star_on_pressed" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/btn_star_off_pressed" />
<item android:state_checked="true" android:drawable="#drawable/btn_star_on" />
<item android:state_checked="false" android:drawable="#drawable/btn_star_off" />
</selector>
With this selector defined I then replaced this line in the checkbox definition
style="?android:attr/starStyle"
with this
android:button="#drawable/btn_star_selector"
I then created four images to represent the different states of the checkbox, namely on, off, on pressed and off pressed and also placed these in res/drawables.
Hope this helps anyone else that is trying to figure out how to customize checkboxes.