state_pressed remains true while onClick is running - java

I want to set a different background color for a button only while the button is being touched by the user.
But that color remains that way during the whole time onClick for that button is running even though I am no longer touching the button
Please help me find a solution to this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#777777" />
<item android:color="#android:color/black" />
</selector>

Assuming your on-click method takes a long time, You should add a second item for stating the case of the button not being pressed:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#777777" />
<item android:state_pressed="false" android:color="#000000"/>
<item android:color="#android:color/black" />
</selector>

Related

Select item from xml to apply it on a button

Here's what I have :
Buttonshape.xml :
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF008AB8"/>
<stroke android:color="#0299D0"/>
<corners android:radius="15dp"/>
</shape>
</item>
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape"
/>
Everything is working greate, my button corner is rounded and it apply the drawable, but I have a question :
Is it possible to add more items in my buttonshape.xml and select a specific drawable in my BlueFragment.xml
For example doing something like this :
Buttonshape.xml :
...
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
<item android:drawable="#drawable/b_2"></item></layer-list>
and in my BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape|b_popcorn"
/>
<Button
android:id="#+id/BtePlay2" ...
android:background="#drawable/Buttonshape|b_2"
/>
Something like this to set only one drawable for each button instead of making 50+ xml files for each buttons ...
Thanks
we have only option that is using selector, but its only depend on view state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_checked" android:state_checked="true"/>
<item android:drawable="#drawable/bg_unchecked" android:state_checked="false"/>
</selector>
so I think we can not do any thing like you expected, so sorry for that !

Multiple Toggle Buttons with their own on/off state icon

I have a couple of ToggleButtons inside a HorizontalScrollView, I want each of these ToggleButtons to have a different on and off drawable
ToggleButton:
<ToggleButton
android:id="#+id/toggle_[nameOfToggle]"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/toggle_selector"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
I have a selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- currently pressed turning the toggle on -->
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/toggle_state_on"/>
<!-- currently pressed turning the toggle off-->
<item android:state_pressed="true"
android:drawable="#drawable/toggle_state_off"/>
<!-- not pressed default checked state -->
<item android:state_checked="true" />
<!-- Default non-pressed non-checked -->
<item />
</selector>
toggle_state_on.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_on_[nameOfToggle]"/>
</layer-list>
What is the best way to programmatically achieve this, is there a way to check what toggle button it is and change the drawables of the on/off state based on that?

Scale with button press using selector

so my selector works all good and well but I want the button to change size when press as well. Is there a way to change the android:layout_height inside the selector?
My current selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="false" >
<item
android:state_pressed="false"
android:drawable="#drawable/grey_button08"
/>
<item
android:state_pressed="true"
android:drawable="#drawable/grey_button09"
/>
</selector>
Look at this post.
You can set your background as a shape and set size attrs to it

Custom button drawable state_pressed and state_checked not working on CheckBox or RadioButton - Android

I am setting custom drawable to the checkbox button,
But only android:state_checked="true" and android:state_checked="false" seem to work.
Other states aren't working.
I am unable to set a custom drawable on pressed state.
This is the selector I am using:
<?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/radio_btn_selected" />
<item android:state_checked="false" android:drawable="#drawable/radio_btn_normal" />
<item android:state_pressed="true" android:drawable="#drawable/radio_btn_pressed" />
<item android:state_focused="true" android:drawable="#drawable/radio_btn_pressed" />
<item android:drawable="#drawable/radio_btn_normal"/> <!-- default -->
</selector>
This is how I am setting it to radio button:
radioButton.setButtonDrawable(R.drawable.radio_btn_selectors);
android:state_pressed="true" and android:state_focused="true" are not working because of the ordering in which you declared various states. Whenever you declare a drawable like this, system will goes from top to bottom and if a state is matched, it doesn't look for the other ones and applies the changes based on the very first selection. I think here android:state_checked="false" condition might be executing instead of android:state_pressed="true" and android:state_focused="true". So move android:state_checked="false" to the bottom and then try.

How to change button background colour without changing onclick/onpress colors

I'm wondering if this is even possible but hopefully someone will be able to confirm.
I've created a simple custom button layout in XML to handle the focused/pressed and dormant states. See code at bottom. This works fine when I use it to create a new button. However, I would like the user to be able to change the button colour via a colour picker if they don't like the default. However, the only way I know to change the button background colour programmatically is to use
mybutton.setBackgroundColor(someothercolor);
but if I do this it overwrites all the XML layout code and I lose the colour change when the button is pressed. I guess this is by design as I'm essentially overwriting the entire background style but what I really want to do is to allow the user to change the button colour when its not pressed to something custom but keep the style and layout of the other states the button could be in (i.e. what happens when its pressed).
Any ideas anyone?
Thank you in advance.
Nat
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/originalbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
Maybe you can consider creating a ColorStateList programmatically, as described here: How do I create ColorStateList programmatically?
You can try this:
1. remove the default color <item android:drawable="#color/originalbuttoncolor" />
2.Then:
`StateListDrawable ret = (StateListDrawable) res.getDrawable(R.drawable.btn_selector);
ret.addState(new int[] {}, new ColorDrawable(your_desire_color));
mybutton.setBackgroundDrawable(ret);`
You can make multiple files of your selector-list, each one contain different color as the default color, and link those files to the color picker, so you save your logic of selector.
For example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/originalbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
And:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#color/yellowbuttoncolor" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#color/someotherbuttoncolor" />
<item android:drawable="#color/originalbuttoncolor" />
</selector>
Edit: if you want to take color from user, this may work, if the selector state will overrided by this code:
ColorDrawable cd = new ColorDrawable(); // initialize it from the color picker;
StateListDrawable states = (StateListDrawable) mybutton.getBackground();
states.addState(new int[] {-android.R.attr.state_pressed, android.R.attr.state_focused}, cd); // the minus means false value
mybutton.setBackground(states);

Categories