• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Saturday 29 March 2014

Posted by Unknown
No comments | 00:22
Hello guys, got a good response from my first video which encouraged me to write my second blog.
In this blog i will show how to create a splash screen in android which also produces a sound on start up. So, lets get started.


Follow the steps correctly-

1) Create an Android Application Project.

2) Take an image file and name it as "logo.jpg" and save it in the "res/drawable-mdpi".

3) Go to "res/layout/activity_main.xml" and insert 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"
    tools:context=".MainActivity"
    android:background="@drawable/logo" >
</RelativeLayout>

We have named our image file as "logo.jpg" so we are using the same name in "android:background " in the code. We can change this file name to whatever we want but the same should be the image filename.

4) Now go to "src/your_package_name/MainActivity.java" and insert the following code.
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class MainActivity extends Activity {
      MediaPlayer mp;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mp = MediaPlayer.create(this, R.raw.music);
            mp.start();
            new Handler().postDelayed(new Runnable() {

                  @Override
                  public void run() {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(MainActivity.this,NewActivity.class);
                        mp.stop();
                        startActivity(i);
                        finish();
                       
                  }
                 
            }, 5000);
      }
}

In this code we are using MediaPlayer class to play the audio on startup.

MediaPlayer mp;
mp = MediaPlayer.create(this, R.raw.music);
mp.start();

Then we are using the Handler class to create a delay of 5s. This handler class consists of run() function in which we are creating our new activity.Then this activity gets called after 5s.

5) Now go to "src/your_package_name" folder, right click there and create a new class. Lets name the class as "NewActivity.java" .Copy the below code in NewActivity.java.

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

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

This class basically displays the text.

6) Now go to "res/layout" and right click there to create a new xml file.Lets name the file as "new_activity.xml" .Copy the below code in the new_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:text="Hello its a new Activity"/>

</LinearLayout>

7) Now the last thing, open the AndroidManifest.xml and we will create our new activity for "NewActivity.java".Simply copy paste the below code after the end of previous activity tag ie after "</activity>" .

<activity android:name="your_package_name.NewActivity" />



Now we are done. We have created our new Android Splash Screen with sound. Comment your problems and doubts. I will love to handle it.

Thank you.

0 comments:

Post a Comment