Top Banner Advertisement

New Post

Rss

Monday, May 19, 2014
Android Tutorial - Add Splash Screen

Android Tutorial - Add Splash Screen


Android Tutorial - Add Splash Screen
Android splash screen are normally used to show user some kind of progress before the app loads completely. Some people uses splash screen just to show case their app / company logo for a couple of second. Unfortunately in android we don’t have any inbuilt mechanism to show splash screen compared to iOS. In this tutorial we are going to learn how to implement splash screen in your android application.

Android Splash Screen Using Timer

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)
2. For Splash Screen we are creating a separate activity. Create a new class in your package and name it as SplashScreen.java
3. Open your your AndroidManifest.xml file and make your splash screen activity as Launcher activity.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="info.androidhive.androidsplashscreentimer"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk
  android:minSdkVersion="8"
  android:targetSdkVersion="17" />

  <application
  android:allowBackup="true"
  android:icon="@drawable/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/AppTheme" >

  <!-- Splash screen -->

 <activity
   android:name="info.androidhive.androidsplashscreentimer.SplashScreen"
   android:label="@string/app_name"
   android:screenOrientation="portrait"
   android:theme="@android:style/Theme.Black.NoTitleBar" >
  
 <intent-filter>
   <action android:name="android.intent.action.MAIN" />
   <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

 </activity>

  <!-- Main activity -->
  <activity
   android:name="info.androidhive.androidsplashscreentimer.MainActivity"
   android:label="@string/app_name" >
  </activity>

  </application>

</manifest>
4. Create a layout file for splash screen under res ⇒ layout folder. I named the layout file as activity_splash.xml. This layout normally contains your app logo or company logo.
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/gradient_background" >


  <ImageView

  android:id="@+id/imgLogo"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerInParent="true"
  android:src="@drawable/wwe_logo" />

  

  <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_marginBottom="10dp"
  android:textSize="12dp"
  android:textColor="#454545"
  android:gravity="center_horizontal"
  android:layout_alignParentBottom="true"
  android:text="www.fikri-yogi.blogspot.com" />

  
  </RelativeLayout>

5. Add the following code in SplashScreen.java activity. In this following code a handler is used to wait for specific time and once the timer is out we launched main activity.
SplashScreen.java
package info.androidhive.androidsplashscreentimer;

  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Handler;
  
  public class SplashScreen extends Activity {
  
  // Splash screen timer
  private static int SPLASH_TIME_OUT = 3000;

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

  new Handler().postDelayed(new Runnable() {

  /*
  * Showing splash screen with a timer. This will be useful when you
  * want to show case your app logo / company
  */

  
  @Override
  public void run() {

  // This method will be executed once the timer is over
  // Start your app main activity

  Intent i = new Intent(SplashScreen.this, MainActivity.class);
  startActivity(i);

  // close this activity
  finish();

  }

  }, SPLASH_TIME_OUT);

  }

}
Run the application, you will see the splash screen for 3 sec and then your main activity will be launched.
How to Show Alert Dialog in Android

How to Show Alert Dialog in Android


Alert DIalog tutorial android
Creating alert dialog is very easy. In this tutorial i will be discussing about creating different alert dialogues with one button (ok button), two buttons (yes or no buttons) and three buttons (yes, no and cancel buttons).

Android alert dialog with One button

The following code will create a simple alert dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon()is to set icon to alert dialog.
AlertDialog alertDialog = new AlertDialog.Builder(

  AlertDialogActivity.this).create();

  // Setting Dialog Title
  alertDialog.setTitle("Alert Dialog");
  

  // Setting Dialog Message
  alertDialog.setMessage("Welcome to AndroidHive.info");

  // Setting Icon to Dialog
  alertDialog.setIcon(R.drawable.tick);

 
  // Setting OK Button
  alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

  // Write your code here to execute after dialog closed
  Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();

  }

  });

  
  // Showing Alert Message
  alertDialog.show();

Android alert dialog with two button

