1

In my Android app, I'll let user see a demo which would auto click [ simulate mouse clicks ] on some buttons, I've done the auto click part from my previous question [ How to simulate a delay? ], but I wonder if there is a way to show on screen the color change on a button [when I simulate a click ] so user can see that it is supposed to be clicked, otherwise without color change, how would they know which button is clicked, so my question is how to let a button change it's background color to blue [ or any other color I like ], then .3 second later change back to its original color ? I'm looking for a method, so I can call like this :

Button myButton = new Button(...);
int duration = 300;  // 300 ms
changeColor(myButton, Color.blue, duration);

In Android how to achieve this effact ?

Frank
  • 30,590
  • 58
  • 161
  • 244

1 Answers1

1

Well you could use states of button for changing color like you want. For this approach you will need create a drawable xml file. Something like this(example from one of my project):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <solid android:color="@color/transparent_button_ripple_color"/>
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <solid android:color="@color/transparent_button_ripple_color"/>
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle"  >
            <solid android:color="@color/transparent_button_ripple_color"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <gradient android:angle="-90" android:startColor="@color/colorTransparent" android:endColor="@color/colorTransparent" />
        </shape>
    </item>
</selector>

And them control the color by setting state to the button. Also for representation of click in android you could add a ripple effect(Just for notice). https://guides.codepath.com/android/ripple-animation

Edited

I looked to your previous question and create the solution with respect of your code:

Create this method:

private void updateViewColorWithDelay(View targetView) {
    targetView.setBackgroundColor(Color.parseColor("#000000"));
    targetView.postDelayed(new Runnable() {
        @Override
        public void run() {
            ivAvatar.setBackgroundColor(Color.parseColor("#ffffff"));
        }
    },300);
}

And use it with your views:

private class AutoDemoListener implements View.OnClickListener {
    public void onClick(View v) {
        Is_AutoDemo_B=true;
        Out("AutoDemoListener");
        switchView(demoView, registrationView);
        startRegistration();
        final Handler handler = new Handler();

        registrationView.symbolButton[2][8].performClick();
        updateViewColorWithDelay(registrationView.symbolButton[[2][8]);

        handler.postDelayed(new Runnable() {
            public void run() {
                registrationView.symbolButton[4][13].performClick();
                updateViewColorWithDelay(registrationView.symbolButton[4][13]);
            }
        }, 1000);

        handler.postDelayed(new Runnable() {
            public void run() {
                registrationView.symbolButton[0][1].performClick();
                updateViewColorWithDelay(registrationView.symbolButton[0][1]);
            }
        }, 3000);

        handler.postDelayed(new Runnable() {
            public void run() {
                registrationView.symbolButton[6][18].performClick();
                updateViewColorWithDelay(registrationView.symbolButton[6][18]);
            }
        }, 5000);

        handler.postDelayed(new Runnable() {
            public void run() {
                Is_AutoDemo_B=false;
            }
        }, 5100);

    }
}
  • All my buttons are generated by my Java program, how to achieve this by program without using xml ? – Frank Jan 09 '20 at 18:57