The ModMyTM Family of Sites.
   
Go Back   Android and G1 Forums, Mods, Hacks, News, Downloads, Skins, Themes, and more! | ModMyGphone > ModMyGPhone Wiki
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Splashscreen

From ModMyGPhone Wiki

Image:Splashscreen.png


The basic logic is to use a thread to do all the background work while the image is being displayed.


public class Splashscreen extends Activity

{

   ImageView splash;

   final Handler mHandler = new Handler();


   public void onCreate(Bundle icicle)
   {
       super.onCreate(icicle);

       setContentView(R.layout.main);

       splash = (ImageView) findViewById(R.id.splash);

       Thread t = new Thread()
       {
            public void run()
            {
                mHandler.postDelayed(update, 4000);
            }


       };
       t.start();
   }


   


  • mHandler is used to send the runnable when our data processing is done.



  
    final Runnable update = new Runnable() 
    {
        public void run() 
        {
           splash.setVisibility(View.GONE);
        }
    };
}


  • The update Runnable object will run when it receives the runnable from mHandler.


  • The above code takes-off the image after 4 seconds. But in most conditions, you would want to remove it as soon as the processing is done. For this, replace the postDelayed message with,
mHandler.post(update);
  • In this case, if your application requires less processing during startup, the splashscreen will not be visible.



Alternate Method

  • In the above code we sent a Runnable object to hide the image. We can also send a message(android.os.Message).
private Handler mhandler = new Handler() 
{

        public void handleMessage(Message msg)
        {
            splash.setVisibility(View.GONE);
            super.handleMessage(msg);
        }

};


  • In this case, we have to define mHandler and override the method handleMessage(Message). We won't need the Runnable object now.



mHandler.sendMessageDelayed(msg, 4000);


  • The post/postDelayed method will be replaced by the sendMessage/sendMessageDelayed method.


So, our new source-code will be,

public class Splashscreen extends Activity 
{
    ImageView splash;
       
    public void onCreate(Bundle icicle) 
    {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        splash = (ImageView) findViewById(R.id.splash);
        
        Thread t = new Thread() 
	{
            public void run() 
            {
            	Message msg= new Message();
            	
            	mHandler.sendMessageDelayed(msg, 4000);
            	
            	
            }
      
	};
	t.start();
    }
       
    private Handler mHandler = new Handler() 
    {
    
            @Override
    
            public void handleMessage(Message msg) 
            {
    
                splash.setVisibility(View.INVISIBLE);
                super.handleMessage(msg);
            }
    
    };

}


_______________


Download Source

_______________


All times are GMT -6. The time now is 12:30 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Powered by vbWiki Pro 1.3 RC5. Copyright ©2006-2007, NuHit, LLC
Copyright © 2007-08, ModMy LLC