selector with 9 patch - android

I'm try to create selector to CheckBox and replace the android:button with the selector.
In the selector i had two 9patch image , but for some reason that not work good.
the 9patch images are ok.
This the selector code:
<selector android:constantSize="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/bopen" />
<item android:state_checked="true"
android:drawable="#drawable/bclose" />
<item android:drawable="#drawable/bclose" />
</selector>
What can i do ?

Second line is missing the selector state hence you might see a problem with the selector, right?

Related

Changing Color State on an "ImageToggle" Button

I currently have a set of ToggleButtons, enclosed by a RadioGroup, with each button referring to a selector that changes the color of the button state, and writing code to implement the radio-buttonesque behavior. This works great. (EDIT: I should mention that the selector is what the android:background element of the button is referred to)
The Selector is something like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/gold"
android:state_checked="true" />
<item android:drawable="#color/white" />
</selector>
Now I want a second set of buttons to have the exact same behavior. The catch is that this time, I want Images on some of them, not text.
There are lots of StackOverflow posts on how to change the Image based on the Button's state but I don't want to change the image based on the state. I want to change the color of the button based on the state (the images have transparent backgrounds).
I'm really trying to avoid modifying the button images themselves.
I believe the solution involves making a selector with a layer-list but this is new territory for me.
Create a xml named colors.xml in res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="red">#ff0000</color>
<color name="green">#00ff00</color>
</resources>
In drawable folder, create a xml file my_btn_toggle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#color/red" />
<item android:state_checked="true" android:drawable="#color/green" />
</selector>
and in xml section define your toggle button:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btntoggle"
android:background="#drawable/my_btn_toggle"/>
I think you are looking for tinting attributes - mentioned here.
In your case, try to set ColoStateList to android:tint attribute. In ColorStateList doc you have example that shows color selector - like drawable selector, but for colors.
(I'm not sure it's already support pre-Lollipop devices or you should do it programatically with PorterDuff filters for older versions.)
What apparently one needs to do is create a layered drawable in the selector like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<layer-list>
<item android:drawable="#color/gold"
android:gravity="fill"/>
<item android:drawable="#drawable/image"
android:gravity="fill"/>
</layer-list>
</item>
<item android:state_checked="false">
<layer-list>
<item android:drawable="#color/white"
android:gravity="fill"/>
<item android:drawable="#drawable/image"
android:gravity="fill"/>
</layer-list>
</item>
</selector>
The standard approach to variable state button selectors is what Priyank mentions above. Just the graphic must be transparent and declared like above, such that it will be layered with a background color to pull off the color state change

How can I change the highlight color of a borderless button?

An ImageButton using the borderlessButtonStyle has the holo blue highlight when tapped on it. How can I change the highlight color?
(I know how to change the holo color on regular buttons and image buttons using http://android-holo-colors.com/)
thanks!
button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_checked="false" />
<item android:drawable="#drawable/button_focused_orange"
android:state_checked="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true"></item>
</selector>
copy this in drawable folder. And set the item drawables according to you. Then set it as
background for your button in layout xml.

How to change check box tick color in android

I am developing android application In that i use check box but default check box tick color is blue so i want to change that color to yellow. is there any inbuilt property to set color to check box tick.
Unfortunately, changing the color of checkbox check mark isn't a simple attribute
Create a selector xml file in res\drawables\ folder with name cb_selector.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="#drawable/checked" />
<item android:state_checked="false" android:drawable="#drawable/unchecked" />
</selector>
In your layout file apply this file to your checkBox
<CheckBox
android:id="#+id/cb"
android:text="My CheckBox"
android:button="#drawable/cb_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Add a unchecked.png, and checked.png in your drawables folder. These are checked and unchecked image of checkbox.
You can use the attribute app:buttonTint of the AppCompatCheckBox from the android.support.v7 library.
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="#color/colorAccent"/>
Advantage: works also below API 21 and you don't need to redraw the checkbox.
As of API 21 you can use the Button Tint attribute
android:buttonTint="#FFFF00"
If you want to do this programmatically, then you simply do this:
final CheckBox cb = new CheckBox(getApplicationContext());
cb.setButtonTintList(getColorStateList(R.color.colorAccent));
Chris Stillwell's answer gave me the idea to try this as I couldn't simply set the colour using the attributes. :)
Go to styles.xml and add this line.
<style>
<item name="colorAccent">#android:color/holo_green_dark</item>
</style>
using this you can change color or set different color
If you want to change only tint color than must go with the below solution. Its work perfectly.
Create a Selector "check_box_tint.xml" in your res/drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/your_checked_color" />
<item android:state_checked="false" android:color="#color/your_unchecked_color" />
</selector>
Now Use this drawable as color of your checkbox tint.
<CheckBox
android:id="#+id/cbSelectAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="#drawable/check_box_tint"/>
Kotlin version:
checkBox.buttonTintList = ColorStateList.valueOf(R.color.colorPrimary)
Use Custom selector for the checkbox.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/patch_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/patch_normal" android:state_enabled="true"/>
<item android:drawable="#drawable/patchdisable" android:state_enabled="false"/>
</selector>
Like this.
For those still looking for an answer (I am aware this is an older question) – I found this solution works well without having to worry about API: https://stackoverflow.com/a/31840734/7601437
In short: create a style for the checkbox, e.g. checkboxStyle and then implement it as a theme: android:theme="#style/checkboxStyle"
Firstly, we must create a drawable that include checked and uncheck color situations, then you must set this drawable as buttonTint;
drawable_checkbox;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/kelleyGreen" />
<item android:state_checked="false" android:color="#color/warmGrey" />
</selector>
<style name="CheckBox" parent="Widget.AppCompat.CompoundButton.CheckBox">
<item name="android:textAppearance">#style/TextAppearance.Regular.XSmall</item>
<item name="android:textColor">#color/warmGrey</item>
<item name="buttonTint">#drawable/drawable_checkbox</item>
</style>
For applying color programmatically it will require API Level >19 if your min sdk is >19 then you can use
checkbox[i]!!.setButtonTintList(getColorStateList(activity!!,R.color.green))
OR
view.setButtonTintList(getColorStateList(activity!!,R.color.green))
If nothing works than use AppCompatCheckBox with app:buttonCompat="your_drawable_selector"
This is working with png.