The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);


  // Setting Dialog Title
  alertDialog.setTitle("Confirm Delete...");

  // Setting Dialog Message
  alertDialog.setMessage("Are you sure you want delete this?");

  // Setting Icon to Dialog
  alertDialog.setIcon(R.drawable.delete);

  // Setting Positive "Yes" Button
  alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog,int which) {

  // Write your code here to invoke YES event
  Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();

  }

  });

  

  // Setting Negative "NO" Button
  alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

  // Write your code here to invoke NO event
  Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
  dialog.cancel();

  }

  });

  

  // Showing Alert Message
  alertDialog.show();

Android alert dialog with three button

Here setNeutralButton() is used to create a neutral cancel button
AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);

  // Setting Dialog Title
  alertDialog.setTitle("Save File...");

  

  // Setting Dialog Message
  alertDialog.setMessage("Do you want to save this file?");

  

  // Setting Icon to Dialog
  alertDialog.setIcon(R.drawable.save);

  

  // Setting Positive "Yes" Button
  alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

  // User pressed YES button. Write Logic Here
  Toast.makeText(getApplicationContext(), "You clicked on YES",

  Toast.LENGTH_SHORT).show();

  }

  });

  

  // Setting Negative "NO" Button
  alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

  // User pressed No button. Write Logic Here
  Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();

  }

  });

  

  // Setting Netural "Cancel" Button
  alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

  // User pressed Cancel button. Write Logic Here
  Toast.makeText(getApplicationContext(), "You clicked on Cancel",

  Toast.LENGTH_SHORT).show();

  }

  });

  // Showing Alert Message
alertDialog.show();

Get Started Developing for Android with Eclipse

Get Started Developing for Android with Eclipse


Get Started Developing for Android with Eclipse
We are going see how to make a very simple Android app (in our case, a product inventory app) that will call a PHP script to perform basic CRUD (CreateReadUpdateDelete) operations. To brief you on the architecture, this is how it works. First your android app calls a PHP script in order to perform a data operation, lets say “create”. The PHP script then connects to your MySQL database to perform the operation. So the data flows from your Android app to PHP script then finally is stored in your MySQL database. Allright, lets dig deeper.

1. Download & Install the Android SDK

a. Download the Android SDK
b. Install/Extract the downloaded SDK
c. Go to the directory where you installed the SDK and open SDK Manager to open Android SDK and AVD Manager.
d. In AVD manager under Avaliable Packages you can see different versions of SDK’s
e. Select SDK Platform tools and one of the version of SDK and click on install

 

2. Downloading Eclipse Software

Although there are lot of IDE out there Eclipse is recommended IDE which will give you best support for Android app development.

 

3. Installing Android Development Toolkit (ADT) plug-in

a. Open Eclipse s/w and under Help -> Install New Software
b. Now you will see a window which allows you to install new plug-in
c. Click on Add button and in Name and in Location give the link https://dl-ssl.google.com/android/eclipse/ and proceed with further steps.

4. Creating Sample Project

Creating a sample android project involves very few steps
a. In your Eclipse IDE go to File -> Android Project
b. Give Project Name, Select Build Target, Application Name, Package Name, Activity Name, Min SDK version and click Finish
c. Now you can see bunch of files created in the project explorer.

5. Creating New Android Virtual Device (AVD)

The AVD is an emulator which provides you android hardware and software environment to test application on computer.
a. In Eclipse open SDK Manager under Windows -> Android SDK and AVD Manager
b. Click on New on the right side.
c. Give Name, Select Target give SD Card size and click on Create AVD.
d. Now a new AVD is created with the specification you provided and Close the Android SDK and AVD Manager

6. Running the Project

Once you successfully created AVD you are ready to test your application.
a. Right Click on the project in Package Explore and click on Run As -> Android Application.
b. Now you can see an AVD is opened and booting up.( It will take much time to launch AVD for the first time)
c. Once the AVD started you can see the output on the AVD screen.
Copyright © 2012 Android Study All Right Reserved