Irfan
Irfan

Reputation: 97

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

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?

Upvotes: 0

Views: 855

Answers (1)

Daniel Beleza
Daniel Beleza

Reputation: 419

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.

Upvotes: 1

Related Questions