Wednesday, December 4, 2013

Dialogs in android (alert dialog, date picker, and progress dialog): Android tutorial


Program description:

This program will show how to create a normal alert dialog, items alert dialog, date picker dialog, and progress bar dialog.


There are 4 types of dialogs in android:

1. Alert Dialog
2. Date Picker Dialog
3. Time Picker Dialog
4. Progress Dialog

Alert dialog - is a divided into again 4 types

a. Simple alert dialog - will have title, message, and maximum 3 buttons (yes, no, neutral)
b. items alert dialog  - will have title, items, and maximum 3 buttons
c. Single choice alert dialog - will have title, list of radio items, and maximum 3 buttons.
d. Multi choice alert dialog - will have title, list of check boxes, and maximum 3 buttons.

Date Picker dialog - will show current date, or passed date where use can select a date.

Time Picker dialog - will show current time, and user can select a different time

Progress dialog - which will show progress bar in the dialog.

Progress bar has 2 styles, horizontal progress bar and spinner progress bar.                            Default style is spinner style for progress bar.

Immediately after starting the program, it will show normal alert dialog, from there items, from there progress dialog, and then finally date picker dialog.


First Activity
package co.techpalle.b15_dialogs;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.widget.DatePicker;
import android.widget.Toast;

public class MainActivity extends Activity {
 //step1: define constants for dialogs
 public static final int NRML_DLG = 1;
 public static final int ITMS_DLG = 2;
 public static final int PRGRS_DLG = 3;
 public static final int DTPKR_DLG = 4;
 private DatePickerDialog.OnDateSetListener dpn =
  new DatePickerDialog.OnDateSetListener() {
  @Override
  public void onDateSet(DatePicker view, int year, int monthOfYear,
    int dayOfMonth) {
   
  }
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //Step2 : show the normal dialog first.
  showDialog(NRML_DLG);
 }
 
 //step3 : implement oncreate dialog to create all the dialogs.
 @Override
 protected Dialog onCreateDialog(int id) {
  Dialog d = null;
  switch(id){
  case NRML_DLG:
   //we have to create normal alert dialog
   AlertDialog.Builder ab = new AlertDialog.Builder(this);
   ab.setTitle("First Dialog");
   ab.setIcon(R.drawable.ic_launcher);
   ab.setMessage("Do you want to proceed?");
   ab.setPositiveButton("Yes", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     showDialog(ITMS_DLG);//proceed to next dialog
    }
   });
   ab.setNegativeButton("No", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     //..put a toast..
    }
   });
   d = ab.create();
   break;
   
  case ITMS_DLG:
   //code to create alert items dialog
   AlertDialog.Builder ab1 = new AlertDialog.Builder(this);
   final CharSequence[] items = {"home", "about us", "contact us"};
   ab1.setTitle("Items Dialog");
   ab1.setItems(items, new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     Toast.makeText(getApplicationContext(),
       items[which], 0).show();     
    }
   });
   ab1.setPositiveButton("Proceed", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     showDialog(PRGRS_DLG);
    }
   });
   d = ab1.create();
   break;
   
  case PRGRS_DLG:
                       //code to create progress dialog
   ProgressDialog p = new ProgressDialog(this);
   p.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
   p.setProgress(30);
   p.setButton("Go next", new OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
     showDialog(DTPKR_DLG);
    }
   });
   d = p;
   break;
 
  case DTPKR_DLG:
                       //code to create date picker dialog
   DatePickerDialog dp = new DatePickerDialog(this,
     null, 2013, 6, 1);//year month day
   d = dp;
   break;
  }
  return d;
 }
 
}




xml layout file for First Activity 
File name: 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: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" >  
   <TextView  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:text="@string/hello_world" />  
 </RelativeLayout>  

Download complete code : Click to download

More  programs on dialogs:
android custom Dialogs tutorial

Tags: android, dialog, alert dialog, custom dialog, date picker dialog, items dialog, progress dialog, tutorial, examples
Buy mobile phones.

No comments:

Post a Comment