Change color of button on toggle - android

I have an image button (star) which is used to mark something as favorite. I envision that when the user clicks on the star, the star will turn yellow. When they click on an already yellow start, it will go back to normal.
Transition from one color to the other would make a call to the server. I am doing that part already.
To change the color on click I did this.
<?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="true"
android:drawable="#drawable/ic_action_fav" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:drawable="#drawable/ic_action_ic_action_star" />
</selector>
However, this changes the color only for the time being when the buttons is clicked. It doesn't remain changed on the click, in other words, it doesn't toggle.
How can I toggle the color of a button on each click?

Use android:state_selected in the state list along with View.setSelected(boolean selected) in your Java code.

Related

"Block" the pressed state of a button if it's not clickable in Kotlin, Android

My button has 3 states and the representation of each depends on a particular condition. (You can follow this question I've asked earlier to see the screenshots of what states my button has):
Different background for a button's states in Kotlin
I want my button to kind of "block" its pressed state if it is not clickable.
My button's states:
<?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/background_stroke_bluish_rounded_corners_2dp"/>
<item android:state_pressed="false"
android:drawable="#drawable/background_bluish_rounded_corners" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/background_dark_teal_rounded_corners" /> <!-- focused -->
<item android:state_enabled="true" android:drawable="#drawable/background_bluish_rounded_corners"/>
</selector>
How I tried to set the isClickable to false:
updateFragmentView?.mFragmentRootView?.btnUpdate?.isClickable = false
Right now it works and the needed event while isClickable = true doesn't happen, but what I want to implement is not to let the pressed state become visible if the button is not clickable. Is that possible to implement without changing the XML?
Something alike can be found in isUserInteractionEnabled for iOS. If set to false, the button's highlighted color doesn't work (if the button has one).
UPD: I've tried setEnabled(false) and also .isEnabled = true, but that does influence on the logic of my fragment, so that's not working for me.

How do I imitate the behaviour of a pressed button in Android?

I'm trying to make a custom button, that behaves just like a 'pressed button' in Android with my own background images.
Pressing changes my button's image (color is darker), but I can't seem to get the focussed / focussed&pressed states right.
When you release your finger on the default button there is a circle filling the whole button but it won't happen with mine.
That's my code:
<?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="#drawable/fertig_focussed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/fertig_focussed_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/fertig_pressed"/>
<item android:drawable="#drawable/fertig_regular" />
</selector>
Thanks in advance!
That's because android check stats in the order and get the one matching first.
You need put "pressed" first, focus pressed second, then focused.

listview selection color in android

in my listview, when user long press on a item, i draw a custom actionbar and provide user option to delete multiple items at a time.
by default if i perform long press action, i will get selection color as blue and it gets disappear.
To overcome i tried adding a selector like this.
listviewselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/BLUE" />
</selector>
If i set this selector, when user performs long press i can see red color, but after wards if user performs selection, no color is getting retained on item. By default it looks white.
I tried setting background color based on condition like below
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(REDCOLOR);
}
else
{
convertView.setBackgroundColor(WHITE);
}
In this case, if user performs long press and then if user select multiple items i could see red color and by default all the items color will be white. But if user touches any item the default color will be nothing i.e no color appears on selection.
How to get default white color, on tap blue color and upon multiple selection red color?
i tried like this
New selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/WHITE" />
</selector>
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.RED));
}
else
{
convertView.setBackgroundColor(R.drawable.listviewselector);
}
In this i get by default all items blue color. why?
i tried like this, it worked.
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.BLUE));
}
else
{
convertView.setBackgroundColor(android.R.drawable.list_selector_background);
}

How to enable the "pressed" look on LinearLayout

I have the following UI presented when my ListView is empty:
Now, I want that when the user will press this "New Reminder" layout, it'll change to a "highlighted" state (with the blue focus background in ICS and the yellow color in GB)
The layout is clickable and the onClick method is called, but there is no indication for the user while he press his finger down.
I tried setting focusable to true, but it didn't do the trick.
What can I do to give any view the default "pressed" effect?
Thank you!
With your layout, you can set background with drawable like below.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#android:drawable/star_big_on" />
<item android:state_pressed="true" android:drawable="#android:drawable/star_big_on" />
<item android:drawable="#android:drawable/star_big_off" />
</selector>
Draw-able like below
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/left_radio_selected"/>
<item android:state_enabled="false" android:drawable="#drawable/left_radio_inactive"/>
<item android:state_enabled="true" android:state_selected="true" android:drawable="#drawable/left_radio_selected"/>
<item android:drawable="#drawable/left_radio_active"/>
and set your linerlayout.setSelected(true);
I don't know if I understand you correctly. If you want to create pressed state for this layout, you have to prepare state drawable xml (more info). Up there you can set what should your layout looks like while in pressed state.
I'm not sure but your question seems a little vague. From my understanding what you want might be suggested in this post Force a ListView item to stay "pressed" after being clicked?

how to change button color when it is clicked in android

By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background resource :
<?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" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you

Categories

Resources