Changing the background of a button and sleep - android

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.

Related

How do you pause code in android studio?

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.

sleep in android doesn't work

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.

How to set timer when acitivity changes?

Actually i just want to know whether can we set timer when the activity changes.
Detail description:
startActivity(new Intent(this,Second.class));
I know that by using this code we can change one activity to another activity. By the above code when the activity is changing it changes quickly as know to everyone, but what i want is in button click event when i write this code, when i click that button the same activity needs to be on screen for some particular time (i.e., around 10 seconds) & after that it needs to change activity. I thought of keeping timer here but i didn't got any idea how to do that.
Can anyone please help me with this.
The easiest thing would be to simply create a Handler and post a message to it 10 seconds later.
Handler activityChanger = new Handler();
activityChanger.postDelayed(new Runnable(){
startActivity(new Intent(this,Second.class));
}, 10000);
Put that where you normally create your activity within the scope of startActivity. You're current activity should run for 10 more seconds and the new one starts up.

Programmatically 'press' button every 60 seconds

In my app, I've been asked to add an auto update function. What I'm trying to do is have a timer event so that if the user hasn't pressed the update button in the last 60 seconds, do a 'refreshButton.performClick();'.
I've been searching but I can't find an example where the timer interacts with the UI. I keep getting errors like 'only the original thread that created a view hierarchy'.
If the user does press the button, I want to reset the timer.
Is this even possible?
You only can interact with aView from the UI thread.
You can make something like this in your timer task;
Define this variable in your class:
Handler handler = new Handler();
In your timer task call:
handler.post(new Runnable(){
public void run(){
refreshButton.performClick();
}
});
I think you are going in the wrong direction. Instead of trying to press the button, just set a timer to call the same method that is called once the button is pressed, it will make it way simpler, and will not involve the UI for no reason.
You can use AlaramManager class and a background service to run your code on every 60 second.
Here is an example see AlarmService_Service.java

Changing button background dynamically in thread in Android

I'm developing my first app for Android and its supposed to be a game. Everything is fine but there's one thing I just can't wrap my head around.
In my game's "main" activity (not the first activity which starts when the app starts) I want to have a method which starts a thread which changes a buttons background color/image (go with color because I haven't made any images just yet) for one second then turns it back. I wan't the method to also have an integer parameter which makes it perform this n times. I want to be able to call like changeButtons(5); and it turns button x background blue for 1 second then waits 1 second five times.
So practically I'm trying to make a "main" thread which runs during the game and inside that thread I'm going to run this method whenever certain conditions are true (a thread which calls a thread).
So I have 2 questions. The first one is "Is this possible? " and If so can someone show me how to do it (not all of it of course but help me get started at least)? Especially I want to know if I can change a buttons background color in a thread and if so can someone show me how to write/get me started that thread?
The second question is a follow-up, if you can do this, can you have a like a boolean b which turns to true if someone presses a button and the thread can "notice" that change? For example, if the thread is running and Obama presses button x and b turns "true" in the method OnClick(View v) can I, inside my main thead have an if(b == true){Obama.moon();} and Obama will moon?
Sure you can.
In android you can use the Handler class (example available) to post actions to the event queue. You can do something like this:
final Handler handler = new Handler();
final Runnable updateRunner = new Runnable() {
public void run() {
// we are now in the event handling so to speak,
// so go ahead and update gui, set color of button for instance
}
};
new Thread(new Runnable() {
public void run() {
while (condition) {
SystemClock.sleep(1000);
handler.post(updateRunner);
}
}
}).start();
This will trigger the run in updateRunner each second.
Regarding your follow up, it can be done as well (of course :) ). You can for instance implement an observable pattern to the class that handles the button x. When pressed, notify the observers with something like observers.updateChange(b)
where you previously had a thatClassOverThere.registerObserver(this) in your main thread.

Categories

Resources