ProgressDialog

Progress bars are used to show progress of a task. For example, when you are uploading or downloading something from the internet, it is better to show the progress of download/upload to the user.

1. MainActivity.java

package com.tutorialsee.progressdialog;

import java.util.Calendar;
import com.tutorialsee.progressdialog.R;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.text.method.CharacterPickerDialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.HONEYCOMB) public class MainActivity extends Activity {
private CharacterPickerDialog dialog = null;
String options = "0123456789ABCDEFGHIJ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.button1);
Button click2 = (Button)findViewById(R.id.button2);
Button click3 = (Button)findViewById(R.id.button3);
Button click4 = (Button)findViewById(R.id.button4);
Button click5 = (Button)findViewById(R.id.button5);
Button click6 = (Button)findViewById(R.id.button6);

click.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final ProgressDialog progressdialog = ProgressDialog.show(MainActivity.this, "Please wait",
"Loading please wait..", true);
progressdialog.setCancelable(true);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
// put the thread to sleep for 2 seconds
Thread.sleep(2000);
} catch (Exception e) {
}
progressdialog.dismiss();
}
}).start();
}
});


click2.setOnClickListener(new View.OnClickListener() {
ProgressDialog progressdialog;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
progressdialog = new ProgressDialog(MainActivity.this);
progressdialog.setMessage("Loading....");
progressdialog.setTitle("Please Wait..");
progressdialog.setProgressStyle(progressdialog.STYLE_HORIZONTAL);
progressdialog.setProgress(0);
progressdialog.setMax(100);
progressdialog.show();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (progressdialog.getProgress() <= progressdialog.getMax()) {
Thread.sleep(1000);
handle.sendMessage(handle.obtainMessage());
if (progressdialog.getProgress() == progressdialog.getMax()) {
progressdialog.dismiss();
}
}
} catch (Exception e) {
}
}
}).start();
}

Handler handle = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
progressdialog.incrementProgressBy(20);
}
};

});


click3.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DialogFragment newFragment = new mytimepicker();
newFragment.show(getFragmentManager(), "timePicker");
}
});

click4.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.show();
}
});

dialog = new CharacterPickerDialog(this, new View(this), null, options,false) {
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"onClick! " + ((Button) v).getText().toString(),Toast.LENGTH_SHORT).show();
dismiss();
}

public void onItemClick(AdapterView parent, View view,int position, long id) {
dismiss();
}
};

click5.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new OnDateSetListener() {
public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
// TODO Use the selected date.
}
}, 2013, 10, 11);
datePickerDialog.show();
}
});

click6.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Create custom dialog object
final Dialog dialog = new Dialog(MainActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog_layout);
// Set dialog title
dialog.setTitle("Custom Dialog");
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.img);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();

Button declineButton = (Button) dialog.findViewById(R.id.button34);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}
});
}

public static class mytimepicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// do something with the time chosen.
}
}
}

2. activity_main.xml

<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="#FAFAFA"
    tools:context="${relativePackage}.${activityClass}" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#009688"
        android:gravity="center"
        android:text="@string/texts"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="#009688"
        android:text="@string/textss"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="42dp"
        android:background="#009688"
        android:gravity="center"
        android:text="@string/textsss"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button3"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="42dp"
        android:background="#009688"
        android:gravity="center"
        android:text="@string/button4"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button4"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="42dp"
        android:background="#009688"
        android:gravity="center"
        android:text="@string/button5"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button5"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginTop="42dp"
        android:background="#009688"
        android:gravity="center"
        android:text="@string/button6"
        android:textColor="#ffffff" />

</RelativeLayout>

3. dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#B2DFDB"
    android:orientation="vertical"
    >

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#B2DFDB"
    android:orientation="horizontal"
    android:padding="8dp" >
   
    <ImageView
         android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@drawable/ic_launcher"
        android:src="@drawable/ic_launcher" />

   

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="@string/text"
        android:gravity="center"
        android:textColor="@android:color/black" />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#B2DFDB"
    android:gravity="center"

    android:orientation="vertical"
   >
  
    <Button
        android:id="@+id/button34"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#009688"
        android:text="Submit"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
            android:padding="8dp"
        android:textColor="#ffffff" />
    </LinearLayout>
</LinearLayout>