9

So currently, I am trying to create a selected state for three textviews

Currently, for each of the textviews ( H M S) the text is red:

enter image description here

However, when tap the H M or S I want it to turn another color--white.

So I tried to follow this:

Android - Textview change color on changing of state

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 that:

  <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, the textviews do not change color after I click on them.

How do I fix this?

Also, I was wondering if it is better to implement this in java code since I need to perform a different function after each textview is clicked/highlighted. If so, how can that be implemented?

Community
  • 1
  • 1
Rohit Tigga
  • 2,373
  • 9
  • 43
  • 81

1 Answers1

10

You can do this programatically like this:

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);
        }

    }
});

And here's my layout:

<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:

For your specific case would be like:

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);

}
Simas
  • 43,548
  • 10
  • 88
  • 116
  • I see, but I was asking if I press it and then it stays a different color until I press it again. – Rohit Tigga Jul 17 '14 at 08:49
  • @XiJiaopin Well that's exactly what it does. It resets only when the activity is recreated. – Simas Jul 17 '14 at 08:52
  • Is there any way to make it so that only one is selected at a time? – Rohit Tigga Jul 17 '14 at 09:16
  • Would you have to deselect the first one to select another one? or should they switch? – Simas Jul 17 '14 at 09:21
  • Let's say TextView A is selected. when the textview b is touched then textview A automatically gets deselected and textview b is the only one selected. – Rohit Tigga Jul 17 '14 at 09:25
  • @XiJiaopin updated. Code will prevent more than 1 TextViews to be selected. – Simas Jul 17 '14 at 09:33
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57478/discussion-between-xijiaopin-and-user3249477). – Rohit Tigga Jul 17 '14 at 09:38
  • In my case, I only needed the else case block part. It is working but one problem that I have been facing is that I am using the selector in the text view from XML that will not work once this above logic is called inside onClick. How can I solve that part? – bhaskar Aug 08 '18 at 04:14