Android: create a TextView that will flash when clicked

How to set up TextView instantly when clicked? With blinking, I want to say that I want to change the background color of the TextView. I essentially want one of the objects to appear in the ListActivity, but inside a regular view.

I tried to do this by adding OnClickListener, but I really need something like adding On (Un) SelectListener. Using onClickListener, I can change the background of the TextView, but obviously the background remains in that color. I was thinking about using a new handler (). PostDelayed (new Runnable () {...}) kind of reset the background after a little while, but I did not know if this would be excessive, m trying to do.

What would you recommend?

+4
source share
1 answer

This is usually achieved by using a <selector> . For instance:

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

The selector performs arbitration between other drawings depending on the state of the view in which it is located. You put the above xml into a file in res / drawable / and then used it as a background for your view. You must also have normal and pressed outputs.

You can also create selector selections in code where it is called StateListDrawable .

But maybe your solution is simpler ...

+1
source

Source: https://habr.com/ru/post/1305877/


All Articles