How to display different image after another with Glide? - java

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);
}

Related

Why is there no delay on Handler?

I have created a handler for an alert that should activate for 4 seconds, stops for 4 seconds, and activates again. When i put it in the if statement, it doesn't work; the alert keeps playing, stops for less than a second and continues activating again without the delay. Wonder if anyone knows why is it happening and what should i do to correct it. Thank you.
private Handler handler2 = new Handler();
private Runnable startalert = new Runnable() {
#Override
public void run() {
alert2.start();
handler2.postDelayed(this, 4000);
}
};
#Override
public void onLocationChanged(Location location) {
if (location == null) {
speedo.setText("-.- km/h");
}
else {
currentSpeed = location.getSpeed() * 1.85f; //Knots to kmh conversion.
speedo.setText(Math.round(currentSpeed) + " km/h");
}
if (currentSpeed <=4.99) {
background.setBackgroundColor(Color.GREEN);
handler2.removeCallbacks(startalert);
} else if(currentSpeed >=5.00 && currentSpeed <=9.99) {
background.setBackgroundColor(Color.YELLOW);
handler2.removeCallbacks(startalert);
} else if(currentSpeed >=10.00) {
background.setBackgroundColor(Color.RED);
startalert.run();
}
}
Instead of 'this', use runnable object.
private Runnable startalert = new Runnable() {
#Override
public void run() {
alert2.start();
handler2.postDelayed(startalert, 4000);
}
};
Another method:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
alert2.start();
handler.postDelayed(this, 4000);
}
}, 4000);

Android - Nested Handler / Runnable

My application content part had too much code. There were about 3000 lines of XML code. This caused my application to startup slowly. (launch in about 8 seconds) I placed the content in 6 viewstub objects. and I created a lot of handlers. Is it a problem? Is it hierarchically correct? How can I do all these handler operations asynctask.
Also how can I make my content lighter and faster.
Thanks in advance!
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
viewStubPager.setLayoutResource(R.layout.viewstubpager);
coachStubPager = viewStubPager.inflate();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
viewStub1.setLayoutResource(R.layout.viewstub1);
coachStub1 = viewStub1.inflate();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
viewStub2.setLayoutResource(R.layout.viewstub2);
coachStub2 = viewStub2.inflate();
viewStub3.setLayoutResource(R.layout.viewstub3);
coachStub3 = viewStub3.inflate();
viewStub4.setLayoutResource(R.layout.viewstub4);
coachStub4 = viewStub4.inflate();
viewStub5.setLayoutResource(R.layout.viewstub5);
coachStub5 = viewStub5.inflate();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Objects.requireNonNull(notificationManager).cancelAll();
sharedPreferencesKeys();
initialize();
calculate();
sharedPrefStartup();
alertDialogClickListener();
changeListener();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
layouts = new int[]{R.layout.vki_slide1, R.layout.vki_slide2, R.layout.vki_slide3, R.layout.vki_slide4, R.layout.vki_slide5, R.layout.vki_slide6, R.layout.vki_slide7, R.layout.vki_slide8, R.layout.vki_slide9};
VKIPagerAdapter = new MyViewPagerAdapter();
vkipager.setAdapter(VKIPagerAdapter);
VKIPagerAdapter.notifyDataSetChanged();
vkipager.setOffscreenPageLimit(10);
vkipager.addOnPageChangeListener(viewPagerPageChangeListener);
pageIndicator.setCount(layouts.length);
pageIndicator.setSelection(0);
bottombar.setVisibility(View.VISIBLE);
}
}, 100);
}
}, 100);
}
}, 100);
}
}, 100);
}
}, 150);
It's a bit late, but this might help others...
I don't know if the code in the question will start faster with my proposed solution (I doubt this will be the case), however, it's way more readable and uses only one Runnable. This can also be used for animations.
I derived this example from the following answer:
https://stackoverflow.com/a/11198037/6423246
Handler mHandler = new Handler();
int inflater = YOUR_CONSTANT_1;
void yourFunction() {
// ...your first inflater code here...
mHandler.postDelayed(mRunnable, your_delay_in_millis);
}
Runnable mRunnable = new Runnable() {
#Override
public void run() {
switch(inflater) {
case YOUR_CONSTANT_1 : {
// ...your second inflater code here...
inflater = YOUR_CONSTANT_2;
mHandler.postDelayed(mRunnable, your_delay_in_millis);
break;
}
case YOUR_CONSTANT_2 : {
// ...your third inflater code here...
inflater = YOUR_CONSTANT_3;
mHandler.postDelayed(mRunnable, your_delay_in_millis);
break;
}
// etcetera
case YOUR_CONSTANT_LAST : {
// ...your last inflater code here...
// in your final case, you could opt to remove callbacks
mHandler.removeCallbacks(mRunnable);
break;
}
}
}
};

I am using handler for delay in android but it's not working

