Home > Enterprise >  How to highlight a TextView when you click on it just like when you click on a Button
How to highlight a TextView when you click on it just like when you click on a Button

Time:12-10

By default, when you click on a Button, it will get highlighted. How to do the same thing for TextView when the user click on it?

CodePudding user response:

You should be able to set this in the xml layout file, by setting the view clickable and setting the foreground property correctly.

For example:

...
<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:foreground="?attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
/>
...

Just a note, foreground is only available in Android API version 23 or above. If you need to use it in a version below that, replace android:foreground with android:background. Not exactly the same behaviour (ripple effect is placed in the background of the text, instead of in a layer above of the view as the property name indicates), but it should do the trick.

  • Related