Android Change the image source to start up time after a few seconds.

The following code is for the splash screen in my application. What I need to do is when the activity loads, the image should be displayed by default, and then after a few seconds the image should be changed to another in the same action. I have a different image, similar to the first, with a different color. I wanted to change the screen for this image in a few seconds.

I did it as in my code.

package com.ruchira.busguru; import android.media.MediaPlayer; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.RelativeLayout; public class SplashScreen extends Activity { ImageView imgBus; MediaPlayer introSound, bellSound; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); imgBus = (ImageView) findViewById(R.id.imgBus); imgBus.setImageResource(R.drawable.blackbus); introSound = MediaPlayer.create(SplashScreen.this, R.raw.enginestart); introSound.start(); Thread timer = new Thread(){ public void run(){ try{ sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }finally{ imgBus.setImageResource(R.drawable.bluekbus); } } }; timer.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.splash_screen, menu); return true; } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); introSound.stop(); finish(); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); introSound.stop(); finish(); } } 

but the problem is that the program stops executing in a thread saying ...

"android.view.ViewRoot $ CalledFromWrongThreadException: only the source thread that created the view hierarchy can touch its views."

How can I achieve this? Someone please help me deal with this issue. I am new to Android development.

Thanks.

+6
source share
3 answers

You must use the ImageView and Runnable handler. A handler is Android-specific schedulers, and they can work in the user interface thread. Try the following:

 ImageView imgBus; MediaPlayer introSound, bellSound; Runnable swapImage = new Runnable() { @Override public void run() { imgBus.setImageResource(R.drawable.bluekbus); } }; 

And inside the onCreate() call:

 imgBus = (ImageView) findViewById(R.id.imgBus); imgBus.setImageResource(R.drawable.blackbus); imgBus.postDelayed(swapImage, 3000); // Add me! 

Understand that splash screens are unhappy because you should focus on launching the application as soon as possible (when loading a slower item in the background). However, sometimes a slight delay is inevitable.

+15
source

Write it inside finally

 runOnUiThread(new Runnable() { public void run() { SplashScreen.class.img.setImageResource(R.drawable.bluekbus); } }); 

You should always update your user interface from the user interface thread itself ...

+2
source

Try the following:

We cannot access and update the user interface from workflows, only we can access the user interface from UIThread. If we need to update the background thread, we can use the Handler to do this, as in the following code.

  this.imgBus = (ImageView) findViewById(R.id.splashImageView); imgBus.setImageResource(R.drawable.your_first_image); this.handler = new Handler(); introSound = MediaPlayer.create(SplashTestForSO.this, R.raw.enginestart); introSound.start(); Thread timer = new Thread(){ public void run(){ try{ sleep(2000); YourActivity.this.handler.post(runnable); sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }finally{ } } }; timer.start(); this.runnable = new Runnable() { public void run() { //call the activity method that updates the UI YourActivity.this.imgBus.setImageResource(R.drawable.your_scond_image_view); } }; 
0
source

All Articles