Android - Textview Change Color on Changing of State

Android - Textview change color on changing of state

Create new a new xml (in the drawable folder). with the color you can specify image for each event state

and you can you can set this xml as you background

if your xml is 'res/drawable/abc.xml' then set background as

android:background="@drawable/abc"

Edited to add color in state xml

our xml, res/drawable/abc.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="@color/gray" />
</selector>

Then declare gray in your res\values\strings.xml

<color name="gray">#808080</color>

Android: change TextView textColor when parent is focused

This is an old post, but since I had the same problem, here is the XML attribute I found to do this :

android:duplicateParentState="true"

(to be added to the TextView to change its "focused" state when the Layout's state changes)

How to set the text color of TextView in code?

You should use:

holder.text.setTextColor(Color.RED);

You can use various functions from the Color class to get the same effect of course.

  • Color.parseColor (Manual) (like LEX uses)

    text.setTextColor(Color.parseColor("#FFFFFF"));
  • Color.rgb and Color.argb (Manual rgb) (Manual argb) (like Ganapathy uses)

    holder.text.setTextColor(Color.rgb(200,0,0));
    holder.text.setTextColor(Color.argb(0,200,0,0));
  • And of course, if you want to define your color in an XML file, you can do this:

    <color name="errorColor">#f00</color>

    because the getColor() function is deprecated1, you need to use it like so:

    ContextCompat.getColor(context, R.color.your_color);
  • You can also insert plain HEX, like so:

    myTextView.setTextColor(0xAARRGGBB);

    Where you have an alpha-channel first, then the color value.

Check out the complete manual of course, public class Color extends Object.


1This code used to be in here as well:

textView.setTextColor(getResources().getColor(R.color.errorColor));

This method is now deprecated in Android M. You can however use it from the contextCompat in the support library, as the example now shows.

Android: how can I change the color of a textview when a button is pressed with selector XML

You mentioned you wish to change the color of textview but never mentioned color of text in textview. this can be done very easily using the same selector file. Implement this way for proper implementation, so you can just get things done inside onclicklistener no need to handle button states. Modify your selector file as

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/default_blue" android:color="@color/default_blue"/>
<item android:state_pressed="false" android:drawable="@color/default_color" android:color="@color/default_color"/>
</selector>

in your layout xml for textview set

android:textColor = "@drawable/buttons_drawable_resource"

Note: Do not skip to add android:state_pressed="false" in selector as i gave above with default selections for button as well as textview text color else it will crash with NPE.

Change textview color on selected

Create a color state list resource in your /res/color/ folder, like this:

/res/color/text_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ff0000" />
<item android:color="@android:color/white"/>
</selector>

Then use this resource as your text color:

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textColor="@color/text_color"
android:clickable="true"
/>

Note that for this to work, you either have to set the android:clickable attribute to true or set a click listener on your textview in code.

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 of setBackgroundColor().

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.

Using selector to change TextView text color

You have to use getColorStateList(). And for xml, see here.

I was also struggling with this problem. If you want to have use a state list, you need to declare it in the color resources folder, instead of the drawable folder, and use the setTextColor(getResources().getColorStateList(R.color.tab_text_selector)) method.



Related Topics



Leave a reply



Submit