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/
Related
Is there an XML attribute so that I can set the appearance of my custom Preference with attribute: android:enabled="false"?
For example, if you use a default CheckBoxPreference and disable it, it will be greyed out and indicate to the user that it is disabled and clicking it will probably do nothing.
But with my custom preference, disabling it will not make it look any different, and thus it will confuse the user when they click on it and it does nothing.
Create drawable checkbox.xml file consisting of <selector> which sets checkbox button's image depending on its status:
<?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/checkbox_checked" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#drawable/checkbox_unchecked" /> <!-- default -->
</selector>
You can even use colors in place of these drawables.
In your custom checkbox preference checkbox_preference.xml using this drawable add checkbox's button. Place this file in layout folder:
<CheckBox android:id="#+android:id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox" />
Inflate this using your prefs.xml . The layout attribute is used to put the custom layout with the custom checkbox.
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/category_title">
<CheckBoxPreference
android:key="preferenceKey"
android:title="#string/preferenceTitle"
android:defaultValue="false"
android:layout="#layout/checkbox_preference"
/>
</PreferenceCategory>
</PreferenceScreen>
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 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.
I have some textViews in my LinearLayout.
They are clickable and I would like to make them onClick like for a ListView.
For the listView, when the user clicks an item, the background becomes green I think.
I know that i can do this manually with
tv.SetBackgroundColor(Color.GREEN);
But is there something automaticaly to do this, like for the listView where the selection is managed automaticaly.
Thank you.
You need to define the background as a new XML file containing a list of states.
http://developer.android.com/guide/topics/resources/color-list-resource.html
For example, create a file called background_states.xml in your drawable folder and write something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#color/white" ></item>
<item
android:state_pressed="true"
android:drawable="#color/white" ></item>
<item
android:drawable="#color/black" />
</selector>
Then define this new file as background in your TextView:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/background_states"
See the link above for more information about the different states.
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"/>