Make the selected state for text view (highlighted)

So, I'm currently trying to create the selected state for three text views

Currently, for each of the text views (HMS), the text is red:

enter image description here

However, when you press HM or S, I want it to turn a different color - white.

So I tried to do the following:

Android - text color change when state changes

and I did this (selected_text.xml):

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true"
            android:color="#ffd10011"/> <!-- selected -->
        <item android:color="@color/red_highlight"/> <!-- default -->
</selector>

and applied this:

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Hours"
        android:textSize="30sp"
        android:textColor="@color/selected_text"
        android:id="@+id/hourtext"
        android:layout_marginLeft="45dp"
        android:layout_alignTop="@+id/minutetext"
        android:layout_alignLeft="@+id/seekArc"
        android:layout_alignStart="@+id/seekArc" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Minutes"
        android:textSize="30dp"
        android:textColor="@color/selected_text"
        android:id="@+id/minutetext"
        android:layout_below="@+id/seekArc"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="28dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/Second"
        android:textSize="30dp"
        android:textColor="@color/selected_text"
        android:id="@+id/secondtext"
        android:layout_alignTop="@+id/minutetext"
        android:layout_alignRight="@+id/seekArc"
        android:layout_alignEnd="@+id/seekArc"
        android:layout_marginRight="43dp" />

However, text images do not change color after clicking on them.

How to fix it?

Also, I was wondering if it is better to implement this in java code, since I need to execute a different function after each mouse click / selection. If so, how can this be implemented?

+4
1

:

findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView textView = (TextView) v;
        if (textView.isSelected()) {
            textView.setTextColor(Color.RED);
            v.setSelected(false);
        } else {
            textView.setTextColor(Color.BLUE);
            v.setSelected(true);
        }

    }
});

:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="some text"
        android:textSize="20sp"
        android:textColor="#FF0000"/>

</LinearLayout>

EDIT:

:

View previousView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView previousText = (TextView) previousView;
            TextView curText = (TextView) v;
            // If the clicked view is selected, deselect it
            if (curText.isSelected()) {
                curText.setSelected(false);
                curText.setTextColor(Color.RED);
            } else { // If this isn't selected, deselect  the previous one (if any)
                if (previousText != null && previousText.isSelected()) {
                    previousText.setSelected(false);
                    previousText.setTextColor(Color.RED);
                }
                curText.setSelected(true);
                curText.setTextColor(Color.BLUE);
                previousView = v;
            }

        }
    };

    findViewById(R.id.txt).setOnClickListener(clickListener);
    findViewById(R.id.txt2).setOnClickListener(clickListener);
    findViewById(R.id.txt3).setOnClickListener(clickListener);

}
+6

All Articles