Selector on background color of TextView
The problem here is that you cannot define the background color using a color selector, you need a drawable selector. So, the necessary changes would look like this:
<?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/selected_state" />
</selector>
You would also need to move that resource to the drawable
directory where it would make more sense since it's not a color selector per se.
Then you would have to create the res/drawable/selected_state.xml
file like this:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/semitransparent_white" />
</shape>
and finally, you would use it like this:
android:background="@drawable/selector"
Note: the reason why the OP was getting an image resource drawn is probably because he tried to just reference his resource that was still in the color directory but using @drawable
so he ended up with an ID collision, selecting the wrong resource.
Hope this can still help someone even if the OP probably has, I hope, solved his problem by now.
Android: How to use selector for background of a TextView?
Set this to your TextView:
android:clickable="true"
Changing TextView background color on click
When specifying a drawable for a background
android:background="@drawable/selectors"
the item selectors in selectors.xml
requires you to have the drawable
attribute set.
You're setting the color
attribute which won't work.
Instead change color
to drawable
.
<?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/option_on">
</item>
<item
android:drawable="@drawable/option_off" >
</item>
</selector>
Here's a reference. Refer to the item tag section.
android:drawable
Drawable resource. Required. Reference to a drawable resource.
Changing TextView background color on click android
If the textview is clicked the background changes to yellow and remains yellow until it is click again. Then it returns to its default background.
It's a matter of logic as you need to keep in your click listener the current click state.(blind coding):
textView.setOnClickClickListener(new View.OnClickListener() {
private boolean stateChanged;
public void onClick(View view) {
if(stateChanged) {
// reset background to default;
textView.setBackgroundDrawable(circleOffDrawable);
} else {
textView.setBackgroundDrawable(circleOnDrawable);
}
stateChanged = !stateChanged;
}
});
To improve the answer, you should keep stateChanged
flag in the activity and retain its value across activity recreations - if the user rotates the activity. (Store the flag in onSaveInstanceState
and restore in onCreate
if its parameter is not null.)
how to change the textView color from selector
Just try with this code -
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void clickme(View view)
{
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setTextColor(Color.RED);
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="clickme"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Try this with your Selector.
Selector on TextView does not change color
Use getColorStateList()
instead of getColor()
.
If you support < API 23, you can use the helper method,
ContextCompat.getColorStateList(context, R.color.your_selector);
Javadoc of the getColor():
Returns a color integer associated with a particular resource ID. If
the resource holds a complex {@link ColorStateList}, then the default
color from the set is returned.
JavaDoc of the getColorStateList():
Returns a color state list associated with a particular resource ID.
The resource may contain either a single raw color value or a complex
{@link ColorStateList} holding multiple possible colors.
Programatically make TextView background color change while pressed
You are on the right track. However there is an important detail.
There are two types of resources that can be affected by states: ColorStateList
and StateListDrawable
.
A color state list can only be used in certain contexts, for example in TextView.setTextColor()
. As far as I can see, you cannot use a color state list as parameter of setBackgroundColor()
if you want to change the background of a View when it's pressed. You need a state list drawable for that. And in a state list drawable, the android:drawable
attribute is mandatory.
So, to sum up:
- The xml file should be placed in
res\drawable
, - Its structure should be slightly different (i.e. state list, not color list), and
- You need to use
setBackgroundResource()
instead ofsetBackgroundColor()
.
Example file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@android:color/white" />
<item android:drawable="@android:color/black"/>
</selector>
If you want to use custom colors instead of white and black, you simply need to define them as resources in res\values
and reference them from here.
Related Topics
How to Make Sticky Headers in Recyclerview? (Without External Lib)
Bind Service to Activity in Android
Missing "<Sdk>/Extras/Google/Google_Play_Services/Libproject" Folder After Update to Revision 30
Get Path of Image from Action_Image_Capture Intent
Gradle Build Tool Cannot Find Play-Services-Tasks.Aar? Why
Android Adb Device Offline, Can't Issue Commands
Firebase (Fcm) How to Get Token
Android Sharedpreference Security
Imageview Be a Square with Dynamic Width
How to Restrict Android App to Specific Device Make
Edittext Loses Content on Scroll in Listview
Android and Setting Width and Height Programmatically in Dp Units
Background-Size:Cover Not Working in Android Native Browser
Edittext, Clear Focus on Touch Outside