Using colors in a selector xml - android

I want to set colors for clicking instead of images. Doing so doesn't give any errors except for force-closing at runtime. Is it even possible to do this or can selector only be used for images?
Code I'm trying below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:background="#00C0FF" />
<item android:state_focused="true" android:state_pressed="false" android:background="#0060FF" />
<item android:state_focused="false" android:state_pressed="true" android:background="#00C0FF" />
<item android:state_focused="false" android:state_pressed="false" android:background="#FFFFFF" />
</selector>

Not per the documentation
<item>
Defines a drawable to use during certain states, as described by its attributes. Must be a child of a <selector> element.
android:drawable
Drawable resource. **Required.** Reference to a drawable resource.
You can create a simple shape drawable to hold the color
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33FF33"/>
</shape>

Is it even possible to do this or can selector only be used for images?
No, this doesn't make any sense (to me at least). Without a 'drawable', how will it know exactly 'what' to set to state_focused and state_pressed?
Why not just create some single colour drawables (bitmaps for example) and provide those as the drawables for each item?

Related

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"

using standard Android button but with modifications

I'm trying to use the the standard android button in my layout, but make the background transparent when not pressed, or making text left or right justified, or whatever custom modification I want. Is there some simple way I'm missing to inherit from the standard button but change a few properties? I've looked at the two posts below, and can't get them to work and am too new to leave comments on those pages, plus both of those solutions have problems anyway:
I've tried copying #android:drawable/btn_default source per How to disable the default Button color changing property on onClick, but all of the resources linked to from there are private. I tried to find the source for those private files, but some of them i can't find even if i go into the android sdk folder to get the raw files. Where can i find these files, if this is the way to edit the standard button? copying those private files is definately not ideal though, if the standard selected/pressed/whatever button changes in another api i'll still be using the old ones in this case and have inconsistent buttons...
Also, I've seen Standard Android Button with a different color which is good for making custom buttons in general, but how do I set it to be exactly like the standard button? i.e. what are the colors, are the gradients right, etc. Again, this has the problem that if standard button changes i'll still be using old values.
Use styles on your buttons:
<Button id= ... style="#style/myButton" />
values/styles.xml:
<style name="myButton" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/myBackground</item>
</style>
To deal with various button states (e.g. pressed, etc) you'll need a selector drawable resource, example drawable/myBackground.xml:
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states
-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/button_unfocused" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/button_unfocused" />
<!-- Focused states
-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/button_focus" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/button_focus" />
<!-- Pressed
-->
<item android:state_pressed="true" android:drawable="#drawable/button_press" />
</selector>
The drawable/button_press.xml could specify a gradient, shape, borders, etc, as you need.
An example button_press.xml that does a background gradient, rounded corners, and border:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#FF404040" />
<corners android:radius="6dp" />
<gradient android:startColor="#FF9B00" android:centerColor="#FFB300" android:endColor="#FFCA00" android:angle="90" />
</shape>
The first part of CSmith's answer about using styles with parent="#android:style/Widget.Button" is the best way to change button properties like left or right justified text. However the rest of the answer describes how to create your own selectors, so I thought I'd add my solution to inherit selectors from the default button, and then just override specific ones you want to change:
just put android:drawable="#android:drawable/btn_default" for those selectors you want default behavior for, and then you can just specify custom fields for only those selectors you want to change. in my case of trying to have a button that inherits all the selectors but has a transparent background normally instead of the gray one, do the following (duplicating the first part of CSmith's answer here for clarity, thanks CSmith!):
Define a button using a custom style:
<Button id= ... style="#style/myButton" />
res/values/styles.xml:
<style name="myButton" parent="#android:style/Widget.Button">
<item name="android:background">#drawable/myBackground</item>
...other changes to default button...
</style>
then, to inherit all selectors and just overwrite the ones that show gray:
in res/drawable/myBackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="#drawable/transparent_btn" />
<item android:state_pressed="true"
android:drawable="#android:drawable/btn_default" />
<item android:state_focused="true"
android:drawable="#android:drawable/btn_default" />
<item android:drawable="#drawable/transparent_btn" />
</selector>
and, in res/drawable/transparent_btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#android:color/transparent" />
</shape>
NOTE: this example doesnt worry about state_enabled, if you want specific behavior for whether a button is enabled or not just overwrite those selectors as well.

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"

What is the default drawable for pressing a list item

When the user presses a ListView item (android:state_pressed="true") it flashes a shade of yellow (or you can press and hold).
What drawable is this? I've created my own selector because I want my own ListView item color , but I lose the pressed color.
There's an Android doc about skinning buttons that references #ffff0000, but this produces red.
Does anyone know what it is and how to reference it?
The default system resources can be found in <android-sdk>/platforms/android-<version>/data/res. In particular, the list selector is defined in drawable/list_selector_background.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false"
android:drawable="#color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>
The drawable that is shown on a press, list_selector_background_transition, is not a single color but two 9-patch images, a yellow and a white one, with an animated transition between them.
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:drawable/list_selector_background_pressed" />
<item android:drawable="#android:drawable/list_selector_background_longpress" />
</transition>
The thing your talking about is the Android OS built-in selector.
Make your own highlight with an xml-file in your drawable-folder like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#YOURCOLOR" />
<item android:color="#FE896F" />
</selector>
Then in your XML-file there you have your ListView.
android:textColor="#drawable/highlight" //For text to appear like YOURCOLOR
//or if you wish the background
android:background="#drawable/highlight" //For the background to appear like YOURCOLOR
I hope this is it and tell me if this worked or not!
A color is defined as #AARRGGBB where AA represents the alpha (transparency) value, RR the amount of red, GG the amount of green, and BB the amount of blue. Thus #ffff0000 is solid and all red. If you want orange, you want to add some green, i.e: #ffffA500. Google for RGB color values to see pages of colors with their rgb values.
I have used a color picker tool in a photo editing application to find the outer and inner colors of the gradient and made my own.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#ffb300"
android:centerColor="#ffc800"
android:endColor="#ffb300"
android:angle="270"/>
</shape>

Android - different image for rollover on ImageButton

Is it possible to specify a different image when the user's focus comes to an ImageButton? I want to display an image button on a LinearLayout and change the image when the user's focus comes on the button or when the user presses the button.
Thanks.
Yes, you can do this. What you need is a drawable xml file that defines a selector.
<selector xmlns:android...
<item android:state_enabled="false" android:state_focused="true" android:drawable="..." />
<item android:state_enabled="true" android:state_focused="false" android:drawable="..." />
</selector>
Then, use the id of this drawable XML when specifying the ImageButton in your layout XML.
The precedent answer did not work for me. Here is the code I found somewhere else:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/refresh_pushed" android:state_pressed="true" />
<item android:drawable="#drawable/refresh" />
</selector>
You can also add a state for foccussed objects by adding a line and using:
android:state_focused="true"

Categories

Resources