1

The below code is for ImageButton to change its image on every click. I have created a loop to change its position, but it changed so fast.

So I need a delay function. I have tried this solution, but it did not work for me.

It says "Handler is abstract and cannot be instantiated"

The code:

public void ShapeSelectingInGame() {

    ShapeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ShapeButton = (ImageButton) v;
            selectShape = rand.nextInt(4);
            ShapeSaying = rand.nextInt(8);
            ColorOfShape = rand.nextInt(10);
            shapeID = "shape_" + selectShape + ShapeSaying + ColorOfShape;
            resID = getResources().getIdentifier(shapeID, "drawable", "com.example.asgames.hitit");
            ShapeButton.setImageResource(resID);
            HitTypeString.setVisibility (View.INVISIBLE);
        }
    });

    for (int i = 10; i < 10000; i += 100)
    {
        ShapeButton.setX(i);
    }
    ShapeButton.setVisibility(View.VISIBLE);
}
Ahmad
  • 1,618
  • 5
  • 24
  • 46

5 Answers5

3

Use handler.postDelay

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
    // your code here
}
}, 1000);

where 1000 means 1 second

Anmol317
  • 1,356
  • 1
  • 14
  • 31
1

Try this and pass milliseconds as the parameter:

wait(20000);

20000 means you are waiting for 20 seconds where 20000 are milliseconds.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Muhammad Saad Rafique
  • 3,158
  • 1
  • 13
  • 21
1

I have tried this solution but did not work for me it says "Handler is abstract and cannot be instantiated"

You have imported wrong Handler which is ava.util.logging.Handler

You should import following

import android.os.Handler;

Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
1
   private Handler mHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            ShapeButton = (ImageButton) v;
            selectShape = rand.nextInt(4);
            ShapeSaying = rand.nextInt(8);
            ColorOfShape = rand.nextInt(10);
            shapeID = "shape_" + selectShape + ShapeSaying + ColorOfShape;
            resID = getResources().getIdentifier(shapeID, "drawable", "com.example.asgames.hitit");
            ShapeButton.setImageResource(resID);
            HitTypeString.setVisibility (View.INVISIBLE);
        }
    };
    private static final int DELAY = 3000;
    public void ShapeSelectingInGame() {
        ShapeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mHandler.postDelayed(mRunnable, DELAY);
            }
        });

        for (int i=10; i<10000;i+=100)
        {
            ShapeButton.setX(i);
        }
        ShapeButton.setVisibility(View.VISIBLE);


 }

It seems you have imported a wrong Handler class

import java.util.logging.Handler;

Change it to

import android.os.Handler;
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60
  • I corrected that but the object v is not declared anymore and the Image button never appeared – Ahmad Apr 18 '17 at 14:59
0

You can try this code;

  new Handler().postDelayed(
                new Runnable() {
                    public void run() {

  // your code

 }
                }, 3000); // milisecond
Berat Eyüboğlu
  • 1,663
  • 3
  • 18
  • 32