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);
Related
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
I have the Problem that my Android app does not delay a second (or 10 seconds), if I use the postDelayed method..
Basically I would like my program to wait one second after I clicked the button, then update the text on my textview ("READY"), wait another 2 seconds, then update the textview again ("SET") and then it should start another activity (not yet implemented :-) ).
With my code, the programm starts and after I click the button the textview shows the last text ("SET") immediately.. It just does not wait.
What am i doing wrong?
Here is my code:
public class MyCounterActivity extends Activity {
private long mInternval = 100000;
private Handler mHandler;
private Runnable mStatusChecker = new Runnable() {
#Override
public void run() {
//updateInterval(); //change interval
startRepeatingTask();
}
};
void startRepeatingTask(){
mHandler.postDelayed(mStatusChecker, mInternval);
//mStatusChecker.run();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gym_counter);
final TextView tv1 = (TextView) findViewById(R.id.fullscreen_content);
final Button startButton = (Button) findViewById(R.id.startbutton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final long up;
EditText textUp = (EditText) findViewById(R.id.editTextUp);
up = Integer.parseInt(textUp.getText().toString());
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
},1000);
Log.d("after 1 runnable", "whaaat");
tv1.setText("Ready");
handler.postDelayed(new Runnable() {
#Override
public void run() {
//
}
}, 2000);
Log.d("after 2nd runnable", "whaaat 2");
//startRepeatingTask();
tv1.setText("SET");
}
});
}
I also tried to run it with the runOnUiThread() (within the onClick(View v) but with with the same result). I expected it to wait 1 second (startRepeatingTask()) and then runs the loop and waits several seconds...
runOnUiThread(new Runnable() {
#Override
public void run() {
startRepeatingTask();
for (int u = 0; u < up; u++){
startRepeatingTask();
}
}
}
});
Hope my description makes sense :-).
Thank you for your help!
EDIT:
I was now able to find a solution for my first problem. The answer from #mad in this post helpded me: How to start a different activity with some delay after pressing a button in android?
(Thats probably the same thing that #laalto tried to tell me. Thanks for the hint!)
In the onClick()
tv1.setText("READY");
mHandler.postDelayed(mDelay1, 2000);
And then the Runnable
private Runnable mDelay1 = new Runnable() {
#Override
public void run() {
if (tv1.getText()=="READY")
tv1.setText("SET");
}
};
BUT:
If i want to refresh the text on my Textview after every second, how do i do that? I cant just call mHandler.postDelayed() several times.. Any help is appreciated.
When you call postDelayed(), it just places the Runnable in a queue and returns immediately. It does not wait for the runnable to be executed.
If you need something to happen after a delay, put the code in the run() method of the runnable.
Whenever you call something like Thread.start(), handler.postDelayed, view.postDelayed, AsynchTask, TimerTask .. you enter the world of threading or you might call it parallel computing.
So there can be multiple threads ("codes") running at the same time.
When you are inside your Activity it is running in a Thread that is calld UI-thread or main thread. All graphics is handled in that thread and that thread alone.
Do NEVER wait in the UI-thread!
Example: you have a button that switches color from say gray to yellow on pressing it. Now you enter a Thread.sleep(10000); - waiting 10 seconds at the start of your onClick.
You will then see that the button stays yellow (=pressed) for 10 seconds even if you only pressed very shortly. Also: if you overdo it android os will become angry and post the user if he wants to force-close your app.
So what happens on handler.postDelayed?
Android will very quickly open a thread that runs in the background parallel to your UI thread. So in some nanoseconds it has done that and will execute the next command in UI thread (in the example above it is Log.d). In the background it will wait and count the millis until time is up. Then any code that is inside the runnable.run method will again be executed in the ui-thread after the wait.
Note also: postDelayed will not be super precise with the wait time as usually the ui-thread is quite buisy and when the wait time is up it may have something else to do. Your runnable code will be added to a queue and executed when ui-thread is ready again. All this happens without you having anything to do about it.
Also:
Remember to work with try/catch inside the runnable.run as many things can happen while waiting - for example user could press Home button closing your app - so the ui-element you wanted to change after the wait could already been destroyed.
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.
I need an advice how to create some animations I want to add in my buttons. Actually I have the animation code, the thing which I need is how to set properly the timing of each one. Here is what I tried already :
fest.setVisibility(View.INVISIBLE);
handler.postDelayed(new Runnable() {
#Override
public void run() {
fest.setVisibility(View.VISIBLE);
fest.startAnimation(anim);
handler.removeCallbacks(this);
}
}, 500);
This is the things which I did for 7 buttons. First I set the visibility to invisible because I want to achieve the effect that they are appearing after 5 miliseconds after onCreate and for every next button I am increasing the delay time with 5 miliseconds so every of them to appear after the previos one. But the problem in this code is that when the next handler starts for the second button for example, the previos button is getting invisible for a part of the seconds and shows again(I hope someone understand what I mean).
Any suggestions for a bette implementation of something like that?
Thanks in advance!
So here is the thing which fixed that problem. I used this for every button and it's working as I want :
final Handler festHandler = new Handler();
festHandler.postDelayed(new Runnable() {
#Override
public void run() {
Animation anim = AnimationUtils.loadAnimation(Menu.this, R.anim.fadein);
fest.setVisibility(View.VISIBLE);
fest.startAnimation(anim);
festHandler.removeCallbacks(this);
}
}, 400);
I am having a problem trying to refresh a View in an Android application. I have a button that have a image and what I need to do is to change the image when someone clicked the button.
Where is the problem? The image don't refresh until the activity finished proccessing the code. Any idea how I can refresh the image as soon as It execute the instruction
buttton1.setBackgroundDrawable(getResources().getDrawable(R.drawable.f1));
Have you considered using the xml side and have the drawables as selectors as then the selectors will get chosen by the particular key/touch event to display the correct graphic..
Try running your method that does your processing from a thread.
ficha1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button bot = (Button) findViewById(R.id.boton1);
bot.setBackgroundDrawable(getResources().getDrawable(R.drawable.f2));
//ficha.setText(fichas.get("boton1").toString());
new Thread(
new Runnable() {
public void run() {
controlJugada(fichas.get("boton1").toString(), bot);
}
}
).start();
}
});
I solved a similar problem by putting the offending code in a post delayed handler with a zero delay.
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// code slowing things down here
}
}, 0);
The first thing you do in the onclick listner is change the backgrount of the button