I have a custom button in my drawable folder.
I want to use it multiple times in my XML file, with different colors.
Is there a way to use the same custom_button but with different color?
Firstly create a selector xml file .Selector will let you change button image on specific states like focused or pressed .
I am giving you the sample code for selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/ok_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/ok" />
</selector>
Now using StateListDrawable you can assign a number of graphic images to a single Drawable and swap out the visible item by a string ID value.
StateListDrawable state_up = new StateListDrawable();
state_up.addState(new int[] {android.R.attr.state_pressed},getResources().getDrawable(R.drawable.btn_up_cyan));
state_up.addState(new int[] {android.R.attr.state_focused},getResources().getDrawable(R.drawable.btn_up_cyan));
b1.setBackgroundDrawable(state_up);
Just use different color images as background and use the above code when you wish to change the background
Related
I need to get the id of the drawable I assigned to an ImageView by xml.
I have different buttons that need to change look when pressed, so I wanted to write just one method for all of them putting into 2 arrays the NOT_TOUCHED drawable ids and the TOUCHED drawable ids and so on...
How can I do this?
If you want to change the image view to different state when image view touch or not touch, you better use drawable selector resource. Create a drawable/background.xml like following, then set as background of your image view.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/focused_image" android:state_focused="true"></item>
<item android:drawable="#drawable/selected_image" android:state_selected="true"></item>
<item android:drawable="#drawable/normal_image"></item>
</selector>
I have some TextViews which are dinamically added to a LinearLayout. These TextViews are clickable and have an onLongClickListener (I also intend to add onClickListener later)
Here is the thing, I want these TextView to change their background color when pressed and I read that you can use selectors to do such thing.
So I made this xml file in res/drawable/text_view_pressed.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="#000000"/>
<item android:state_pressed="false"
android:color="#FFFFFF"/>
</selector>
I tried to create a TextView and using this xml file likes this:
TextView t = new TextView(this);
t.setBackgroundColor(R.drawable.text_view_pressed);
But when I do this, it will give this error in t.setBackgroundColor: "Should pass resolved color instead of resource id here: getResources().getColor(R.color.text_view_pressed)" but it doesn't work as intended if I use getResources().getColor(R.color.text_view_pressed).
Anyone got an idea how to do it?
You are on the right track. However there is an important detail.
There are two types of resources that can be affected by states: ColorStateList and StateListDrawable.
A color state list can only be used in certain contexts, for example in TextView.setTextColor(). As far as I can see, you cannot use a color state list as parameter of setBackgroundColor() if you want to change the background of a View when it's pressed. You need a state list drawable for that. And in a state list drawable, the android:drawable attribute is mandatory.
So, to sum up:
The xml file should be placed in res\drawable,
Its structure should be slightly different (i.e. state list, not color list), and
You need to use setBackgroundResource() instead of setBackgroundColor().
Example file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#android:color/white" />
<item android:drawable="#android:color/black"/>
</selector>
If you want to use custom colors instead of white and black, you simply need to define them as resources in res\values and reference them from here.
I have already been using selector drawables to make my button change background according to the state.
However, I also want to change the text color and left compound drawable together with the background. But the default selector XML atrribute does not contain any "android:textColor" or "android:drawableLeft" to be assigned.
I know I can always achieve this with extend my own button class, but is there any clean way out?
I am not very sure about drawables but for changing textcolor depending upon button state, I use selectors as below,
<?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/color_light_green"></item>
<item android:color="#fff"></item>
//state you want
</selector>
and then apply it to textColor attribute in the xml as,
android:textColor="#drawable/selector_btn_text_color"
Eclipse doesn't auto suggest color attribute in selector but we can do it. :)
I have a list view with items, colored white background, I'd like the user interaction to be more clear when user clicks on item, make the background color different than white.
How do I implement this with code (no xml)?
You need to create a selector xml file in your "drawable" folder and use it as the background of your items, for example:
<?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/itemPressedColor" />
<item
android:state_pressed="false"
android:drawable="#drawable/itemNormalColor" />
</selector>
Where itemPressedColor and itemNormalColor would be drawables defined in the same folder
Use State List for xml or State list Drawable in code
You can use a selector drawable, something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/red"
android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
And then set this drawable as the background of your list item.
To do the same in code, you need to implement a custom adapter and in its getView() method use code like this:
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[] { android.R.attr.state_pressed }, getResources().getDrawable(R.color.red));
selector.addState(new int[] {}, getResources().getDrawable(R.color.white));
...
View item = ...
item.setBackgroundDrawable(selector);
I currently have a keypad in my application 0 - 9, I require an on and off state for each button.
To do this I've used a StateList as follows:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="#drawable/dialpad_1_off" />
<item android:state_pressed="true"
android:drawable="#drawable/dialpad_1_on" />
</selector>
However this is only for one button, each button has a different on and off graphic, dialpad_2_off, dialpad_3_on etc...
So do I have to create a Statelist for every single button or is there a way to do it within one Statelist XML file?
You could make the background of the image change state and use that common background for all of the buttons. Then you could use either text or an image as the button foreground.