Trying to get the following ;
ImageView x is shown for y seconds. Then, x is Invisible again and a different ImageView (z) is shown for y seconds. And so on..
I've got :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.series_onthouden);
hideAllImages();
showImage(3, 2000);
showImage(4, 1000);
}
public void showImage(int color, final int sec) {
Thread thread = new Thread() {
#Override
public void run() {
try {
Thread.sleep(sec);
} catch (InterruptedException e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < myImagebtns.length; i++) {
((ImageView) findViewById(myImagebtns[i]))
.setVisibility(View.INVISIBLE);
}
}
});
}
};
if (!thread.isAlive()){
((ImageView) findViewById(myImagebtns[color])).setVisibility(View.VISIBLE);
thread.start();
}
}
It works for the first color.. But also shows the second simultaniously. The second color should be shown when the first turns invisible (after x seconds).
ty
You could try using a Handler like the example below:
Handler handler = new Handler();
im1.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im1.setVisibility(View.INVISIBLE);
im2.setVisibility(View.VISIBLE);
}
});
}
},500);
where 500 is the delay between events.
Repeat postDelayed calls for as many images as necessary.
Hope this helps!
Related
I want to display an other image after a delay. Here is my code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView=findViewById(R.id.imageView);
ArrayList<String> test = new ArrayList<String>();
test.add("e");
test.add("a");
if (test.get(0) == "e") {
Glide.with(this)
.load("https://something.something/something.jpg")
.into(imageView);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (test.get(1) == "a") {
Glide.with(this)
.load("https://something.something/something2.jpg")
.into(imageView);
}
}
}
But only the 2nd image apppears if i do that. Any solution?
Create a new method
private void loadImage(String imageLink){
Glide.with(this).load(imageLink).into(imageView);
}
now simply call this where you want to load first image and second image in the same imageView
loadImage("imageLink");
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 3sec
loadImage("next ImageLink");
}
}, 3000);
Something like this, you can also modify loadImage method so it can also accept ImageView as a parameter if you want to load images in different ImageView with delay
if (test.get(0) == "e") {
loadImage("https://something.something/something.jpg");
}
if (test.get(1) == "a") {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 3sec (3000 = 3 sec)
loadImage("https://something.something/something2.jpg");
}
}, 3000);
}
Hope this will help!
Edit 1:
if you want to load both pics with delay use
if (test.get(0) == "e") {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 3sec (3000 = 3 sec)
loadImage("https://something.something/something.jpg"); // image 1
}
}, 3000);
}
if (test.get(1) == "a") {
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 3sec (3000 = 3 sec)
loadImage("https://something.something/something2.jpg"); // image 2
}
}, 3000);
}
After click button I would like to change its color, then wait one second and change its color back.
This is my code:
public void click(final View view) throws InterruptedException {
final Button btn = findViewById(view.getId());
btn.setBackgroundColor(Color.parseColor("#0000ff"));
btn.setClickable(false);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
t.join();
btn.setBackgroundColor(Color.parseColor("#e2e2e2"));
btn.setClickable(true);
}
It doesn't work. I've checked it with more complex code and debugger and it looks like all UI changes are made collectively after finish this function.
I've found this thread: apply ui changes immediately and tried to put setBackgroundColor() and setClickable() into runOnUiThread function:
runOnUiThread(new Runnable() {
#Override
public void run() {
btn.setBackgroundColor(Color.parseColor("#0000ff"));
btn.setClickable(false);
}
});
But it also doesn't work. What should I do?
Something like this :
private final Handler handler = new Handler();
public void click(final View view) {
view.setBackgroundColor(Color.parseColor("#0000ff"));
view.setClickable(false);
handler.postDelayed(() -> {
view.setBackgroundColor(Color.parseColor("#e2e2e2"));
view.setClickable(true);
}, 1000);
}
#Override protected void onPause() {
super.onPause();
handler.removeCallbacks(null);
}
The question is not very clear. However, I am trying to summarize the question that I have understood from your question.
You are trying to set a button's background color on clicking on it and change it back after some time. If this is the situation, then I think your idea of how threads work is wanting.
In your code, the button will change the color immediately as the sleep that you are using is running in another thread (other than UI thread). The code is executed correctly, however, you cannot see the effect of the Thread.sleep as its running in a separate thread.
So all you need to do here is to change the background color again inside the thread. Modify your code like the following.
public void click(final View view) throws InterruptedException {
final Button btn = findViewById(view.getId());
btn.setBackgroundColor(Color.parseColor("#0000ff"));
btn.setClickable(false);
Thread t = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1000);
btn.setBackgroundColor(Color.parseColor("#e2e2e2"));
btn.setClickable(true);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
}
This should work.
I have created a demo trying to show what the code will do.
However, using Handler in case of updating UI elements in this specific case is recommended. Please see the comments below.
public void click(final View view) throws InterruptedException {
final Button btn = findViewById(view.getId());
btn.setBackgroundColor(Color.parseColor("#0000ff"));
btn.setClickable(false);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn.setBackgroundColor(Color.parseColor("#e2e2e2"));
btn.setClickable(true);
}
}, 1000);
}
Not sure why that wouldn't work, but I've done something similar with
delayHandler = new Handler();
delayHandler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//change stuff on ui
}
});
}
}, 1000);
if that doesn't work the only other functional difference in my code is that instead of btn being a final Button it's a private global variable in my activity.
Hope the following code will help :
btn.setBackgroundColor(Color.RED); // color you want for a second
new CountDownTimer(1000, 1000) {
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
btn.setBackgroundColor(Color.BLUE); //to change back color to prior state
}
}.start();
Try this,i think it's work for you..
final Button bPdf = findViewById(R.id.pdf);
bPdf.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
bPdf.setBackgroundColor(Color.parseColor("#0000ff"));
new CountDownTimer(1000, 50) {
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
bPdf.setBackgroundColor(Color.parseColor("#e2e2e2"));
}
}.start();
}
});
I want to toast (or show a dialog) when progressbar ends. I tried to do this with if in run() method, but it causes the following error:
Can't create handler inside thread that has not called Looper.prepare()
how can I do that?
This is my code:
#Override
public void run() {
int myProgress = 0, Speed = 50;
ProgressBar myProgressBar = (ProgressBar) findViewById(R.id.progressbar);
while (myProgress<750){
try{
Thread.sleep(Speed);
myProgress++;
if (myProgressBar != null) {
myProgressBar.setProgress(myProgress);
}
}
catch(Throwable t){ }
}
}
runOnUiThread(new Runnable() {
public void run() {
//your code here
}
});
Change like this.
runOnUiThread(new Runnable() {
public void run() {
int myProgress = 0, Speed = 50;
ProgressBar myProgressBar = (ProgressBar) findViewById(R.id.progressbar);
while (myProgress<750){
try{
Thread.sleep(Speed);
myProgress++;
if (myProgressBar != null) {
myProgressBar.setProgress(myProgress);
}
}
catch(Throwable t){ }
}
}
});
Worker threads are meant for doing background tasks and you can't show anything on UI within a worker thread unless you call method like runOnUiThread. If you try to show anything on UI thread without calling runOnUiThread, there will be a java.lang.RuntimeException.
Tell me if you still face any problem.
I tried this and it works now:
public void run() {
int myProgress = 0, Speed = 50;
ProgressBar myProgressBar = (ProgressBar) findViewById(R.id.progressbar);
while (myProgress<750){
try{
Thread.sleep(Speed);
myProgress++;
if (myProgressBar != null) {
myProgressBar.setProgress(myProgress);
}
}
catch(Throwable t){ }
}
final int finalMyProgress = myProgress;
runOnUiThread(new Runnable() {
#Override
public void run() {
if (finalMyProgress == 750)
Toast.makeText(word_guess2.this, "hi", Toast.LENGTH_SHORT).show();
}
});
}
I'm programming a small android app in Java/eclipse.
In one part of my app i need a thread, as i build in the following way:
protected void onResume() {
super.onResume();
// we're going to simulate real time with thread that append data to the graph
new Thread(new Runnable() {
#Override
public void run() {
// we add 100 new entries
for (int i = 0; i < 100; i++) {
runOnUiThread(new Runnable() {
#Override
public void run() {
addEntry();
}
});
// sleep to slow down the add of entries
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// manage error ...
}
}
}
}).start();
}
Evertything works fine so far. But now i want to start that thread not automatically. I want to handle ".start()" with a button.
How can i realize it?
I'm very new to Java and Android.
Thanks in Advance!
You can use Handler with Runnable instead of your Thread idea, Check out the following code, it server your purpose,
private Handler broadcastHandler;
private Runnable broadcastRunnable;
protected void onResume() {
super.onResume();
broadcastRunnable = new Runnable() {
#Override
public void run() {
// Your UI related operations
runOnUiThread(new Runnable() {
#Override
public void run() {
addEntry();
}
});
// Add some delay
broadcastHandler.postDelayed(broadcastRunnable, 1000);
}
}
public void onButtonClick(View view) {
broadcastHandler.postDelayed(broadcastRunnable, 1000);
}
I am working on this android application:
A Button and a textField, the button click change the text of the textField.
So i want to add some animation to the button click by changing the text character by character and wait 100 millisecondes before adding each character.
But when I run the application and press the button, all the text appear after the sleep time without any animation :p
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
for(int i=0 ; i<str.length() ; i++)
{
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
txt.append(String.valueOf(str.charAt(i)));
}
}
});
you do not want to sleep on the main-thread. Better use postDelayed
you block ui thread now and you only see the result of onClick. you should not call sleep in ui thread. this is not best, but it should work
#Override
public void onClick(View arg0) {
final Handler handler = new Handler();
new Thread() {
public void run() {
for (int i = 0; i < str.length(); i++) {
final int _i = i;
try {
Thread.sleep(100);
} catch (Exception e) {
}
handler.post(new Runnable() {
public void run() {
txt.append(String.valueOf(str.charAt(_i)));
}
});
}
}
}.start();
}