Wednesday, 19 September 2012

Loading Screen

Loading Screen:

1.src: Loading.java


package com.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;
    @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;
   
    //Before running code in the separate thread
@Override
protected void onPreExecute()
{
//Initialize the ViewSwitcher object
       viewSwitcher = new ViewSwitcher(Loading.this);
       /* Initialize the loading screen with data from the 'loadingscreen.xml' layout xml file.
        * Add the initialized View to the viewSwitcher.*/
viewSwitcher.addView(ViewSwitcher.inflate(Loading.this, R.layout.loadingscreen, null));

//Initialize the TextView and ProgressBar instances - IMPORTANT: call findViewById() from viewSwitcher.
tv_progress = (TextView) viewSwitcher.findViewById(R.id.tv_progress);
pb_progressBar = (ProgressBar) viewSwitcher.findViewById(R.id.pb_progressbar);
//Sets the maximum value of the progress bar to 100
pb_progressBar.setMax(100);

//Set ViewSwitcher instance as the current View.
setContentView(viewSwitcher);
}

//The code to be executed in a background thread.
@Override
protected Void doInBackground(Void... params)
{
try
{
//Get the current thread's token
synchronized (this)
{
//Initialize an integer (that will act as a counter) to zero
int counter = 0;
//While the counter is smaller than four
while(counter <= 4)
{
this.wait(1500);
//Increment the counter
counter++;
publishProgress(counter*10);//setting the time interval of the progeress bar
}
}
}
catch (InterruptedException e)
{
e.printStackTrace();
}
return null;
}

//Update the TextView and the progress at progress bar
@Override
protected void onProgressUpdate(Integer... values)
{
//Update the progress at the UI if progress value is smaller than 100
if(values[0] <= 100)
{
tv_progress.setText("Progress: " + Integer.toString(values[0]) + "%");
pb_progressBar.setProgress(values[0]);
}
}

//After executing the code in the thread
@Override
protected void onPostExecute(Void result)
{
/* Initialize the application's main interface from the 'main.xml' layout xml file.
        * Add the initialized View to the viewSwitcher.*/
viewSwitcher.addView(ViewSwitcher.inflate(Loading.this, R.layout.main, null));
//Switch the Views
viewSwitcher.showNext();
}
    }
   
    //Override the default back key behavior
    @Override
    public void onBackPressed()
    {
    //Emulate the progressDialog.setCancelable(false) behavior
    //If the first view is being shown
    if(viewSwitcher.getDisplayedChild() == 0)
    {
    //Do nothing
    return;
    }
    else
    {
    //Finishes the current Activity
    super.onBackPressed();
    }
    }
}


2.res--drawable--
2.1loadingscreenbg.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" />
2.2 tile.png

3.res--layout
3.1 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>

3.2 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>



output:1.
2.







No comments:

Post a Comment