Pagine

Wednesday, 24 September 2014

Create a splash screen in Android

The splash screen is an activity of presentation, with the aim of showing for example the icon of a company of the application itself.

In this tutorial we show how to create a simple application with a splash screen, in which are displayed the Blogger logo and a toast containing the url of this blog for a certain time interval.

The main files are:

- MainActivity.java, where is defined the splash screen:

public class MainActivity extends ActionBarActivity { 
 // Splash screen timer
 private static int TIME_OUT = 8000;

 @Override
 protected void onCreate(Bundle savedInstanceState) {   
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main);

  //layout of the toast
  LayoutInflater myInflator = getLayoutInflater();
  View toastLayout = myInflator.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.toast) );
  int duration = Toast.LENGTH_LONG;

  //toast
  Toast toast = new Toast(getApplicationContext()); toast.setDuration(duration); 
  toast.setView(toastLayout); 
  toast.setGravity(Gravity.BOTTOM, 0, 80);

  //delay for the toast
  try
   Thread.sleep(1000);
  }
  catch(InterruptedException ex) { 
   Thread.currentThread().interrupt();
  } toast.show();

  //shows company logo
  new Handler().postDelayed(new Runnable() {
  
  @Override
  public void run() {
  //passage to the second activity after the time interval
  Intent i = new Intent(MainActivity.this, SecondActivity.class);
  startActivity(i);
  finish(); }
  }, TIME_OUT); 
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) { 
  getMenuInflater().inflate(R.menu.main, menu); 
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) { 
  int id = item.getItemId();
  if (id == R.id.action_settings) {

   return true
  }
 return super.onOptionsItemSelected(item); 
 }

- activity_main.xml, where is defined its layout:

<?xml version="1.0" encoding="utf-8"?>
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/ android"

  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical"   
  android:background="@android:color/white" >

  <ImageView
    android:id="@+id/imageView1" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/logo" />

</RelativeLayout



- custom_toast.xml, where is defined the toast:

<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/ android"

  android:id="@+id/toast" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:background="@android:color/white" 
  android:orientation="vertical" >

  <TextView
    android:id="@+id/textUsername" 

    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/site" 
    android:textColor="@android:color/darker_gray" 
    android:textSize="25sp" 
    android:textStyle="bold" />

</LinearLayout

The final result is the following:


 You can download the complete example from our repository: Download SplashScreen.zip




No comments:

Post a Comment