Change button background when pressed - android

I want to change background of a button when it is clicked. I tried to use a selector. But It didn't work. Here is the selector (add_grp_slctr.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp_d" />
<item android:drawable="#drawable/add_grp" />
</selector>
And the button :
<Button
android:id="#+id/addGrpBtn"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:background="#drawable/add_grp_slctr"
android:onClick="addGrpDialogOpen" />
add_grp_d and add_grp are images(png).

I tried a similar code which will be white by default, black when pressed on an onclick of a button:
//***This is the btn_selector which is to be declared in drawable folder***
<?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/black" /> <!-- pressed -->
<item android:drawable="#android:color/white" /> <!-- default -->
</selector>
and called this on the button.xml -->
android:background="#drawable/btn_selector"
Hope this would help..:)

go through the http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList once. Also state_focussed for button only works when you are focussing the button using a hardware-keyboard.
As for your case
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_selected="false" android:drawable="#drawable/button_default"/>
<item android:state_pressed="true" android:state_selected="false" android:drawable="#drawable/button_default" />
<item android:state_pressed="false" android:state_selected="true" android:drawable="#drawable/button_selected"/>
<item android:state_pressed="true" android:state_selected="true" android:drawable="#drawable/button_selected" />
<item android:drawable="#drawable/button_selected" />
</selector>

Use your selector
change your code like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp" />
<item android:drawable="#drawable/add_grp_d" />
</selector>
My Code
<item android:drawable="#drawable/shadow_design_click" android:state_pressed="true"/>
<item android:drawable="#drawable/shadow_design" android:state_focused="true"/>
<item android:drawable="#drawable/shadow_design"/>
</selector>

I think you should change your selector a bit.
Check this answer here.

Instead of passing cuastom XML file, if you just want to change the color of you button then you can try with the following way.
Button lineColorCode = (Button)findViewById(R.id.button1);
Now inside button's click event use following code.
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);

Related

Android ImageView set selector programatically dosen't work

temp_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="#color/color1" />
<item android:state_selected="true" android:color="#color/color2" />
<item android:color="#color/color3" />
</selector>
And I applied it through the code below
imageView.setImageResource(R.drawable.image)
ImageViewCompat.setImageTintList(imageView, ColorStateList.valueOf(R.color.temp_selector))
I don't know why it doesn't work... What should I do?
(It has to be applied programatically. not xml.)
Use below to change color
imageView.isSelected = true
imageView.isSelected = false
Use below selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/color3" android:state_selected="false" />
<item android:color="#color/color2" android:state_selected="true"/>
<item android:color="#color/color1" android:state_pressed="true" />
</selector>

Android Button background states with unexpected behaviour

I have a Button with a backround ressource that defines states and backgrounds
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_pressed="true" />
<item android:drawable="#color/gray" android:state_pressed="false" />
<item android:drawable="#color/white" android:state_selected="true" />
<item android:drawable="#color/gray" android:state_selected="false" />
</selector>
I also tried
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_pressed="true" android:state_selected="true" />
<item android:drawable="#color/gray" android:state_pressed="false" android:state_selected="false" />
</selector>
when pressed it does change colors, but when I apply btn.setSelected(true); nothing happens
any ideas ?
OK I got it,
for future people who will encounter this, this has solved it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_pressed="true" />
<item android:drawable="#color/white" android:state_selected="true" />
<item android:drawable="#color/tab_gray" />
</selector>
Try btn.performClick();, which clicks the button. This might work:
void pressButton(Button btn, boolean pressed) {
if (pressed != btn.isPressed()) {
btn.performClick();
}
}

selector disable botton

I have this selector for one of my button's background
<?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/bg_botton_disable"/>
<item android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/bg_botton_pressed"/> <!-- pressed -->
<item android:state_enabled="true"
android:drawable="#drawable/bg_botton" /> <!-- default -->
</selector>
(Button) bt = (Button) findviewById(R.id.MyBotton);
bt.setEnabled(false);
The code disables the functionality of my button. I mean when I press the button it doesn't launch its own onClick() method. That's fine, but the problem is that it doesn't change the background of the button to bg_botton_disable drawable.
Does anyone has any suggestion how to fix it?
Reorder your selector items
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:drawable="#drawable/bg_botton" /> <!-- default -->
<item android:state_pressed="true"
android:drawable="#drawable/bg_botton_pressed"/> <!-- pressed -->
<item android:state_enabled="false"
android:drawable="#drawable/bg_botton_disable"/>
</selector>

selector for button not working

I want to change the background of the button when the button is unclickable.I have used selector for that but it is not working in the case when the button remains unclickable.
This is the selector file:
<?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/button_lightgrey" />
<item android:state_pressed="true" android:state_enabled="true" android:drawable="#drawable/button_blue"/>
<item android:state_focused="true" android:drawable="#drawable/button_darkgreen" />
<item android:drawable="#drawable/button_lightgreen" />
</selector>
This is the button where i am using this selector file:
<Button
android:id="#+id/PrevButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="5.0dip"
android:layout_weight="1.0"
android:background="#drawable/ibtn"
android:onClick="onPrevButtonClick"
android:text="Prev" />
Please help me.All other functions are working in the selector file but only the unclickable button case is not working.Thanks
instead of button.setClickable(false) do button.setEnabled(false) and use the same selector xml.
this may helps you
i think in your selector file having issue try selector file like below
pls change images as you require
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img_btn_repeat_pressed"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/img_btn_repeat_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/img_btn_repeat_pressed" android:state_focused="true" />
<item android:drawable="#drawable/img_btn_repeat"
android:state_focused="false"
android:state_pressed="false" />
</selector>
Just modify your selectors with below selector.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="#android:color/transparent" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
</selector>
Hope this helps you.
Edit
As per your comments you can't use selector for button which is the state as false(button.setclickable(false);) In this state you can't use your selector

selector for listview doesn't work

I want to set the color of row in listview if selected to yellow and otherwise be white so I use the following selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="#color/encounterselector_color" />
<item
android:drawable="#color/encounter_normal" />
</selector>
where
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="encounterselector_color">#fbeda5</color>
<color name="encounter_normal">#ffffff</color>
</resources>
and I use it like the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/EncounterGrid"
android:background="#drawable/encounterlistview"
>
<!-- remaining code -->
but the row is always white , any idea how to fix that
Setting the background color with a selector is a bit tricky. Basically you have to create a drawable for each color and use them in your android:drawable attributes. You cannot directly use colors.
Check this related question for more details.
I use the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the switched off state -->
<item android:state_enabled="false"
android:drawable="#drawable/grey_bar" />
<!-- These are the partial states -->
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<!-- This is the switched on state -->
<item android:state_enabled="true"
android:drawable="#drawable/button_normal" />
</selector>
Where all of the drawables I point to are defined in xml, or are existing 9 patch images.
use android:color on your selector and not android:drawable because you are getting reference to the color resources, so your selector will be like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:color="#color/encounterselector_color" />
<item
android:color="#color/encounter_normal" />
</selector>
Nothing was working for me until I set drawSelectorOnTop = "true".
Everything worked after that.

Categories

Resources