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.
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 want make a list of Checkbox with white text in black background. We are using following code:
CheckBox chkAdditionalPack = new CheckBox(MainActivity.this);
chkAdditionalPack.setTag(j);
chkAdditionalPack.setText(offerPackageListForAddl.get(j).getOfferPackageName().toString());
chkAdditionalPack.setTextColor(Color.WHITE);
It gives a view like below:
The problem is now boxes of checkboxes is not clearly visible. How can I make it clearly visible keeping intact other parts?
If you are using android-studio, select your checkbox, and in the properties, set 'buttonTint' property value to whatever color you want using the editor. Or, if you prefer the XML solution, use
android:buttonTint="#color/white"
Note: This assumes that the color white is defined.
try the below xml code ref from solution
<?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/cbchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true" >
</item>
</selector>
One really simple way to do it, within the xml for the checkbox, simply set the android:background variable to the colour you wish.
Instead of having the text and check box created together like this, simply create a custom list item layout xml file.
e.g custom_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
/>
</LinearLayout>
Simply use this :
android:buttonTint="#FFFFFF"
** replace #FFFFFF with any color
I wanted a clickable TextView that changes color and holds it, that changes previously changed text back to default color. And of course I wanted some state to be changed when each TextView was "checked." Basically a TextView that acts like a RadioButton, or a RadioButton minus the button.
At first I tried to come at from the TextView angle. But it seems like using RadioButtons and adding text behavior is easier since RadioButtons extend TextView anyway.
So I have this color resource applied to each RadioButton:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:color="#0000ff" />
<item android:state_focused="true" android:state_pressed="true" android:color="#0000ff" />
<item android:state_focused="false" android:state_pressed="true" android:color="#0000ff" />
<item android:state_checked="true" android:color="#0000ff" />
<item android:color="#ff00ff00" />
</selector>
That gets me the text behavior I want. Now I just need a way to hide the RadioButton itself. Ideally this would be in XML so I can just apply a style to various RadioButton, but I'm not going to turn my nose up at a RadioButton extension.
So, anybody know how to hide the RadioButton button itself?
You should use CheckedTextView. It supports checked state.
OR
You can use RadioButton and set its button to null:
<RadioButton
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/selector"
android:button="#null"
android:paddingLeft="0dip"/>
I'm trying to give a custom background to my spinner. I've downloaded the HTC sense spinner design (Cf. following pictures) with the 9 patch format.
I've created a new XML file with the following code inside:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:drawable="#drawable/spinner_normal" />
</selector>
And I've added a spinner to my layout with this code:
<Spinner
android:id="#+id/my_spinner"
android:layout_width="180dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:background="#drawable/custom_spinner" />
But whereas being correctly displayed, I got the following format:
Did I make something wrong?
I hope you'll can help me.
Regards.
V.
The stretchable areas defined on your 9-patch seem wrong, and that is why your 9-patch are stretched like this. Have a look at Nine-patch docs and notice that the left-border defines what area should be stretched. I think you should also define a stretchable area below the "arrow" if you want the "arrow" to be centered.
If you do not know how to edit 9-patch you should use draw9patch it is included in the AndroidSDK/tools.
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"/>