I want to be able to change the background color of a button when pressed, then change it back half a second later. I have tried many things, but for some reason, most don't work.
For example, Thread.sleep(500); gives me an error, SystemClock.sleep(500); pauses the UI, and for some reason I cannot make a handler using Handler mHandler = new Hander(); No solution that I have found has worked so far.
You should never pause or sleep the main / UI thread!
What you will want to do is add code that runs in the background to change your button color. There are many ways to do this:
Handler
AsyncTask
POTs (Plain Old Threads)
If you do it by trying to pause the UI, you are also pausing everything else on the screen which is not what you want.
Related
I'm trying to change the imageview 3 times in a short period of time.
I want to set the image, have sheldon say "Bazinga", then change imageview directly after he the audio is completed. And then I want to do that a second time.
Note that the first image is already set before calling this function:
sound = MediaPlayer.create(this, R.raw.bazinga);
sound.start();
page.setImageResource(R.drawable.counttotwofirst);
while(sound.isPlaying()){}
sound.start();
page.setImageResource(R.drawable.counttotwosecond);
while(sound.isPlaying()){}
sound.release();
page.setClickable(true);
I've tried to use invalidate(); but that didn't work.
Is there anyway to force update or at least wait until the imageview has been updated?
How long time is there between updates if imageviews?
Is there some other view I should use instead?
You need to re-think the design a bit. I am guessing you are running all of that code on the main (UI) thread of your app. The problem is that although you are changing the image resource, you are then causing the UI thread to block (sitting in a while() loop), so the UI updates don't get performed until after your entire function returns. You probably need some more pieces to achieve the sound/animation effects you want. Off the top of my head, probably create a background thread for performing the sound loading/playback so that's not done on the UI thread. Also, instead of sitting in a while() loop until the sound finishes, it's better to have a callback notify you that playback has completed (take a look at MediaPlayer::setOnCompletionListener()).
Well I just want to press a button and a countdown appears in it however when I press the button the program stops and finally shows the number 1 but doesn't show 3 or 2.
btnTurno.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
btnTurno.setText("3");
SystemClock.sleep(1000);
btnTurno.setText("2");
SystemClock.sleep(1000);
btnTurno.setText("1");
}
});
What I'm doing wrong?
First of all, Sleep should be called as Thread.sleep(x);
But on the other hand , sleep is NOT recommended since it will block the user interaction with the application.
If you want to make a countdown ( as it looks like what you are doing ), you should look at this
http://developer.android.com/reference/android/os/CountDownTimer.html
onClick is being executed in single handler message on GUI thread, so each time you change text, it overwrites previous value, so system is not able to redraw view.
Also you should not call sleep() on main gui thread, that will cause ANR - application is notresponding
Setting the text and drawing the text are separate operations that both happen on the same thread. You're setting the text three times before the framework gets the chance to draw anything, so all you see is the last change.
For this to work, you'd need to do your text updates and sleeps on a separate thread. However, you can't make UI calls directly on a different thread, so you need to post your messages.
One approach would be to create an AsyncTask, and use your code above but call publishProgress() where you're currently calling setText(). The publishProgress() call in the async thread results in onProgressUpdate() running in the UI thread; from there, you can call setText() to update the UI.
Edit: the CountDownTimer approach mentioned in a different answer is better.
I want to change the background of a button to red and then wait for one second before calling another activity.
This is my code:
btn1.setBackgroundColor(Color.RED);
SystemClock.sleep(1000);
startActivity(intent);
The problem is that the application sleeps for one second and starts the activity, but the color of the button does not change. How can I fix this?
When you use
SystemClock.sleep(1000);
your Main thread which handles the Looper gets Sleep.
And then when its returns it first change the color and then start the Activity. which are done one after the other without delay, so u are not able to see the changed color.
Use Handler postDelayed which will help u to run the activity after the delay u need and which also not block the Main Looper Thread by sleep
No it is setting Color but you are not able to see that. I will explain why you are not able to see.
The color is setting after 1 second. But you are starting new activity after 1 second, so you are not able to see the change of color. Actually the sleep paused the thread for given time.
To notice this effect, try below code.
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.RED);
SystemClock.sleep(5000); // color will set after 5 seconds
}
});
I don't know how to overcome this problem. I answered just to inform this.
You are setting the color on the same thread that is sleeping so your changes are not visible because the sleep command causes the UI to freeze.
You should set the color and then spawn a new thread that will wait 5 seconds before launching your other activity.
hey guys
I am trying to make a media player in which there is a seekbar.
Seekbar is running with a thread ; this thread is a runnable and not another class.
So far i have added the seekbar and now i want to stop the seekbar when pause button is pressed but there is nothing like :-
myThread.stop or myThread.pause??
Also i have to run one thread in background since if the user comes back to "now Playing"
activity he can see the seekbar at correct position
Thread is holding me back
Thanks
pause and stop and all those other methods don't exist any more (for very good reasons).
what you want to do is let your thread sit in a loop and do whatever it is you want it to do. Put in some settable flags that you can control from outside that will let the thread exit when you're done with it.
Keep in mind that threads aren't re-usable. Once you have run a thread you can't run it again.
Also, the seek bar can be set to the correct position when the user returns to your activity (I assume you'll keep the music playing in the background). There's no need to waste resources updating the seekbar's position while the activity is not in display.
What about using the buffering update callback provided by MediaPlayer?
It doesn't seem reasonable to me to have a thread running in the background to do this.
In my activity there's some stuff going on in a background thread, which gets started in Activity_1. The processing of the background thread takes a while and I want to notify the user when it's completed via an AlertDialog. However, the user might have changed to Activity_2 or Activity_3 in the meantime and I would like to pop up the AlertDialog always in the current Activity.
Any idea how to realize this?
I ended up doing something like this in my background thread. It works, but not sure if it is a "good" solution.
Looper.prepare();
mActivity.showDialogAlertDefault();
Looper.loop();
Looper.myLooper().quit();
This probably means the right way is to use a Service, instead of a plain background thread. Your background work will be more resilient to being paused or shut down, and you don't have to worry about making an AsyncTask that is constantly keeping track of a different Activity parent.