Android Button background color when clicked - android

Can anyone help me with changing the background color of my button so that it can change color when clicked and change back to normal color after clicked. I am not interested in using images and have looked, everyone keep posting half the codes so I still don't understand fully. If possible, please provide a link where I can see step by step instructions, These half posted comments does not help me at all because I am totally new to android and Java.

You need to make special drawable for that with selector element.
<?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:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
and in the button
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
More info here

<item
android:state_window_focused="false"
android:drawable="#android:color/transparent" />
<item
android:state_pressed="true"
android:drawable="#drawable/item selected"/>
<item
android:state_focused="true"
android:drawable="#drawable/item selected"/>
<item
android:drawable="#android:color/transparent"/>
Put this xml in a drawable file and set the resource.

Related

Understanding of Android ripple animation

I want to create an animation like material ripple animation, so I decided to look at its source code.
The enter point is :
android:background="?android:attr/selectableItemBackground">
go to: Android\Sdk\platforms\android-30\data\res\values\attrs.xml
<!-- Background drawable for bordered standalone items that need focus/pressed states. -->
<attr name="selectableItemBackground" format="reference" />
Next we go into Android\Sdk\platforms\android-30\data\res\values\themes.xml
<item name="selectableItemBackground">#drawable/item_background</item>
Nothing special, just another drawable.
Next in: Android\Sdk\platforms\android-30\data\res\drawable\item_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true" android:drawable="#drawable/list_selector_background_focused" />
<item android:drawable="#color/transparent" />
</selector
We are close, in this file we saw drawables of different states. Transition file at:
Android\Sdk\platforms\android-30\data\res\drawable\list_selector_background_transition.xml is:
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:drawable/list_selector_background_pressed" />
<item android:drawable="#android:drawable/list_selector_background_longpress" />
</transition>
Finaly we have 4 files, in picture below
So the questions is:
How this 4 squares turns into this circular ripple animation?
That means this black pointers?
what the orange fill and green border do ?
(optional) Where is the magic part?

How to make selector keep the pressed state till clicked again

I use selector to create custom buttons with
<item android:drawable="#drawable/button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_bg_normal"></item>
Now I want that the button acts as a state button remaining in the pressed status till is clicked again to return unpressed.
If possible I want to avoid to achieve this setting status programmatically.
Is there a way to do this using selectors (or something similar)?
Can you use ToggleButton instead of Button?
layout.xml
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:textOff="done"
android:textOn="done"
android:background="#drawable/test_selector"/>
test_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- on checked -->
<item
android:state_checked="true"
android:state_pressed="false"
android:drawable="#drawable/button_bg_selected" />
<!-- normal -->
<item
android:state_checked="false"
android:state_pressed="false"
android:drawable="#drawable/button_bg_normal" />
<!-- pressed -->
<item
android:state_pressed="true"
android:drawable="#drawable/button_bg_pressed" />
</selector>

remove listitem blue selection

I added the following selector to my listview in my drawerlayout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item android:state_focused="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/colorPrimaryDark" /> <!-- pressed -->
<item android:drawable="#color/colorPrimary" /> <!-- default -->
</selector>
colorPrimaryDark and colorPrimary are just color values
This work amost fine. But if i press and release an listitem on API 19 or lower, the item gets blue. How is that possible?
If you are using AppCompat as a theme you should reference the colors from theme like this
<item android:drawable="?attr/colorPrimary"
I found the problem.
android:choiceMode="singleChoice"
This appeared to be the problem.
android:cacheColorHint="#00000000"
add this attribute for your listview

Disable button with button selector

I have a button selector that changes the button image when it is pressed. I have also set an image for when the button is disabled. I try and disable the button programmatically but the disabled button image is not appearing. Is my button_selector correct?
<item android:drawable="#drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
<item android:drawable="#drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="#drawable/red_btn_bg_pressed" android:state_focused="true"/> <!-- focused -->
<item android:drawable="#drawable/red_btn_bg"/> <!-- default -->
I am using mButton.setEnabled(false) in my code to disable the button
try this one and i uploaded one sample project for you for more help check the project
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_disable" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_normal"/>
</selector>
and use this selector in button as following
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="30dp"
android:background="#drawable/selector"
android:enabled="false"
android:text="Disable Button" />
sample code link
https://www.dropbox.com/s/lydkog10rkujbsa/ButtonSelector.rar
Try this.
<item android:drawable="#drawable/red_btn_bg_pressed" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="#drawable/red_btn_bg_disabled" android:state_enabled="false"/> <!-- disabled -->
<item android:drawable="#drawable/red_btn_bg"/> <!-- default -->

How to get selection appearance like listview for textview in android

I have a textview and some text is present inside of it. Now if i tap on textview, i want section appearance like listview. when i press on the text, it should show me selection appearance, how to do?
use selector Link
<?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:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>

Categories

Resources