Android button disable till the Toast is seen - android

I have a button which shows a progress dialog, on end of progress dialog, a toast is shown.
I want the button to be diabled when the progress dialog and the toast are seen on the UI. i.e. after the toast is gone i want my button to be enabled again
Can anybody suggest what to do

As soon as you show the toast, set the button clickable to false, and start this timer task. The method of the class Timer namely schedule(), is such that it is executed after the provided time. In this case i passed the time as Toast.LENGTH _SHORT
final Handler handler = new Handler();
Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
your_button.setClickable(true);
}
});
}
},Toast.LENGTH_SHORT); //// If your toast is for length short.

Put the below code before the progress dialog starts
Button myBtn=findViewById(R.id.button1);
myBtn.setVisibility(View.INVISIBLE);
//myBtn.setEnabled(false);
After Toast.makeText() is called, put the below code:
myBtn.setVisibility(View.VISIBLE);
//myBtn.setEnabled(true);
Note that setVisibility will make the button visible/invisible, setEnabled(false) will turn your button to non-clcikable mde.

Related

Wait before the activity is fully displayed

I'm currently having a problem when a button is pressed before the activity is fully displayed. My app is a questionnaire type that spawns buttons as options for the question. If I press the button repeatedly and fast, a black screen will randomly occur but the activity will still display afterwards.
My current solution is to put a delay before I set the event listener to the buttons.
private Runnable task = new Runnable() {
public void run() {
addEventHandlerToButtons();
}
};
And put this in onCreate or onPostCreate
Handler handler = new Handler();
handler.postDelayed(task, 500);

How to wait button clicks for a while in android

I am developing an application for blinds.
I have 4 screen sized buttons (overlapped). Every step of program one button will be clickable and every button has more than one job.
My program starts with a voice (Android TTS engine). Like "please touch screen to do x". After this step I want to wait 3 seconds for button click, if button is not clicked vocalize "please touch screen to do y" and wait 3 seconds again for job y. (x and y is first button's jobs).
Button should do one of them according to touching screen. But how can I wait 3 seconds for button click and continue to vocalize next options and wait 3 seconds again.
If first button is clicked, it will disappear-button 2 will be clickable- and TTS engine will start to vocalize second buttons options. Application will be work like this but I am stuck in waiting button clicks part.
I would advise you to use android.os.Handler instead. In your case you could do something like this:
public void onCreate() {
this.handler = new Handler()
playTheVoiceOfThingX()
viewToTap.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
doThingX();
}
});
handler.postDelayed(new PrepareThingYTask(), 3000);
}
class PrepareThingYTask() implements Runnable {
viewToTap.setOnClickListener(new OnClickListener(){
#Override
public void onClick() {
doThingY();
}
});
handler.postDelayed(new PrepareThingZTask(), 3000);
}
class PrepareThingZTask() implements Runnable {
....
}
A good reminder, the runnable executed by the handler can be executed in UIThread, so no heavy work on it, or create a different looper to run it.
Regards
You could solve your problem with busy wait.
So after you first vocalized "please touch screen..." you would start a background thread which waits for a specific amount of time, like so:
new Thread(new Runnable() {
public void run() {
Thread.sleep(3000);
runOnUiThread(new Runnable() {
#Override
public void run() {
//vocalize
}
});
}
}).start();
As you can see, from within the thread a new runnable is started after 3 seconds which again runs on the UI Thread. This, because I think I remember that you should make such sound-things (depending on your method of how to play the file / sound) only from the UI Thread.
However, this is just an idea and I could not test-run my code!
But I hope I inspired you!
Regards
Me

Cannot hide button using setVisibility and Handler

I am having a problem I've been working for too long now. I'm trying to show a button, and after a delay, hide it.
birdBubble.setVisibility(vis);
final Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
birdBubble.setText("blalb alba");
handler.postDelayed(this, 2000);
birdBubble.setVisibility(invis);
}
});
birdBubble.setVisibility(invis);
i am doing all of this in a AsyncTask because i need to show a sequence of buttons. What it happens is that at the beginning the button is shown and after 2 seconds, the text is changed, but the button doesn't turn INVISIBLE. Any ideas? If you need more code, let me know. Thanks!
set the visilibility like following
birdBubble.setVisibility(View.VISIBLE);
birdBubble.setVisibility(View.INVISIBLE);
birdBubble.setVisibility(View.GONE);

displaying in android a timed custom dialog while pausing main thread execution

I am showing a custom dialog in my application, which stays for a small time, (say 2 seconds) and then disappears. I am calling this custom dialog from the main activity.
I want my calling activity to be paused till the custom dialog disappears. The issue is that activity code after the dialog is dismissed is always executed, while the dialog is being shown.
I have tried showing the dialog, sleeping for 2 seconds and then dismiss it, which is not working. The activity code after the dialog is dismissed is executing.
cust_dlg.show();
Thread.sleep(2000);
cust_dlg.dismiss();
I have also tried putting the dialog in a timer task, which also fails
final Timer t = new Timer();
cust_dlg.show();
t.schedule(new TimerTask() {
public void run() {
cust_dlg.dismiss();
t.cancel();
}
}, 500);
I have also tried simple threading with the custom dialog in a thread and put sleep simultaneously in the main activity, which causes exception and force closes.
Somebody please tell me a way to pause the main activity while the custom dialog is shown, so that the code after the custom dialog dismiss is NOT executed.
Thanks
Try a Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(RBLQuizSpiel.this);
if (aktuellGeloest == true){
builder.setMessage(R.string.quiz_neu)
.setTitle(R.string.quiz_glueckwunsch)
.setPositiveButton(R.string.quiz_ja, dialogClickListener)
.setNegativeButton(R.string.quiz_nein, dialogClickListener).show();
} else {
builder.setMessage(R.string.quiz_neu)
.setTitle(R.string.quiz_schade)
.setPositiveButton(R.string.quiz_ja, dialogClickListener)
.setNegativeButton(R.string.quiz_nein, dialogClickListener).show();
}
}
}, 1500);
no more code behind this.
1500 = ms
Well an you need to set the execution of the following code in the Clicklistner.
Android is event-driven OS. The Dialog will be put in a message qeue and your program steps on..

Android dialog box

Is it possible to give a time limit in dialog box like a toast message. I want to display a set of strings in toast or dialog message box with button option. I used custom toast box previously, but i cant able to insert a button over the toast message. some of my friends suggested to implement dialog box instead of using Toast message. is there any possible to give Time limit in dialog box,(like Toast.long or shot.).
TimerTask would not be a good choice since you can not change the UI thread from TimerTask; Use Handler instead.... You can do this by using handler and runnable... simply use handler to call the runnable after some time. and in runnable simply dismiss the dialoge....
Handler h = new Handler();
h.postDelayed(runnable, delayMillis);
where runnable can be define as:
public Runnable r = new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
}
};
create a dialog, then create a TimerTask and in the run method dismiss/cancel the dialog. Then create a Timer and schedule this task to be run after our desired time
when you call show() method after that you can start a counter after a certain interval when the counter condition is true then you can set the visibility of dialog box is false by calling dismiss() method.

Categories

Resources