Android Tutorial Membuat SplashScreen Sederhana

Android Tutorial Membuat

SplashScreen Sederhana

Splash screen adalah tampilan tambahan yang akan muncul saat
pertama kali membuat suatu aplikasi.
Buat project baru di
Android Studio File
New Project. Kemudian pilih Empty
Activity
 dan melanjutkannya hingga selesai.
activity_main.xml

Tampilan setelah SplashScreen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

<?xml version="1.0"
encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.users.splashscreen.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textStyle="bold"
        android:text="SELAMAT
DATANG \n\nwww.users.com"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Buat layout baru
dengan nama splashscreen.xml dan masukan code dibawah ini.

splashscreen.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

<?xml version="1.0"
encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:background="@mipmap/ic_launcher"/>

    <ProgressBar
        android:id="@+id/loading"
        android:layout_below="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_marginBottom="16dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:text="www.users.com"/>

</RelativeLayout>

Buat class baru
dengan nama Splashscreen.java dan masukan code dibawah ini.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

package com.users.splashscreen;

import android.content.Intent;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Splashscreen extends
AppCompatActivity {

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

        if (android.os.Build.VERSION.SDK_INT
> 9) {
            StrictMode.ThreadPolicy
policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }

        final int
welcomeScreenDisplay = 3000; // 3000 = 3 detik
        Thread
welcomeThread = new
Thread()
{

            int wait = 0;

            @Override
            public void
run() {
                try {
                    super.run();
                    while (wait < welcomeScreenDisplay) {
                        sleep(100);
                        wait
+= 100;
                    }
                }
catch
(Exception
e) {
                    System.out.println("EXc=" + e);

                }
finally
{
                    Intent
intent = new
Intent(Splashscreen.this,
MainActivity.class);
                    finish();
                    startActivity(intent);
                }
            }
        };

        welcomeThread.start();

    }
}

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13

package com.users.splashscreen;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends
AppCompatActivity {

    @Override
    protected void
onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
Buka
AndroidManifest.xml kemudian jadikan class Splashscreen sebagai tampilan awal
sebelum masuk MainActivity.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

<?xml version="1.0"
encoding="utf-8"?>
    package="com.users.splashscreen">

    <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">
        <activity android:name=".Splashscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>




Posting Komentar

advertise
advertise
advertise
advertise