android TextView : Change Text Color on click

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

  • How to auto center crop ImageView Android?
  • Change Material Design AppCompat ActionBar Color
  • ImageButton Border Transparency - How do I remove the square border around round image?
  • startManagingCursor(cursor) deprecated method
  • Galaxy S5 Lollipop - not all breakpoints stop execution under Android Studio debugger
  • Android - OnDateChangedListener - how do you set this?
  • <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: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

    Related posts:

    Embedding ZXing in android app
    Rich text in a textview
    Android System App 101
    Creating an Android View with a particular style programmatically
    Realtime P2P communication between mobile devices
    android - service stops when activity is destroyed
  • Communication between Fragment and DialogFragment
  • TransformException duplicate entry for common.annotations.Beta
  • Android location permissions
  • Where to find free open sourced android custom controls?
  • Can't delete image in contenteditable div on Android
  • How to increase consistency of Android geofence enter/exit notifications?
  • 3 Solutions collect form web for “android TextView : Change Text Color on click”

    Take a look here:

    and here!:
    How to define ColorStateList for TextView?

    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) {
                case MotionEvent.ACTION_DOWN:
                ((TextView)view).setTextColor(0xFFFFFFFF); //white
                case MotionEvent.ACTION_CANCEL:             
                case MotionEvent.ACTION_UP:
                ((TextView)view).setTextColor(0xFF000000); //black
            return false;   

    Then you can assign this to whatever TextView you wish:

    newTextView.setOnTouchListener(new CustomTouchListener());

    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:

    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);
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.