0

I have 3 text views as shown below. once i click on one of it, now it turns to red and turn backs to its default colour. I want to keep the selected textview in red. i have these 3 textview 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);
        TextView clickTextView1 = (TextView) view.findViewById(R.id.footer1);
        TextView clickTextView2 = (TextView) view.findViewById(R.id.footer2);

        clickTextView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "toppings!",
                        Toast.LENGTH_LONG).show();
            }

        });
        clickTextView1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "Bigg pizza!",
                        Toast.LENGTH_LONG).show();
            }

        });
        clickTextView2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(getActivity(), "Italiano!",
                        Toast.LENGTH_LONG).show();
            }

        });

.xml file

<?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" android:state_selected="false" >
        <color android:color="#ff0000"/>
    </item>
    <item android:state_pressed="false" android:state_selected="true">
        <color android:color="#ff0000"/>
    </item>
    <item android:state_pressed="true" android:state_selected="true">
        <color android:color="#ff0000"/>
    </item>
</selector>

layout

 <lk.gamma.pizzakraft.menu.QuickReturnListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/footer1"
        android:layout_width="106.5dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:textColor="@drawable/textview_font"
        android:paddingTop="8dip"
        android:text="@string/footer3"
        android:textAppearance="?android:attr/textAppearanceSmall"
        />
    <!-- android:background="#D8000000" -->

    <TextView
        android:id="@+id/footer2"
        android:layout_width="107dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toRightOf="@+id/footer"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:textColor="@drawable/textview_font"
        android:text="@string/footer2"
        android:textAppearance="?android:attr/textAppearanceSmall"
         />

    <TextView
        android:id="@+id/footer"
        android:layout_width="106dip"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:background="@drawable/textview_background"
        android:gravity="center"
        android:paddingBottom="8dip"
        android:paddingLeft="16dip"
        android:paddingRight="16dip"
        android:paddingTop="8dip"
        android:textColor="@drawable/textview_font"
        android:text="@string/footer1"
        android:textAppearance="?android:attr/textAppearanceSmall"
         />

enter image description here

John David
  • 334
  • 3
  • 25

3 Answers3

3

There is one simple solution:

private static TextView selectedView = null;

... ...

eachTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //change last selected view to be normal
            if(selectedView != null)
                 selectedView.setTextColor(Color.parseColor("#D8000000"));

            //set selectedView to be current selected view
            selectedView = (TextView)v;

            //change selected view to be red
            selectedView.setTextColor(Color.parseColor("#FF0000"));



            Toast.makeText(getActivity(), "toppings!",
                    Toast.LENGTH_LONG).show();
        }

    });

If you want to change TextView background color insdead of text color, then just replace 'setTextColor' with 'setBackgroundColor'.

Besides for your case, seems it is not neccessary to use selector, because it is just for specific view state change, nothing to do with other view.

Xcihnegn
  • 11,579
  • 10
  • 33
  • 33
0

you can do it programatically. For example, place this code inside clickTextView.setOnClickListener().

clickTextView.setTextColor(Color.parseColor("#FF0000"));

The text view will change to red.

For this to work you do not need an xml file containing color rules.

Vasanth
  • 60
  • 4
0

Try with below code:

clickTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
              changeViewBackground(true,false,false);
        }

    });
    clickTextView1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            changeViewBackground(false,true,false);
        }

    });
    clickTextView2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            changeViewBackground(false,false,true);
        }

    });


    private void changeViewBackground(boolean view1,boolean view2,boolean  
        view3) {    
            clickTextView.setSelected(view1);
            clickTextView1.setSelected(view2);
            clickTextView2.setSelected(view3);
      }
user543
  • 3,623
  • 2
  • 16
  • 14
  • im using these 3 textviews as 3 tabs, i want to highlight the selected tab in to red and other 2 should be stay in default colour. when i use this, once i select the tab it turns to red forever. how can i chnage the highlighting tab colour according to my tab selection – John David Feb 06 '15 at 06:39
  • i used private void changeViewBackground(boolean view1,boolean view2,boolean view3) { , outside the onCreateView. then it crashed il update m question with error message i got – John David Feb 06 '15 at 07:04
  • There is simple solution: – Xcihnegn Feb 06 '15 at 08:18