Is there a basic XML android tag for changing the background color of a view when a user touches said view? Say it's a textview that is clickable. When the user touches the TextView, I want to give some feedback (i.e. change background color) so user knows it's a button.
-
Once you post a question, please make sure you try the answers posted by people and mark the correct answer. – Prem Dec 13 '13 at 19:50
3 Answers
You can set the background of any view using the style attribute.
You can modify the Text color, font and background color of the button using the style attribute.
Steps to do it.
Step 1. define an XML file called res/drawable/button_clickedState.xml
button_clickedState.xml
<?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/btn_pressed" />
<item
android:state_pressed="false"
android:drawable="@drawable/btn_normal" />
</selector>
Step 2
In Onclick method, add this line of code.
b.setBackgroundResource(R.drawable.button_clickedState);// b is your button
This stackoverflow question provides the details of how to set the style attribute for any views.
For doing this you need to create an XML say background.xml under Resources/drawable-hdpi directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/green"
/>
</selector>
Suppose your TextView is "tv" then add following line to your code
tv.setClickable(true);
tv.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
OPTIONAL
Now you may(or may not)also want to change the text color when user touches/presses button for that you need to create another xml say "textcolor.xml" in Resources/color directory in same way as background.xml is created.
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- color you want when view pressed-->
<item android:color="@color/white" /> <!--or whatever is your default color-->
</selector>
Of course you need to choose suitable colors which matches with background. After creating the xml you need to add following lines in your code (Again same assumption TextView name is "tv)
XmlResourceParser parse==getResources().getXml(R.color.textcolor);
ColorStateList csl=ColorStateList.createFromXml(getResources(), parse);
tv.setTextColor(csl);
[Note: In your Resources/values create a file color.xml and set the String color values]
<resources>
<color name="white"> #EFFBF5</color>
<color name="green">#008000</color>
</resources>

- 198
- 4
- 10
Yes, it can be done in XML.
First create a selector similar to the above answers in your drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/off_white" android:state_focused="true"/>
<item android:drawable="@color/off_white" android:state_pressed="true"/>
<item android:drawable="@color/white"/>
</selector>
Then set the the attribute android:background="@drawable/myDrawableSelector".
<TextView
android:id="@+id/login_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="@drawable/myDrawableSelector"
android:padding="12dp" />

- 8,810
- 2
- 30
- 38