Showing posts with label Android Progress Dialog. Show all posts
Showing posts with label Android Progress Dialog. Show all posts

Saturday, 12 October 2013

Android Progress Dialog

In this exercise we will learn to create a Progress Dialog.

Lets create a button to trigger the progress dialog.

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Start" />


Now to trigger the progress dialog

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity
{
Button st;
ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
st=(Button)findViewById(R.id.button1);
st.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
dialog=ProgressDialog.show(MainActivity.this,"Progress Dialog","Loading...",true);
Thread t=new Thread()
{
public void run()
{
try
{
Thread.sleep(5000);
dialog.cancel();
}
catch(Exception e)
{
Toast.makeText(MainActivity.this,""+e,Toast.LENGTH_LONG).show();
}
}
};
t.start();
}
});
}
}


The program has been coded to show the progress dialog for 5 seconds.