Reputation: 23
I want the code that allows freedom of duration of the toast.
also the location of a toast. i tried using handlers but im not completely sure how it works so that was a failure from my side.
so any better ideas?
Upvotes: 2
Views: 66
Reputation: 162
public void CustomToast(Context context, String msg, long timeinmillisec) {
// TODO Auto-generated constructor stub
final Toast toast = Toast.makeText(context, ""+msg, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
CountDownTimer countDownTimer = new CountDownTimer(timeinmillisec, 1000) {
private boolean isShowing = false;
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
if (isShowing) {
toast.cancel();
} else {
toast.show();
}
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
};
countDownTimer.start();
}
Upvotes: 3
Reputation: 4746
As for the location, you can call the following method of Toast class.
setGravity (int gravity, int xOffset, int yOffset)
You can only set the duration to one of the following constants LENGTH_SHORT and LENGTH_LONG
Upvotes: 1