I have an array of ImageView with a number of invisible elements that I would like to make visible sequentially (i.e. make the first one visible, wait for half a second, make the next one visible, and so on) using either Thread or runOnUiThread.
I have tried the following:
Thread th = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
th.start();
and
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
But I don't get the desired effect. What happens is that all images are made visible at the same time instead of one after the other.
How can I solve this?
int counter=1; //Global variable
private void visibleImageview()
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(counter==1)
{
im1.setVisibility(View.VISIBLE);
}
else if(counter==2)
{
im2.setVisibility(View.VISIBLE);
}
else if(counter==3)
{
im3.setVisibility(View.VISIBLE);
}
++counter;
if(couter<=3)
visibleImageview();
}
}, 3000);
}
Solved it. This is how I managed to do it:
Handler handler = new Handler();
im1.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im2.setVisibility(View.VISIBLE);
}
});
}
},500);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im3.setVisibility(View.VISIBLE);
}
});
}
},1000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},1500);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},2000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
bottom.setVisibility(View.VISIBLE);
}
});
}
},3000);
Related
How can I add timed functionality that doesn't end unless the button is clicked again?
This runs a thread okay but it won't restart unless I go to another tab in the app and go back to it.
metroStartBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
metronomeOn = !metronomeOn;
if(metronomeOn)
{
final Thread t = new Thread(new Runnable()
{
#Override
public void run()
{
while(!toExit){
// Your code
try {
playSound(getContext(), 1);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
else
{
toExit = true;
}
}
});
Because after the first run , toExit is true therefore your while loop will not execute after that
metroStartBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
metronomeOn = !metronomeOn;
if(metronomeOn)
{
final Thread t = new Thread(new Runnable()
{
#Override
public void run()
{
while(metronomeOn){
// Your code
try {
playSound(getContext(), 1);
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
t.start();
}
}
});
In my fragment, I have an AlertDialog and a Bluetooth connection manager. I want to update the AlertDialog with the new states of the Bluetooth connection process, so I used the runOnUiThread(...) method:
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
interactor = new BluetoothInteractor(getActivity(), new OnBluetoothStatusChangedListener() {
#Override
public void OnConnectionStopped() {
alertDialog.setMessage("Disconnected.");
}
#Override
public void OnConnectionStopping() {
alertDialog.setMessage("Stopping connection...");
}
#Override
public void OnConnectionStarting() {
alertDialog.setMessage("Connecting to device...");
}
#Override
public void OnConnectionStarted() {
alertDialog.setMessage("Streaming data...");
}
});
}
});
The first time I update the AlertDialog message (OnConnectionStarting event) everything works fine, but the second time I got android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
What could be happening here?
Replace with
interactor = new BluetoothInteractor(getActivity(), new OnBluetoothStatusChangedListener() {
#Override
public void OnConnectionStopped() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Disconnected.");
}
});
}
#Override
public void OnConnectionStopping() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Stopping connection...");
}
});
}
#Override
public void OnConnectionStarting() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Connecting to device...");
}
});
}
#Override
public void OnConnectionStarted() {
getActivity().runOnUiThread(new Runnable() {
#Override
void run() {
alertDialog.setMessage("Streaming data...");
}
});
}
});
I am trying to update the progressbar, but it is showing a black screen whenever I use progressbar. As soon as I comment the part of progressbar increment, my code works fine. Could someone please help me out with what am I doing wrong?
new Runnable() {
#Override
public void run() {
for(int i = 0; i < timeout; i++) {
handler.post(new Runnable() {
#Override
public void run() {
progressbar.incrementProgressBy(1);
}
});
try {
Thread.sleep(1000);
}
catch(Exception e) {}
}
}
}.run();
using thread you can do like this...
public void loadProgressbar()
{
progressBar.setMax(100);
progressBar.setProgress(0);
new Thread(new Runnable() {
#Override
public void run() {
for (progress = 0; progress <= 100; progress++) {
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progress);
}
});
try {
Thread.sleep(50);
} catch (Exception ex) {
}
}
}
}).start();
}
Just use this code on your main thread .
new CountDownTimer(3000, 300) {
#Override
public void onTick(long l) {
progress = progress + 10;
pb.setProgress(progress);
}
#Override
public void onFinish() {
pb.setProgress(100);
}
}.start();
Is there a slicker/simpler way of doing the following? I have a method in a class that shows a progressbar while a thread runs. This code works but it just seems a little overly clunky having 3 steps.
private void pause() {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
//do stuff
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
}
This does not show a progress bar while a thread runs.
Your thread can run 10 seconds but the visibility of the ProgressBar will only blink if you can see it at all.
Instead, you should hide only once the thread has completed, so this would be correct:
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
//do stuff
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
For a "slicker" way, you could use an AsyncTask which was created for this very task. Example:
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Guaranteed to run on the UI thread
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params) {
// Do stuff
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// Guaranteed to run on the UI thread
mProgressBar.setVisibility(View.GONE);
}
}.execute();
Here you can use the handlers concept to communicate with UI Thread.
I.e,
Handler handler=new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 1:
mProgressBar.setVisibility(View.VISIBLE);
break;
case 2:
mProgressBar.setVisibility(View.GONE);
break;
}
}
}
new Thread(new Runnable() {
#Override
public void run() {
Message processStart = handler.obtainMessage(1);
processStart.sendToTarget();
try {
//do stuff
} catch (InterruptedException e) {
e.printStackTrace();
}
Message processStart = handler.obtainMessage(2);
processStart.sendToTarget();
}
}).start();
I hope this one will helps you :)
I want to turn on/off the flash light in infinite loop, so when it turned on it should wait for 5 seconds and then turned off then wait 5 seconds to turned on again, and so on...
how I can do that?
here is my code:
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// num = Integer.parseInt(n.getText().toString());
while(bl){
if(camera == null){
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
turnOn();
}
});
}
}).start();
}
else{
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
turnOff();
}
});
}
}).start();
}
}
}
});
I would recommend not using Threads in order to achieve this. Why not use the Runnable class and post it with a delay via a Handler? For example:
Handler handler = new Handler(); // make this a member variable of your class
boolean isOn = false; // make this a member variable of your class
final Runnable flashRunnable = new Runnable() {
#Override
public void run() {
if (isOn) {
turnOff();
isOn = false;
} else {
turnOn();
isOn = true;
}
handler.postDelayed(flashRunnable, 5000);
}
};
handler.postDelayed(flashRunnable, 5000);
If you need to run the code inside the Runnable on the UI thread, you even call postDelayed on a View instead of creating a Handler
Try something like so, using Executors instead of (ugly) Thread.sleep():
boolean cameraOn = true
final Runnable runnable = new Runnable() {
#Override
public void run() {
// your logic here:
// if (cameraOn) ...
// else ...
// cameraOn = !cameraOn
}
};
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
#Override
public void run() {
runnable.run();
}
}, 5, TimeUnit.SECONDS);