Android: Custom radiobuttons, is it possible to layer images? - android

I'm making some Custom Android RadioButtons following this tutorial:
http://blog.devminded.com/posts/custom-android-radiobutton
However.. In my project every button is different meaning I have to make very four almost identical images for every button I have.
Is there any way of having a single file for the state_pressed and focus and using a single file for the unique parts?
The code in question is this:
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/red_focused" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/red_unfocused" />
<item android:state_checked="false"
android:drawable="#drawable/red_img" />
<item android:state_checked="true"
android:drawable="#drawable/red_checked" />
Thanks for any help!

Related

Change EditText UI In Android

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

Inflated listview item don't appear as pressed

Sorry if this is too trivial or if it had been previously posted. Right now, I'm with this issue. I have a ListView component where I'm creating a set of items, each one being inflated layouts. When I click in one item, the behavior onClick is full functional and the listeners are doing their job, but the ListView don't show the "pressed status" in the buttons. The below image shows what I'm seeking for:
Does anyone have a hint to give me? I already checked and tryed a couple of things:
1. android:state_focused="true"
2. android:state_pressed="true"
3. setClickable(true)
Thanks in advance!
You have to manually apply styles to the states of the list item. Because, you are creating your own list item view instead of android list item view. so, you have to provide specific styles to show the different states.
Use selector to make the style to the list item. some sample code i am provided here,
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:drawable="#drawable/list_item_gradient" />
<!-- 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>
And in the listview
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:listSelector="#drawable/list_selector_background" />

Android Listview Listitem state color

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.

How to reference the system select button image for custom preference in Android

I have a customer preference widget (xml) which basically contains a TextView and next to that a right aligned ImageButton.
What I am trying to do is set the image for my image button to whatever is used as default by the preferences system. For example see the attached image where I've highlighted the type of image I want to use.
I've tried looking online and saving the ic_btn_round image into my drawables and using that, but it just doesn't look right.
Any advice welcome, thank you
This circle button comes from DialogPreference. If we look into how this preference is implemented, we'll find, that it uses widget described in this xml file:
<!-- Layout used by DialogPreference widgets. This is inflated inside
android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dip"
android:layout_gravity="center_vertical"
android:background="#drawable/btn_circle"
android:src="#drawable/ic_btn_round_more" />
So you need to look into 2 more files: drawable/btn_circle and drawable/ic_btn_round_more.
btn_cicle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_circle_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_circle_disable" />
<item android:state_pressed="true" android:state_enabled="false"
android:drawable="#drawable/btn_circle_disable" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_circle_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_circle_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_circle_normal" />
<item android:state_focused="true"
android:drawable="#drawable/btn_circle_disable_focused" />
<item
android:drawable="#drawable/btn_circle_disable" />
</selector>
ic_btn_round_more.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/ic_btn_round_more_disabled" />
<item
android:drawable="#drawable/ic_btn_round_more_normal" />
</selector>
All drawables referenced from these two xml files are actual *.png files.
So basically you need to use two images to achieve desired effect. But all these resources are internal to Android, and you can't use them directly. The easiest way is to just copy them to your project (as you did with ic_btn_round ).

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