Tuesday, April 29, 2014

How to Create Splash Screen in Android With Background Music

-          Launch Eclipse environment as shown below and give it a name



Screen 1 XML layout(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
 
    <ImageView
        android:id="@+id/logo_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/images" />

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/logo_image"
        android:layout_gravity="center_horizontal"
        android:layout_centerInParent="true" >
    </ProgressBar>
   
    </RelativeLayout>
   
   

</LinearLayout>



Screen 1(MainActivity.java)
package com.askotsolutions.splashscreenapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.Window;

public class MainActivity extends Activity {
      MediaPlayer mp;
      private long ms=0;
    private long splashTime=9000;
    private boolean splashActive = true;
    private boolean paused=false;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mp = MediaPlayer.create(getApplicationContext(), R.raw.song);
        mp.start();
      
        //Hides the title bar
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);
       
        Thread mythread = new Thread() {
            public void run() {
                try {
                    while (splashActive && ms < splashTime) {
                        if(!paused)
                            ms=ms+100;
                        sleep(100);
                    }
                } catch(Exception e) {}
                finally {
                    Intent intent = new Intent(MainActivity.this, WelcomeActivity.class);
                    startActivity(intent);
                }
            }
        };
        mythread.start();
}
    protected void onPause(){
      super.onPause();
      mp.release();
      finish();
      } 
   
}


Screen 2 XML layout (welcome_activity.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
   
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="40dp"
        android:text="Welcome Here!!!" />
   

</LinearLayout>



Screen 2 (WelcomeActivity.java)
package com.askotsolutions.splashscreenapp;

import android.app.Activity;
import android.os.Bundle;

public class WelcomeActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.welcome_activity);
           
           
      }
}




Android Manifest(AndroidManifest.xml)

Register the second activity (WelcomeActivity.java) into your android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.askotsolutions.splashscreenapp"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.askotsolutions.splashscreenapp.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
              android:name=".WelcomeActivity" />
    </application>


</manifest>





download the source code SplashScreenApp.zip

No comments:

Post a Comment