Highlight TextView on click programmatically

I am dynamically creating TextViews that work like buttons. Now I want to highlight them when they are clicked. Something like changing the text color or background color. I tried using a selector but it doesn't work.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#ffffff"/>
<item android:state_pressed="true" android:state_enabled="false" android:color="#ffffff" />
</selector>

      

Here's my loop for creating TextViews.

int z = 0;
    for (MOKGenericDataItem d : data) {
        if (d.getButtonText() != null) {
            final int pagePosition = z;
            TextView btn = new TextView(getActivity());
            btn.setId(z);
            final int id_ = btn.getId();
            btn.setText(d.getButtonText());
            btn.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
            btn.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
            btn.setGravity(Gravity.CENTER);

            mLineareLayoutViewPagerButtons.addView(btn);

            btn1 = ((TextView) view.findViewById(id_));
            btn1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    mViewPager.setCurrentItem(pagePosition,false);
                }
            });
        }
        z++;
    }

      

+3


source to share


2 answers


First of all, this line creates ambiguity as you are taking the variable name as btn1 (which is associated with it) and you are taking the reference TextView

,

 btn1 = ((TextView) view.findViewById(id_));

      

Anyway Go step by step



  • create xml like label_bg.xml

    below in folder drawable

    :

     <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:drawable="@drawable/pressed_color"
                  android:state_pressed="true" />    
            <item android:drawable="@drawable/normal_color" />
        </selector>
    
          

  • In the folder values

    create another one xml

    as shown below:labelcolors.xml

     <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <drawable name="pressed_color">#7ec0ee</drawable> <!--custom color for pressed state -->
    <drawable name="normal_color">#00FFFFFF</drawable> <!--transperent color for normal state -->
    </resources>
    
          

  • Now set the background of the label as label_bg.xml

      <TextView
        android:id="@+id/yourlabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="760dp"
        android:layout_marginTop="515dp"
        android:background="@drawable/label_bg"   <!--like this-->
        android:text="LabelText"
        android:textSize="20dp" />
    
          


since you are dynamically adding views, so you need to set a background for each of your textViews . For this call setBackgroundResource()

on the created object TextView

and set label.xml

as background

+5


source


You need to create a class with OnTouchListener

and Detect touch Motin

. ACTION_DOWN

, change the text color and ACTION_UP

change the default color as per your requirement.

Code:

public class CustomTouchListener implements View.OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
        case MotionEvent.ACTION_DOWN:
            ((TextView) view).setTextColor(0xFFFFFFFF); // white
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            ((TextView) view).setTextColor(Color.parseColor("#4a4a4a")); // lightblack
            break;
        }
        return false;
    }
}

      



Now install the TouchListener with

textView.setOnTouchListener(new CustomTouchListener());

      

+2


source







All Articles