SplashScreen with music in android

In this blog post, I would like to show how to code a SplashScreen with Music in Android. SplashScreen is nothing but a screen with image that run before your actual application.

1. Create a new project File -> Android Project. Create a new class SplashScreen.java and copy paste this code:

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

public class SplashScreen extends Activity {

 MediaPlayer mp;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 requestWindowFeature(Window.FEATURE_NO_TITLE);
 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 WindowManager.LayoutParams.FLAG_FULLSCREEN);
 setContentView(R.layout.splash_screen);
 mp = MediaPlayer.create(this, R.raw.testsong_20_sec);
 mp.start();

 Thread timer = new Thread() {
 public void run() {
 try {
 sleep(5000);
 } catch (InterruptedException e) {
 e.printStackTrace();
 } finally {
 Intent openStartingPoint = new Intent(SplashScreen.this,
 MainActivity.class);
 startActivity(openStartingPoint);
 }

 }
 };
 timer.start();
 }

 @Override
 protected void onPause() {
 // TODO Auto-generated method stub
 super.onPause();
 finish();
 mp.release();
 }
}

2. Now create new xml file called splash_screen.xml layout and copy paste this code:

<?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:background="splashscreen_image"
 android:orientation="vertical" >

</LinearLayout>

3. Now open MainActivity.java and copy paste this code:

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

public class MainActivity extends Activity {

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

4.Now open your activity_main.xml layout and copy paste this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin" >

 <TextView
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/hello_world" />

</RelativeLayout>

5. Now open your AndroidManifest.xml file and copy paste this code:

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

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

 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".SlpashScreen"
 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=".MainActivity"
 android:label="@string/app_name" >
 </activity>
 </application>

</manifest>

6. Create new folder named raw under res i.e /res/raw and add any music file inside it to play on screen.

Note: If don’t want music on splash screen, then safely remove all mediaplayer code from the splashscreen.java class.

ScreenShot:

Screenshot_2014-12-03-16-33-30[1] Screenshot_2014-12-03-16-33-45[1]

 

 

Hope this helpful. Your valuable comments are always welcomed. It will help to improve my post and understanding.

durga chiranjeevi

durga chiranjeevi

I'm currently working as Android developer at Energy Alternatives India. Interested in Application and Game development.
durga chiranjeevi

Leave a Reply

Your email address will not be published. Required fields are marked *