Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlight TextView when clicked programmatically

I dynamically generate TextViews which work like buttons. Now i want to highlight them when they get pressed. Something like change Text color or background color. I have tried to use 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 my loop for creating the 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++;
    }
like image 932
Daniel Storch Avatar asked Oct 16 '14 09:10

Daniel Storch


People also ask

Can I make a TextView clickable?

In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.

How do you highlight text on Android?

To highlight text, touch and hold, then drag your finger.

Can you highlight words in a text message?

Tap on the annotations icon at the bottom of the preview screen to open the annotations toolbar. You will see a set of tools appear. Next, select the highlight text tool.


2 Answers

First of all your this line is creating ambiguity as you are taking a variable name as btn1 (which is relating it to button)and you are taking a reference of TextView,

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

Anyways,Go step by step,

  • create an xml like label_bg.xml like the following in the drawable folder:

     <?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 values folder create another xml like the following,named as 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" />
    

as you are dynamically adding the views so you need to set the background for your each textView programatically .For that call setBackgroundResource() on the textview object created and set label.xml as background

like image 198
nobalG Avatar answered Sep 25 '22 21:09

nobalG


You need to create a class implements with OnTouchListener and Detect touch Motin. ACTION_DOWN, change text color and ACTION_UP change it's default color according to 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 set TouchListener using

textView.setOnTouchListener(new CustomTouchListener());
like image 32
Dilip Avatar answered Sep 21 '22 21:09

Dilip