Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Change the Image View Source at run time after few seconds

Tags:

android

The following code is for a splash screen in my app.What I need to be done is when the activity loads the image should be displayed as default and then after few seconds the image should be changed to another in the same activity. I have another image like the first one with different color. I wanted to change the screen for that image after few seconds.

I have done it like this 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 the program stops by executing in the thread saying...

"android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views."

How I can achieve this target ? Does anybody please help me to sort out this problem. I am new to android development..

Thanks.

like image 218
Ruchira Chamara Avatar asked Nov 14 '12 06:11

Ruchira Chamara


2 Answers

You should use the ImageView's Handler and a Runnable. Handler's are schedulers specific to Android and they can run on the UI thread. Try this:

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

And inside onCreate() call:

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

Understand that splash screens are frowned upon because you should focus on starting the app as soon as possible (while loading the slower element in the background). However sometimes a slight delay is unavoidable.

like image 97
Sam Avatar answered Sep 20 '22 13:09

Sam


Write this inside finally

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

                }
            });

You should always update your UI from the UI Thread itself...

like image 23
anz Avatar answered Sep 19 '22 13:09

anz