Android: Visual version of Button.performClick

I know that I can launch the OnClickListener.onClick buttons manually in the code by calling the performClick function, but this does not look like a visual display since it was clicked. I am looking for a way to manually make a button as if it were pressed. Do I need to manually change the wallpaper and cancel (and then change it again to calling Handler.postDelayed), or is there a way to do this in my pocket?

EDIT

I know how to make a button display different patterns when a user starts printing. The question is:

Is there an easy way to make a button pressed programmatically if the user has not physically pressed?

Decision

I simply subclassed Button and made the button aware of this normal background as StateListDrawable and Drawable, which is used as the pressed state. I am exposing a method that manually sets the background to a "clicked" drawable, and I use Handler.postAtTime to return it to a normal background so that it can be used again as a regular button when I am done.

+3
source share
2 answers

Although this question is very old, I decided that I would still answer. You do not need to subclass the view. The first call performClick(), the visual prompt does not last long, but then you can set the state of the pressed button with view.setPressed(true);, and then reset a couple of milliseconds later, like this.

handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        view.setPressed(false);
    }
}, 100);
+4
source

Ya, 2 drawables. .

xml, :

<?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/focused_drawable" />
    <item
     android:state_pressed="false"
     android:drawable="@drawable/unfocused_drawable" />
</selector>

xml .

android:state_focused="true"

, , :

android:background="@drawable/your xml file name"
0

All Articles