Android button states programmatically in Java (not XML) - android

How does one define Android button image for the "state_pressed"
"android:state_focused" in Java?
For example, how would one accomplish the equivalent in Java for the XML from
http://developer.android.com/reference/android/widget/ImageButton.html
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>

Just use addState method of StateListDrawable
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.phone));
You can use constants below for the first parameter of this method
android.R.attr.state_accelerated
android.R.attr.state_activated
android.R.attr.state_active
android.R.attr.state_drag_can_accept
android.R.attr.state_drag_hovered
android.R.attr.state_enabled
android.R.attr.state_first
android.R.attr.state_focused
android.R.attr.state_hovered
android.R.attr.state_last
android.R.attr.state_middle
android.R.attr.state_pressed
android.R.attr.state_selected
android.R.attr.state_single
android.R.attr.state_window_focused

Create an instance of StateListDrawable and then assign it with imagebutton.setImageDrawable(stateDrawable).

10x to Tang Ke, I`m using this for different list item color whit selection color.
selected state
stateListDrawable.addState(new int[] {android.R.attr.state_pressed},
new ColorDrawable(getResources().getColor(R.color.alpha_blue500)));
default state
stateListDrawable.addState(new int[] {},
new ColorDrawable(getResources().getColor(R.color.red)));
Here you can change color for different state of the row item (ex. paid vs free)
set state to custom layout row item in list adapter
holder.relativeLayout.setBackgroundDrawable(stateListDrawable);

Related

Android set android:state_checked="false" in code

I want to create a selector using the code below:
<?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/sh_radio_icon_checked" />
<item android:state_checked="false" android:drawable="#drawable/sh_radio_icon_unchecked" />
</selector>
So far I have managed to add the first item, like this:
StateListDrawable drawable = new StateListDrawable();
int[] sFocusedSelected = {android.R.attr.state_checked};
Drawable dFocusedSelected = getResources().getDrawable(R.drawable.sh_radio_icon_checked);
drawable.addState(sFocusedSelected, dFocusedSelected);
But do I add the state_checked=false since there's no state_unchecked constant?
Observe "-" (Minus/Hyphen) in the beginning of "android.R.attr.state_checked"
sld.addState(new int[] {-android.R.attr.state_checked }, greyD);
Android : How to update the selector(StateListDrawable) programmatically

List view item click color indication programatically

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);

Changing colors to custom button

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

Change parent's background color when its child is clicked

I have a TableRow which contains a LinearLayout, and then this LinearLayout contains a TextView. What I want is, once the TextView is clicked, the entire TableRow would change its background color.
I've tried to use getParent() and performClick() to pass the click event from TextView to TableRow. The onClick() method of TableRow does get called, but its background color does not change.
Of course I've set the selector by using either
row.setBackgroundResource(R.drawable.menu_item_bgcolor);
or
row.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.menu_item_bgcolor));
Doesn't work. Can anyone provide any insight into this? Thanks,
Below is the selector xml 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="#drawable/menu_item_pressed" />
<item android:state_focused="true" android:drawable="#drawable/menu_item_pressed" />
<item android:drawable="#drawable/menu_item_normal" />
</selector>
try this
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
row.setImageDrawable(states);

Android : How to update the selector(StateListDrawable) programmatically

I want to update the selector for a button programmatically.
I can do this with the xml file which is given below
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/btn_off" />
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/btn_off" />
<item android:state_focused="true"
android:state_enabled="true"
android:drawable="#drawable/btn_on" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_on" />
</selector>
I want to do the same thing programmatically. I have tried something like given below
private StateListDrawable setImageButtonState(int index)
{
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.stateNotNeeded},R.drawable.btn_off);
states.addState(new int[] {android.R.attr.state_pressed, android.R.attr.state_enabled},R.drawable.btn_off);
states.addState(new int[] {android.R.attr.state_focused, android.R.attr.state_enabled},R.drawable.btn_on);
states.addState(new int[] {android.R.attr.state_enabled},R.drawable.btn_on);
return states;
}
but it didnt work.
And how to set android:state_enabled="false" or android:state_enabled="true" programatically.
You need to use the negative value of the needed state.
E.g.:
states.addState(new int[] {-android.R.attr.state_enabled},R.drawable.btn_disabled);
Notice the "-" sign before android.R.attr.state_enabled.
Very late response here but in case anyone else is having problems setting a StateListDrawable programmatically. Then as with the XML files the order in which you set the states into the StateListDrawable is important.
For example this will work as expected:
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
This won't:
StateListDrawable sld = new StateListDrawable();
sld.addState(new int[] {}, new ColorDrawable(Color.GREEN));
sld.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.GRAY));
I am going to answer your question "How to update the selector for a BUTTON programmatically?" by proposing to switch a button for a LinearLayout with embedded ImageView and TextView. There are a number of benefits to doing this, especially if you will later decide to customize your views. There is no loss of functionality resulting from this switch. You will still be able to attach same event listeners you can attach to a button, but will be able to avoid the buttons/tabs styling nightmares. Here is a relevant code from the layout.xml
<LinearLayout
android:id="#+id/button"
style="#style/ButtonStyle">
<ImageView
android:id="#+id/background"
android:src="#drawable/custom_image"/>
<TextView
style="#style/TextStyle"
android:text="Custom Button"
android:id="#+id/text"/>
</LinearLayout>
Next, I have a selector file called custom_image.xml located in the drawable folder. Here is the content of the selector file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/disabled_img" android:state_enabled="false" />
<item android:drawable="#drawable/unselected_img" android:state_selected="false" />
<item android:drawable="#drawable/selected_img" android:state_selected="true" />
</selector>
The three source image files (disabled_img.png, unselected_img.png, selected_img.png) are also located in the drawable folder.
Now back to your Java code. There is no need for the funky StateListDrawable garbage for many reasons. First, it just looks ugly, and is hard to maintain. But most importantly it goes against the principles of keeping your logic separate from your presentation. If you are managing your drawable resources in Java code, you know you are doing something fundametally wrong.
Here is what I am proposing instead. Whenever you want your button to be selected, you just pop this one-liner in there:
((LinearLayout)findViewById(R.id.button)).setSelected(true);
Or whenever you want the button to be in the disabled state, here is another one-liner:
((ImageView)findViewById(R.id.background)).setEnabled(false);
Please notice that in this last example I am specifying the disabled state on the ImageView inside the LinearLayout. For some reason whenever you change the enabled / disabled state of the LinearLayout, the selector is not being triggered. It works fine when you do it on the ImageView instead.
Not enough rep to comment but the accepted answer did not work for me.
My goal was for designers to set enabled, disabled, and pressed background colors on some button. I intended for them to use it to test colors on different displays.
I noticed some other people mentioned it did not work for them either.
The states that need to be defined are
not pressed and enabled, this is what you see when the button is enabled and not pressed.
pressed and enabled, this is what you see when the button is pressed and enabled
not enabled. This is what you see when the button is disabled
Here is some code that will give you an idea of the states. I am using Color.Parse() to generate ints for the colors then I pass them to this method to get the StateListDrawable.
private StateListDrawable createDrawable(int enabled, int pressed, int disabled) {
StateListDrawable stateListDrawable = new StateListDrawable();
stateListDrawable.addState(new int[] { -android.R.attr.state_pressed, android.R.attr.state_enabled }, new ColorDrawable(enabled));
stateListDrawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, new ColorDrawable(pressed));
stateListDrawable.addState(new int[] { -android.R.attr.state_enabled }, new ColorDrawable(disabled));
return stateListDrawable;
}
I don't know how you are adding the StateListDrawable, since the code is not here. But be sure to be check the documentation and the adding the setState().
You can set the properties from the View, such as yourView.setEnabled(true)
I hope that helps

Categories

Resources