[Android] Membuat Splash Screen Dengan ProgressBar

Langsung ke topik aja gan, gimana caraya ngebuat Splash Scren yang ada ProgressBar nya di android.

Pertama Bikin Project dulu, nama projectnya Splash, setelah Project dibuatm kita buat Class Baru namanya “Loading.java” , Edit Class loading.java , sampai seperti ini

package com.example.splash;

import android.os.AsyncTask;
import android.util.Log;
import android.widget.ProgressBar;

public class Loading extends AsyncTask<String, Integer, Integer> {
	public interface LoadingTaskFinishedListener {
		void onTaskFinished();
	}

	private final ProgressBar progressBar;
	private final LoadingTaskFinishedListener finishedListener;

	public Loading(ProgressBar progressBar,
			LoadingTaskFinishedListener finishedListener) {
		this.progressBar = progressBar;
		this.finishedListener = finishedListener;
	}

	@Override
	protected Integer doInBackground(String... params) {
		Log.i("Tutorial", "Starting task with url: " + params[0]);
		if (resourcesDontAlreadyExist()) {
			downloadResources();
		}
		return 1234;

	}

	private boolean resourcesDontAlreadyExist() {

		return true;
	}

	private void downloadResources() {

		int count = 10;
		for (int i = 0; i < count; i++) {

			int progress = (int) ((i / (float) count) * 100);
			publishProgress(progress);

			try {
				Thread.sleep(100);
			} catch (InterruptedException ignore) {
			}
		}
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
		progressBar.setProgress(values[0]);
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		finishedListener.onTaskFinished();
	}

}

Seteleah itu edit bagian activity_main.xml, Edit Seperti ini

<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:background="#ffffffff"
    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" >

    <ProgressBar
        android:id="@+id/progressbar_Horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="52dp"
        android:max="100" />

    <ProgressBar
        android:id="@+id/progressbar_default"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressbar_Horizontal"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="40dp" />

    <TextView
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/progressbar_default"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="19dp"
        android:text="Please Wait....." />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

setelah itu edit MainActivity.java

package com.example.splash;

import com.example.splash.Loading.LoadingTaskFinishedListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements LoadingTaskFinishedListener {

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

		ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
		 new Loading(progressBar, this).execute("");

	}

	@Override
	public void onTaskFinished() {
		completeSplash();
	}
	private void completeSplash(){
        startApp();
        finish(); // Don't forget to finish this Splash Activity so the user can't return to it!
    }
	private void startApp() {
        Intent intent = new Intent(MainActivity.this, Dashboard.class);
        startActivity(intent);
    }

}

nah, kalo udah buat activity baru kasih nama Activitynya Dashboard, biarkan saja sesuai default, lalu running projectnya, kalo bener pasti hasilnya gini

Splash1splash2

 

Download Projectnya disini

Semoga Bermanfaat

4 thoughts on “[Android] Membuat Splash Screen Dengan ProgressBar

  1. Pingback: jesse

  2. Nanya gan…klo misalkan kita udah buat project sendiri terus mau menambahkan splash screen ke dalam project kita gimana caranya?

Leave a Reply

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


+ 4 = 6

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>