I want a delay for two seconds. and every 2 seconds I want to change the text, and for that, I am using handler like this, but it's not working it's only showing hello. it's not changing at all it only shows what I write second. The code is like this,
private Handler handler = new Handler();
int i=5;
private TextView textView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.hello);
textView.setText("Android Things !!");
hello_first.run();
}
private Runnable hello_first = new Runnable() {
#Override
public void run() {
textView.setText("Nice Work");
handler.postDelayed(this,5000);
textView.setText("Hello");
handler.postDelayed(this,2000);
i = i+1;
if(i==5)
handler.removeCallbacks(this);
}
};
You are using postDelayed incorrectly. It looks like you expect it to work the same way Thread.sleep would work. However that is not the case.
Here is a correct implementation of what you are trying to achieve:
private Handler handler = new Handler();
private TextView textView;
private int i = 0;
private boolean flip;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.hello);
handler.post(hello_first);
}
private Runnable hello_first = new Runnable() {
#Override
public void run() {
if(++i == 5) {
handler.removeCallbacks(this);
return;
}
if(flip) {
textView.setText("Nice Work");
} else {
textView.setText("Hello");
}
flip = !flip;
handler.postDelayed(this, TimeUnit.SECONDS.toMillis(2));
}
};
I Hope this will work for you.
Handler handler = new Handler();
Runnable task = new Runnable() {
#Override
public void run() {
textView.setText("Nice Work");
handler.postDelayed(this,2000);
textView.setText("Hello");
handler.postDelayed(this,2000);
}
};
task.run();
For Stopping task
handler.removeCallbacks(task);
Its easy to use like
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText("Hello");
},2000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText("Nice Work");},5000);
A Handler allows you to send and process Message and Runnable objects
associated with a thread's MessageQueue.
Rectify your postDelayed method.
Causes the Runnable r to be added to the message queue, to be run
after the specified amount of time elapses.
DEMO STRUCTURE
textView.setText("Nice Work");
final Handler handlerOBJ = new Handler();
handlerOBJ.postDelayed(new Runnable() {
#Override
public void run() {
// YOUR WORK
textView.setText("Hello");
}
}, 5000); // 5S delay
You can Log and see what happened actually...
Every time you call handler.postDelayed(this, 5000);
it will create two Runnable instance and send them to the handler. So the number of runnable in the queue increase very quickly.
You can set a text list and index, and then throw the runnable to the handler and postDealyed as 2000 milliseconds. Use the text list and index to see what text should be set to the textview.
try this code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.hello);
textView.setText("Android Things !!");
handler.postDelayed(hello_first,5000);
handler.postDelayed(hello_second,2000);
}
private Runnable hello_first = new Runnable() {
#Override
public void run() {
textView.setText("Nice Work");
}
};
private Runnable hello_second = new Runnable() {
#Override
public void run() {
textView.setText("Hello");
}
};

Pause the changing Images when Audio paused

I am building an application in which images starts changing when audio is played.
When audio is play (starts) onclick button, the images also start changing with different delays and i can pause my audio as well. The problem which i am facing and getting confuse is that, when i paused my audio,
How can i pause my images as well ,so that when i click the button again,my audio resumes and images starts changing next from where they were paused.
private Button btn_play;
MediaPlayer mp;
int duration;
private ImageView imageView;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_names);
int resID = getResources().getIdentifier("audio", "raw", getPackageName());
mp = MediaPlayer.create(NamesOfALLAH.this, resID);
duration = mp.getDuration();
btn_play = (Button) findViewById(R.id.btn_play);
imageView = (ImageView) findViewById(R.id.imageView_Names);
btn_play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mp.isPlaying()) {
mp.pause();
btn_play.setText("Play");
} else {
mp.start();
btn_play.setText("Pause");
changeImage1();
changeImage2();
changeImage3();
changeImage4();
changeImage5();
}
}
});
}
public void changeImage1() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.a1);
}
}, 6000);
}
public void changeImage2() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.a2);
}
}, 8000);
}
public void changeImage3() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.a3);
}
}, 10000);
}
public void changeImage4() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.a4);
}
}, 11000);
}
public void changeImage5() {
handler.postDelayed(new Runnable() {
#Override
public void run() {
imageView.setImageResource(R.drawable.a5);
}
}, 12000);
}
`
Use an ImageSwitcher, it does some of the work for you. In any case, if you want to create the animation yourself, you must consider all the changes as a whole. For example, you can repost the runnable to the handler. Pseudo-code:
handler.postDelayed(new Runnable() {
#Override
public void run() {
// set the image you want
if (mustContinue)
handler.postDelayed(this, time);
}
}, 10000);
Toggle the boolean mustContinue every time you press the play/pause button.
EDIT:
Check this as an example.

Show x Imageviews sequentially for y seconds

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!

Categories

Resources