Loading Screen Before Application Start in Android

L

create xml file in rec/Layout/main.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<RelativeLayout    xmlns:android=”http://schemas.android.com/apk/res/android”    android:orientation=”vertical”   android:layout_height=”fill_parent”  android:layout_width=”match_parent” android:background=”#B0B6BA”    ><TextView      android:id=”@+id/RLayout1tag”         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”               android:textColor=”#688CBE”        android:textSize=”15dp”        android:textStyle=”bold”                android:layout_centerInParent=”true”    android:text=”Screen load successfully”    /></RelativeLayout>

<RelativeLayout     xmlns:android=”http://schemas.android.com/apk/res/android”    android:orientation=”vertical”    android:layout_height=”fill_parent”  android:layout_width=”match_parent” android:background=”#B0B6BA”    ><TextView      android:id=”@+id/RLayout1tag”         android:layout_width=”wrap_content”         android:layout_height=”wrap_content”               android:textColor=”#688CBE”        android:textSize=”15dp”        android:textStyle=”bold”                android:layout_centerInParent=”true”    android:text=”Screen load successfully”    /></RelativeLayout>

rec/Layout/loadingscreen.xml

<?xml version=”1.0″ encoding=”utf-8″?><RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”    android:layout_width=”fill_parent”    android:layout_height=”fill_parent”    android:orientation=”vertical”     android:background=”@drawable/loadingscreenbg”>
<ProgressBar        android:id=”@+id/pb_progressbar”        style=”?android:attr/progressBarStyleHorizontal”        android:layout_width=”fill_parent”        android:layout_height=”wrap_content”        android:layout_alignParentBottom=”true”        android:layout_centerHorizontal=”true”         android:layout_marginBottom=”20dip”         android:layout_marginLeft=”20dip”         android:layout_marginRight=”20dip”         android:layout_marginTop=”10dip”/>
<TextView        android:id=”@+id/tv_loadingtext”        android:layout_width=”wrap_content”        android:layout_height=”wrap_content”        android:layout_above=”@+id/pb_progressbar”        android:layout_centerHorizontal=”true”        android:text=”Loading, please wait…”        android:textAppearance=”?android:attr/textAppearanceMedium” />
<TextView        android:id=”@+id/tv_progress”        android:layout_width=”wrap_content”        android:layout_height=”wrap_content”        android:layout_alignBottom=”@+id/pb_progressbar”        android:layout_centerHorizontal=”true”        android:text=”Progress: 0%”        android:textAppearance=”?android:attr/textAppearanceSmall”         android:textColor=”#ffffffff” android:shadowColor=”#000000″         android:shadowDx=”2.0″ android:shadowDy=”2.0″         android:shadowRadius=”3.0″/>
</RelativeLayout>

 

for loading screen background

rec/drawable/loadingscreenbg.xml

<?xml version=”1.0″ encoding=”utf-8″?><bitmap xmlns:android=”http://schemas.android.com/apk/res/android”

android:src=”@drawable/tile” android:tileMode=”repeat” />

 

java file in src/Loading.java

package com.nanostuffs.prashant.loadscreen;
import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ProgressBar;import android.widget.TextView;import android.widget.ViewSwitcher;
public class Loading extends Activity { private ViewSwitcher viewSwitcher;
/** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState)     {        super.onCreate(savedInstanceState);        new LoadViewTask().execute();    }        private class LoadViewTask extends AsyncTask<Void, Integer, Void>    {    private TextView tv_progress;    private ProgressBar pb_progressBar;    @Override protected void onPreExecute()  {        viewSwitcher = new ViewSwitcher(Loading.this); viewSwitcher.addView(ViewSwitcher.inflate(Loading.this, R.layout.loadingscreen, null)); tv_progress = (TextView) viewSwitcher.findViewById(R.id.tv_progress); pb_progressBar = (ProgressBar) viewSwitcher.findViewById(R.id.pb_progressbar); pb_progressBar.setMax(100); setContentView(viewSwitcher); }
@Override protected Void doInBackground(Void… params)  { try  { synchronized (this)  { int counter = 0; while(counter <= 4) { this.wait(1500); counter++; publishProgress(counter*10);//setting the time interval of the progeress bar } } }  catch (InterruptedException e)  { e.printStackTrace(); } return null; }
@Override protected void onProgressUpdate(Integer… values)  { if(values[0] <= 100) { tv_progress.setText(“Progress: ” + Integer.toString(values[0]) + “%”); pb_progressBar.setProgress(values[0]); } } @Override protected void onPostExecute(Void result)  { viewSwitcher.addView(ViewSwitcher.inflate(Loading.this, R.layout.main, null)); viewSwitcher.showNext(); }    }        @Override    public void onBackPressed()     {    if(viewSwitcher.getDisplayedChild() == 0)    {    //Do nothing    return;    }    else    {    super.onBackPressed();    }    }}

 

Output:

About the author

prashant.koli
By prashant.koli

Category