0

I have an ImageButton in my application.

<ImageButton
  android:id="@+id/imageButton1"
  android:layout_width="80dp"
  android:layout_height="80dp"
  android:clickable="true"
  android:src="@drawable/button" />

I bind it to an onClickListener:

View.OnClickListener imgButtonHandler = new View.OnClickListener() {
  public void onClick(View v) {
      // Here I update the image source to a different image.
  }
};

Now what happens is: when I click the imagebutton, the imagebutton changes to a different image. But I want it to change back automatically after 0.5 sec (during the same time the user should not be able to click anything). How do I implement that? I tried to sleep in the onClick function in the listener, but it's not working...

New edit:

The proposed answer will solve my problem if I only have one imagebutton. I tried it out and both work like charm!

Actually it is not working as expected. During that 500ms, the user still could click! It is not solving the problem...

3 Answers 3

2

Posting a delayed runnable might do the job.

public void onClick(View v) {

    new Handler().postDelayed(new Runnable() {

    @Override
        public void run() {
            // change the background of the image button
            (ImageButton)v.setBackgroundResource(R.drawable.someimage);
        }
    }, 500);
}

EDIT:

In fact, I've ran the following code on an actual device with two ImageButton and it works fine.

BTW, if you want the buttons to be un-clickable during the 500ms, just set it as imgBtn1.setClickable(false); and set it back to be clickable in the runnable as imgBtn1.setClickable(true);

public class TestFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.test_layout, container, false);

    final ImageButton imgBtn1 = (ImageButton) view.findViewById(R.id.test_img_btn1);
    final ImageButton imgBtn2 = (ImageButton) view.findViewById(R.id.test_img_btn2);

    imgBtn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            imgBtn1.setBackgroundResource(R.drawable.device_type_apple);
            imgBtn1.setClickable(false);
            new Handler().postDelayed(new Runnable() {

                @Override
                    public void run() {
                        // change the background of the image button
                        imgBtn1.setBackgroundResource(R.drawable.device_type_windows);
                        imgBtn1.setClickable(true);
                    }
                }, 500);
        }
    });

    imgBtn2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            imgBtn2.setBackgroundResource(R.drawable.device_type_apple);
            imgBtn2.setClickable(false);
            new Handler().postDelayed(new Runnable() {

                @Override
                    public void run() {
                        // change the background of the image button
                        imgBtn2.setBackgroundResource(R.drawable.device_type_android);
                        imgBtn2.setClickable(true);
                    }
                }, 500);
        }
    });

    return view;
}

}

5
  • How can I access v in the run function? Feb 18, 2014 at 2:08
  • make the imageButton final.
    – J Jiang
    Feb 18, 2014 at 2:12
  • I update my question. It works if I only have one image button and if I only flip it once. Feb 18, 2014 at 2:19
  • Please see my updated question. It's not working because if I have three buttons, during that 500ms the users could still click other buttons! We need to block the interaction during that 500ms. Feb 18, 2014 at 2:30
  • See the edit, just set it to be ` imgBtn1.setClickable(false);` and set it back in the runnable.
    – J Jiang
    Feb 18, 2014 at 2:32
1

You can use handle with runnable to auto update image

Handler mHandler = new Handler();
    Runnable mUpdateTimer = new Runnable() {

        @Override
        public void run() {
            // code update image here

            // auto update after 0.5s
            mHandler.postDelayed(mUpdateTimer, 500);
        }
    };

And when image button clicked:

View.OnClickListener imgButtonHandler = new View.OnClickListener() {
  public void onClick(View v) {
      mHandler.postDelayed(mUpdateTimer, 500);
  }
};
1
  • I update my question. It works if I only have one image button and if I only flip it once. – Feb 18, 2014 at 2:19
0
Handler.postDelayed

this method is not good way ,see http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,long) it say

Returns true if the Runnable was successfully placed in to the message queue. Returns false on failure, usually because the looper processing the message queue is exiting. Note that a result of true does not mean the Runnable will be processed -- if the looper is quit before the delivery time of the message occurs then the message will be dropped.

so this methed may never invoke,it may be make you image button status never come back ,so you must be care the return value or use other widget ViewFliper,it can set animation when image switch and you can set delpoy .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.