Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android TextView : Change Text Color on click

Tags:

I have a textfield that behaves like a local link, clicking on it fetches an image from database and shows it. It doesn't ping to server all the time.

Here is the xml code for the text view

<TextView android:layout_marginLeft="2dp" android:linksClickable="true"             android:layout_marginRight="2dp" android:layout_width="wrap_content"             android:text="@string/Beatles" android:clickable="true" android:id="@+id/Beatles"             android:textColor="@color/Black"             android:textSize="12dp" android:layout_height="wrap_content" android:textColorHighlight="@color/yellow" android:textColorLink="@color/yellow"  android:autoLink="all"></TextView> 

The question is i want to see the color of text view should be changed in yellow, instead of the same black color,

Just Like the button behavior but instead of changing the background color i want to change a text color

like image 949
Prasham Avatar asked Dec 17 '10 06:12

Prasham


People also ask

How to change text color dynamically in android?

TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How do I change text color in XML?

In XML, we can set a text color by the textColor attribute, like android:textColor="#FF0000" .

How can I change the color of my image in Android?

Tint color means when we want to change the color of the image while rendering in ImageView. In XML is very easy to change tint color by just setting up the attribute android:tint="" in the ImageView tag, as shown in the following example.


2 Answers

I like what Cristian suggests, but extending TextView seems like overkill. In addition, his solution doesn't handle the MotionEvent.ACTION_CANCEL event, making it likely that your text will stay selected even after the clicking is done.

To achieve this effect, I implemented my own onTouchListener in a separate file:

public class CustomTouchListener implements View.OnTouchListener {          public boolean onTouch(View view, MotionEvent motionEvent) {     switch(motionEvent.getAction()){                         case MotionEvent.ACTION_DOWN:             ((TextView)view).setTextColor(0xFFFFFFFF); //white                 break;                       case MotionEvent.ACTION_CANCEL:                          case MotionEvent.ACTION_UP:             ((TextView)view).setTextColor(0xFF000000); //black                 break;     }          return false;        }  } 

Then you can assign this to whatever TextView you wish:

newTextView.setOnTouchListener(new CustomTouchListener());

like image 122
plowman Avatar answered Sep 22 '22 00:09

plowman


You can create your own TextView class that extends the Android TextView class and override the onTouchEvent(MotionEvent event)

You can then modify the instances text color based on the MotionEvent passed.

For example:

@Override public boolean onTouchEvent(MotionEvent event) {     if (event.getAction() == MotionEvent.ACTION_DOWN) {        // Change color     } else if (event.getAction() == MotionEvent.ACTION_UP) {        // Change it back     }     return super.onTouchEvent(event); } 
like image 42
Cristian Avatar answered Sep 25 '22 00:09

Cristian