Issue
I'm trying to change the background color of a Button
when it is clicked, I can change the background color when I have the Button
pressed down when I release the Button
the button returns to its default color. I'm not sure where to go from here I have been trying to find a solution to this problem.
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.game.dice.MainActivity">
<Button
android:id="@+id/but1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:background="@drawable/button_color"
/>
<Button
android:id="@+id/but2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:background="@drawable/button_color"/>
</LinearLayout>
`
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/button_pressed"/>
<item android:state_focused="true"
android:drawable="@color/button_focused"/>
<item android:drawable="@color/button_default"/>
</selector>
Solution
This is because by default when the button is touched it will take click instead of focus.If you want button to be focused and change its color when pressed add this to your button in xml.
android:focusableInTouchMode="true"
Answered By - Sharath kumar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.