How to Set Delay in Android Onclick Function

How to set delay in Android onClick function

Instead of sleeping in the onclick, you could post a delayed message to a handler (with associated runnable) and have it update the UI. Obviously fit this into the design of your app and make it work, but the basic idea is this:

//Here's a runnable/handler combo
private Runnable mMyRunnable = new Runnable()
{
@Override
public void run()
{
//Change state here
}
};

Then from onClick you post a delayed message to a handler, specifying that runnable to be executed.

Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable, 1000);//Message will be delivered in 1 second.

Depending on how complicated your game is, this might not be exactly what you want, but it should give you a start.

delay onClick event in android

Why don't you try adding the delay in the OnClick method itself? You'll have to use a handler and call the postDelayed method on it:

button1.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {

//Execute code here

}
}, 1000);

}
});

How to set a delay for setOnClickListener?

Use handler.postDelay

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

where 1000 means 1 second

Onclick has a delay

Another approach,

you can try with OnTouchListener & handle button with durations

that can achieve all the things that you have done in OnClickListener

OnClickListener - is used for a basic purpose, when we just need a click from a user i.e., click of a button, no drag or gestures on the screen

OnTouchListener is basically a more fine-tuned control than OnClickListener. It performs and action on a press/ release on the screen It allows a user with a combination of the move,down-touch,up-touch, finger drag or any movement gesture on the screen

onTouchListener Reference

How can I delay onClick action

Use Handler with postDelayed, example:

final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Log.d("Log:", "Hello!");
handler.postDelayed(this, 1000);
}
}, 1000);

onClick event with delay

I found a solution. I ended up using the timer class.

Here's my code:

public void playSound(View view) {
dBell.start();
if (vibeOn) {
Vibrator vibe = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibe.vibrate(500);
} else {
// No vibration
}
Timer timer = new Timer();
TimerTask picSwitch1 = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
dBellButt.setImageResource(R.drawable.doorbell);
}
});
}
};
TimerTask picSwitch2 = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
dBellButt.setImageResource(R.drawable.doorbell2);
}
});
}
};
timer.schedule(picSwitch1, 10);
timer.schedule(picSwitch2, 5400);
}

Using this allows the picture to switch after the delayed time.

The problem I was having with the sleep function was it would cause the application to freeze up (duh...). I couldn't use any of the other given interface. This solution fixes that problem. I'll attach the links I used to solve this.

http://developer.android.com/reference/java/util/Timer.html

http://blog.redkitmedia.com/timer-android-example/

Android timer/timertask causing my app to crash?

how to delay put on button click android

Try to use Handler to disable button for given time :

clcikbutton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss aa");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
textView1.setText(DateFormat.getDateTimeInstance().format(new java.util.Date("11/7/2014 5:19:11 AM UTC")));
clcikbutton.setEnabled(false);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
clcikbutton.setEnabled(true);
}
},5000);
}
});

onClick() setting incrementation and delay

Here's how I solve it. Although the suggestions were closer, they weren't exactly spot on to what I was looking for, in terms of logic, they weren't exactly helpful, creating more issues. Perhaps it's due to my explanation of the problem, perhaps because I'm new to Android Studio and the explanations shared here with me. Pardon me if it looks like I'm just using my own answer for internet points, I just had to put a lot more to understand this by myself than what I actually got from the answers shared here.

public void wait3s()
{
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
startStop();
}
});
}
}, 3000);
}

The variable t is a Timer(), and I had to import the classes java.util.Timer, and java.util.TimerTask. I called this method inside my increment() method and under onClick() I just have the increment() method. Turned out pretty neat.

How to set delay in android?

Try this code:

import android.os.Handler;
...
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// Do something after 5s = 5000ms
buttons[inew][jnew].setBackgroundColor(Color.BLACK);
}
}, 5000);

Delay Function on Android Project

You should put a Thread.sleep(long) before the "setContentView(R.layout.xxxx..)" in the onCreate(..) function. In that way, it will actually delay before showing you the elements of the Activity.

If you want to delay even before the onCreate(...) is fired, the approach will need to be different, here is one suggestion:

Run a Service and check for the Foreground application using ActivityManager class (see sample code below). Keep checking for when your app is fired or brought to the 'foreground' (using code below) and then just go back to homescreen & start a timer (in the service itself). Once the timer expires, start your app.

You can run the function below inside an AsyncTask in the Service.

The two approaches are quite different and really depends on what you are looking to achieve exactly.

@SuppressWarnings("deprecation")
private void getRunningAppName() throws NameNotFoundException {
Log.v("neiltag", "Entered getRunningAppName()");
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// The first in the list of RunningTasks is always the foreground task.
RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0);

String foregroundTaskPackageName = foregroundTaskInfo .topActivity.getPackageName();
PackageManager pm = this.getPackageManager();
PackageInfo foregroundAppPackageInfo = pm.getPackageInfo(foregroundTaskPackageName, 0);
String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo.loadLabel(pm).toString();
String packageName = foregroundAppPackageInfo.packageName;

if(foregroundTaskAppName.matches("<NAME OF YOUR APP HERE>")) {

//If your app is fired go back to the Homescreen(i.e. the delay)
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "You are not allowed to open Facbeook now, sorry!", Toast.LENGTH_SHORT).show();
}
});

}

//ADD A TIMER HERE
//ONCE TIMER EXPIRES, FIRE UP YOUR APP AGAIN

}


Related Topics



Leave a reply



Submit