0

I have 3 text views as shown below. Once I click on one of them, it turns to red but turns back to its default colour when I unselect it. I want to keep the selected TextView in red. I have these 3 TextViews in a fragment.

mQuickReturnView = (TextView) view.findViewById(R.id.footer);
mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1);
mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2);

TextView clickTextView = (TextView) view.findViewById(R.id.footer);

clickTextView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(getActivity(), "I just clicked my textview!",Toast.LENGTH_LONG).show();
    }
});

xml.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- not selected has transparent color -->
    <item android:state_pressed="false" android:state_selected="false">
        <color android:color="#D8000000"/>
    </item>
    <item android:state_pressed="true" >
        <color android:color="#ff0000"/>
    </item>
    <item android:state_pressed="false" android:state_selected="true">
        <color android:color="#ff0000"/>
    </item>
</selector>

What should I change to keep it in red once selected.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
John David
  • 334
  • 3
  • 25

1 Answers1

1

You could achieve this using a switch case statement.

First add onClick listeners to your TextViews

mQuickReturnView = (TextView) view.findViewById(R.id.footer);
mQuickReturnView1 = (TextView) view.findViewById(R.id.footer1);
mQuickReturnView2 = (TextView) view.findViewById(R.id.footer2);

mQuickReturnView.setOnClickListner(this);
mQuickReturnView1.setOnClickListner(this);
mQuickReturnView2.setOnClickListner(this);

Then implement the onClick method like below.

@Override
public void onClick(View v) {
    switch(v.getId()){
        /*First TextView was clicked, set it as your clicked color and 
          the others as your default, non-clicked color. */
        case R.id.footer:
            mQuickReturnView.setBackgroundColor(Color.GREEN); 
            mQuickReturnView1.setBackgroundColor(Color.BLACK); 
            mQuickReturnView2.setBackgroundColor(Color.BLACK)); 
        break;

        /*Second TextView was clicked, set it as your clicked color and 
          the others as your default, non-clicked color. */
        case R.id.footer1:
            mQuickReturnView.setBackgroundColor(Color.BLACK); 
            mQuickReturnView1.setBackgroundColor(Color.GREEN); 
            mQuickReturnView2.setBackgroundColor(Color.BLACK); 
        break;

        /*Third TextView was clicked, set it as your clicked color and 
          the others as your default, non-clicked color. */
        case R.id.footer2:
            mQuickReturnView.setBackgroundColor(Color.BLACK); 
            mQuickReturnView1.setBackgroundColor(Color.BLACK); 
            mQuickReturnView2.setBackgroundColor(Color.GREEN); 
        break;
    } 
}
Marcus
  • 6,697
  • 11
  • 46
  • 89
  • Marcus, i want to change the textview background colour, not the text colour – John David Feb 06 '15 at 04:34
  • If you want to change the background color, you just call `mQuickReturnView.setBackgroundColor(Color.GREEN);` instead of `mQuickReturnView.setColor(Color.GREEN);`. I've updated my answer. @JohnDavid – Marcus Feb 07 '15 at 11:40