Jong Onin
Jong Onin

Reputation: 215

Android Development: Proper way of creating Splash Screen

I know that this has been asked several times but I really want to know the proper way of implementing an Android Splash Screen.

1. Should we create an XML Layout and add Runnable Java code in SplashActivity?

2. Or, just make an xml file in drawable and call it under Styles.xml?

I've tried both. But for me, #1 is more difficult for a beginner.

Now, I'm currently working on a splash screen using approach #2. I have made a splashstyle.xml under Drawable enter image description here

Now, I called it under Styles.xml with style name SplashTheme.

enter image description here

Updated my AndroidManifest file.

enter image description here

Then, here's my SplashActivity.java. Notice that I didn't have runnable code on this class. Just a simple Intent.

enter image description here

And now, here's the final output. Since this is just a practice. Don't expect a nice splash screen.

enter image description here

Question1: Now, I want to adjust the image on my splash screen. Adjust it's size. How am i gonna do that since I'm not using an xml layout?

Question2: I want to put a text at the bottom of my splash screen. How am i gonna do that? Is there a way to add text just using the splashstyle.xml or styles.xml?

Question3: I have observed that my splash screen don't display for too long. Is it because my app doesn't have more contents yet? I want the splash screen to display for maybe 3-5 seconds. How am i gonna do that without implementing runnable?

Upvotes: 0

Views: 4434

Answers (4)

R.Anjali
R.Anjali

Reputation: 181

q1.If image size is big we can reduce size like this

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

    <item android:drawable="@color/colorPrimary"></item>

    <item
        android:left="20dp"
        android:right="20dp"
        android:top="20dp"
        android:bottom="20dp">

        <bitmap

            android:gravity="center"
            android:src="@drawable/delete"></bitmap>
    </item>
</layer-list>

Upvotes: 4

Jaiprakash Soni
Jaiprakash Soni

Reputation: 4112

For

Question1: Now, I want to adjust the image on my splash screen. Adjust it's size. How am i gonna do that since I'm not using an xml layout?

Question2: I want to put a text at the bottom of my splash screen. How am i gonna do that? Is there a way to add text just using the splashstyle.xml or styles.xml?

You can't do all things using style.xml it can't create view for your activity it just for enhance your View. Use xml layout for your activity or dynamically add views to your activity.

Question3: I have observed that my splash screen don't display for too long. Is it because my app doesn't have more contents yet? I want the splash screen to display for maybe 3-5 seconds. How am i gonna do that without implementing runnable?

splash screen don't display for too long because in your onCreate() contains starting LoginActivity so when onCreate() called it start your LoginActivity. If you want to control the delay you can use Handler,runnable,Timer task etc.

Upvotes: 0

Komal12
Komal12

Reputation: 3348

Try this,

SplashActivity

public class SplashActivity extends Activity {

    final private Context mContext = SplashActivity.this;
    final private static int SPLASH_TIME_OUT = 3500;
    private Handler handler;
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_splash);

        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        };
        handler.postDelayed(runnable, SPLASH_TIME_OUT);
    }

    /* remove call back in on destroy */
    @Override
    protected void onDestroy() {
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
        super.onDestroy();
    }

    /* device back button click event */
    @Override
    public void onBackPressed() {
        if (handler != null) {
            handler.removeCallbacks(runnable);
        }
        super.onBackPressed();
    }
}

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:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:contentDescription="@string/Image"
        android:src="@drawable/ic_logo" />

</RelativeLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.packagename">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".Halt">
    <activity android:name=".SplashActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Upvotes: 2

Rakshit Sorathiya
Rakshit Sorathiya

Reputation: 679

Answer for your Question 3: I have observed that my splash screen don't display for too long. Is it because my app doesn't have more contents yet? I want the splash screen to display for maybe 3-5 seconds. How am i gonna do that without implementing runnable?

You need to create a class which extends Application and use sleep method to display a splash screen for some seconds.

Example:

Halt.java

import android.app.Application;
import android.os.SystemClock;

public class Halt extends Application {

@Override
public void onCreate() {
    super.onCreate();
    SystemClock.sleep(2500);
  }
}

After this class you need to declare Halt class like this: android:name=".Halt" in your manifest file in application tag. Like this:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.splashdemo">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".Halt">
    <activity android:name=".MainActivity"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

This will work for you.

Upvotes: 1

Related Questions