I need to customize my android spinner. For this I created this spinner_selector.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spinner_default" />
<item
android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/spinner_default" />
<item
android:state_pressed="true"
android:drawable="#drawable/spinner_pressed" />
<item
android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/spinner_focused" />
<item
android:state_enabled="true"
android:drawable="#drawable/spinner_default" />
<item
android:state_focused="true"
android:drawable="#drawable/spinner_focused" />
<item
android:drawable="#drawable/spinner_default" />
</selector>
and then set the background of spinner : android:background="#drawable/spinner_selector"
The problem is that the spinner looks really horrible. Before :
After :
and when it is pressed, it looks like this :
The image that I use is this :
How to solve this? I have this problem for Spinner, Button and EditText too. I really need to customize these components. Please help. Any idea is welcome.
To align text to right a bit, you can use padding in your XML please give Padding to the Spinner like this for ex: android:padding=5dp
The problem seems is the height and width of the Spinner, your need to change it,
either you can give any static value or use wrap content with setting image size bigger.
Related
I am creating a application that has couple of edittext's that chang color when the edittext has focus or not. When the view has the focus the color should be blue, when normal it should have white color, something like this:
I've tried creating two nine patches and setting the background according to this xml but seems its not working. Can anyone tell me how can I achieve this ?
Xml selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_focused="true"
android:drawable="#drawable/spelling_blue" />
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/spelling_white" />
<item android:drawable="#drawable/spelling_white" />
</selector>
Thanks
I don't think you need to use android:state_window_focused.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/spelling_blue" /> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="#drawable/spelling_blue" /> <!-- focused -->
<item
android:drawable="#drawable/spelling_white" /> <!-- default -->
</selector>
I have a State List Drawable xml
<item android:state_pressed="true" android:drawable="#drawable/list_selector_pressed" />
<item android:state_enabled="false" android:drawable="#drawable/list_selector_disabled" />
<item android:drawable="#android:color/transparent" />
Whenever I select the row in the listview I want it to show my pressed image and then when it releases it shows the transparent background (normal). However, it always shows my disabled image after releasing. I need to have the disabled image whenever I disable a row (grey). Any ideas what i'm doing wrong? Also, is there a way to capture the different states (focused, pressed, enabled) to have a better idea what is happening behind the scenes, this might help me in understanding what is happening?
This example should be a good help.
Here's how XML should look like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/listitem_pressed" />
<item android:state_focused="true" android:drawable="#drawable/listitem_selected" />
</selector>
I want to change all EditText UI In android. For example I want to change On Focus, On Blur and ... style. How can I do it? Actually I read some snippet, but I don't understand what they do!
1- create selector for different state
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="#drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="#drawable/textfield_pressed" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="#drawable/textfield_selected" />
<item android:state_enabled="true" android:drawable="#drawable/textfield_default" />
<item android:state_focused="true" android:drawable="#drawable/textfield_disabled_selected" />
<item android:drawable="#drawable/textfield_disabled" />
</selector>
2- To apply the customize drawable xml use the android:backgroung attibute of the EditText
android:background="#drawable/yourXml"
http://www.androidworks.com/changing-the-android-edittext-ui-widget
If you want to change the looks of EditText when certain events like onFocus, onPressed too place, then you will need Selector.
Selector is a drawable and is implemented as xml file, the elements in selector files will refer to specific state. Each Element maps to a specific state.
To change the look and feel of UI components, you can use background, src attributes,etc.
and this link is just the one thats needed.
http://developer.android.com/guide/topics/ui/themes.html
I've the following problem: I made a custom state selector for Android Buttons. They work like expected. The problem begins when I want the same states to apply to a ListView item row.
So I set the style in the Adapter and now I discover that the same state looks different on the listbox view item. It is more of a blend between the default style and the color I want to set. When I long press the item or release the item, I notice that it fades away first into the default color (blue) and then transparent.
I guess I do miss some state/state combination, but I can't figure out which. I tried a lot, also examples from StackOverflow, but nothing works. Hopefully someone here had the same challenge and has a correct override. Below is my drawable selector.
Thanks in advance!
XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime"
android:enterFadeDuration="#android:integer/config_shortAnimTime"
>
<item android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#android:color/transparent" />
<item android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#android:color/transparent" />
<item android:state_window_focused="false"
android:state_focused="true"
android:drawable="#android:color/transparent" />
<item android:state_pressed="true"
android:drawable="#android:color/holo_green_light" />
<item android:state_focused="true"
android:state_pressed="false"
android:state_enabled="true"
android:drawable="#android:color/holo_green_light" />
<item android:drawable="#android:color/transparent" />
</selector>
In touch mode there is no such thing as selected or focused when you are not pressing on an item.
But you can work around it. You need to set choice mode for the list (single or multiple, the default is.none) and then use state_activated in your selector.
I have an issue with my ImageButton not changing state. When I click, or rather touch, the button it stays as the same image. Here is the XML I am using as a selector.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/pushed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:drawable="#drawable/default" />
</selector>
I call this selector from my main.xml as
android:background="#drawable/imagechoice"
imagechoice.xml is the file with the selector
I don't understand why this is not working, unless I have to have some java code, but everything I've seen said this should work.
When using an ImageButton, isn't it the 'src' property you should use and not background?
Make sure that you copy the same images and the button XML into every "drawable" folders (hdpi,ldpi,mdpi).
That's how I solved this problem on my app.
Good luck :)
I have nearly the same XML and it works just fine. Are you sure you're not replacing the drawable in code somewhere?
On another note, your XML can be simplified by using the cascading nature of the state matching.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/pushed"
/>
<item android:state_focused="true"
android:drawable="#drawable/pushed"
/>
<item android:drawable="#drawable/default"
/>
</selector>
This is my xml of a button with my own custom image on it and it works great:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/btn_off" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:drawable="#drawable/btn_off" />
</selector>
Make sure you set Image button background as mentioned below.I think you are not setting the selector as background instead you are setting the image as background.
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_up_selector"
android:text="1"
android:textColor="#fffafa"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"/>