Android - Highlighting of buttons

I have a few buttons which I want to highlight only at the borders.
That is, I want the borders of the buttons to glow with a specific color on some action taken. How do I change the border programatically?
Is it possible with drawables? How?
You can have two drawables one for selected state and one for normal state, please go
through following link:
StateListDrawable
See here: http://developer.android.com/reference/android/widget/ImageButton.html
And also here: http://groups.google.com/group/android-developers/tree/browse_frm/thread/1906f24707594f67/17322a04f7af1a5b for Romain Guy's answer:
In res/drawable, create a file called for instance mybutton_background.xml
and put something like this 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/button_background_focus" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:drawable="#drawable/button_background_normal" />
</selector>
Then set this drawable as the background of your button with
android:background="#drawable/mybutton_background"

how to change font color in selected/focused ListView items?

I am struggling with this which apparently is a very simple effect but incredibly haven't found any intutitive way for doing it in Android.
I have a ListView and I managed to customize the background images so the selected item gets highlighted by getting a new background drawable. This I do creating a new style where I set the android:listSelector attribute to point a StateListDrawable where I have specified which drawables to use for every state.
However each ListView item is a LinearLayout where i have two TextViews. My goal is to be able to change the text color of these child views whenever the parent is selected or pressed, at the same time as the background drawable does. I know there is a ColorStateList but haven't been succesful setting that up.
Has anybody succeed getting something like this to work?
Thanks.
Neither of these are possible answers when your ListView is compromised of a layout that has multiple views. You need to set your child views to:
android:duplicateParentState="true"
Now you can use the methods others have described above to declare your TextViews' colors using a selector such as:
android:textColor="#drawable/my_row_selector"
and I'm sure you're aware, but the selector can be as simple as:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/white" />
<item android:color="#color/black" />
</selector>
As you can see, #color values are allowed. Hope this helps.
Also - android:state_pressed is used in conjunction with the AdapterView.OnItemClickListener.
in your textview propeties
android:textColor="#color/text_selector"
in res/color
text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="YOUR_CUSTOM_COLOR" />
<item android:state_selected="true" android:color="YOUR_CUSTOM_COLOR" />
<item android:color="YOUR_CUSTOM_COLOR" />
</selector>
In order to make it work on selection use the following code:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:state_activated="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
Apparently the key is state_activated="true" state.
When you are deploying the app for Android 11+ (HoneyComb+), you should use
android:state_activated="true"
for selected list state.
For the earlier versions use the combination of:
android:state_checked="true"
android:state_activated="true"
Of course don't forget to include the
android:duplicateParentState="true"
so the view can get the activated/checked state from a parent list view item
Also you may create a res/color folder and add a file "text_selector.xml" with the following lines:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:color="#f0f"/>
<item android:state_pressed="true" android:color="#f0f"/>
<item android:color="#000"/>
</selector>
After that assign in TextView:
android:textColor="#color/text_selector"

Categories

Resources