Types of DialogBox
A dialogBox is a small window that prompts the user to make a decision or enter additional information. A dialogBox does not fill the screen and is normally used for modal events that require users to take an action before they can proceed, like to select date from DatePicker Dialog, to select time from time picker Dialog, to show the Progress, to show a list of items or options to select etc.
Alert Dialog:
A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
DatePicker Dialog and TimePicker Dialog:
A dialog with a pre-defined UI that allows the user to select a date or time.
Progress Dialog:
A Dialog that can show a title, ProressBar and a message, or a custom layout.
List Dialog:
A Dialog that can show a title, List of items and a message, or as above a custom layout.
List Dialog
Date Picker Dialog |
After Date Picked |
Alert Dialog
Alert Dialog |
Character Picker Dialog
Character Picker Dialog |
After Character Picked |
Progress Dialog
Progress Dialog |
Create new Android Project
Project Name: TypesOfDialogs
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: Types Of Dialogs
Package Name: com.shaikhhamadali.blogspot.typesofdialog
Create layout file: activity_types_of_dialog
Project Name: TypesOfDialogs
//tested from 2.3.3 to current android sdk
Build Target: Android 2.3.3 //or greater than that
Application Name: Types Of Dialogs
Package Name: com.shaikhhamadali.blogspot.typesofdialog
Create layout file: activity_types_of_dialog
1. create layout:
- activity_types_of_dialog:
<?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" > <TextView android:id="@+id/tvDemo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:text="Types OF Dialog" android:textColor="#6A96B0" android:textSize="50sp" android:textStyle="bold|italic" /> <Button android:id="@+id/btnListDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="120dp" android:text="List Dialog" android:textSize="25sp" /> <Button android:id="@+id/btnDatePickerDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="120dp" android:layout_toRightOf="@id/btnListDialog" android:text="DatePicker Dialog" android:textSize="25sp" /> <Button android:id="@+id/btnAlertDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="120dp" android:layout_toRightOf="@id/btnDatePickerDialog" android:text="Alert Dialog" android:textSize="25sp" /> <Button android:id="@+id/btnCharacterPickerDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="120dp" android:layout_toRightOf="@id/btnAlertDialog" android:text="Character Picker Dialog" android:textSize="25sp" /> <Button android:id="@+id/btnProgressDialog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="120dp" android:layout_toRightOf="@id/btnCharacterPickerDialog" android:text="Progress Dialog" android:textSize="25sp" /> </RelativeLayout>
2. code of activity:
package com.shaikhhamadali.blogspot.typesofdialogs; import android.os.Bundle; import android.app.Activity; import java.util.Calendar; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.graphics.Color; import android.text.method.CharacterPickerDialog; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; public class TypesOfDialog extends Activity { TextView tvDemo; CharacterPickerDialog cPDialog; Button btnAlertDialog, btnListDialog, prgDialog = null, btnDatePickerDialog, btnCharacterPickerDialog; int year, month, day; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_types_of_dialog); tvDemo = (TextView) findViewById(R.id.tvDemo); cPDialog_init(); // Alert Dialog Demo btnAlertDialog = (Button) findViewById(R.id.btnAlertDialog); btnAlertDialog.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // Create instance of Alert Dialog Builder AlertDialog.Builder builder = new AlertDialog.Builder( TypesOfDialog.this); // set message builder.setMessage("Are you Sure you want to Exit Application?"); builder.setCancelable(false); builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // finish the application TypesOfDialog.this.finish(); } }); builder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub // close the dialog box dialog.cancel(); } }); //create instance of alert dialogand assign configuration of //builderto alert dialog instance AlertDialog alert = builder.create(); // Show Alert Dialog alert.show(); } }); // List Dialog Demo btnListDialog = (Button) findViewById(R.id.btnListDialog); btnListDialog.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Create instance of Alert Dialog Builder AlertDialog.Builder builder = new AlertDialog.Builder( TypesOfDialog.this); // create array of items basic colors RGB (Red,Green,Blue) CharSequence[] items = { "Red", "Green", "Blue" }; // set title builder.setTitle("Chose Color.."); builder.setItems(items, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub switch (which) { case 0: tvDemo.setTextColor(Color.RED); break; case 1: tvDemo.setTextColor(Color.GREEN); break; case 2: tvDemo.setTextColor(Color.BLUE); break; } } }); //create instance of alert dialogand assign configuration of //builderto alert dialog instance AlertDialog alert = builder.create(); // Show Alert Dialog alert.show(); } }); // Progress Dialog Demo prgDialog = (Button) findViewById(R.id.btnProgressDialog); prgDialog.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { //create instance of progress dialog //show(context,title,message,indeterminate) final ProgressDialog myProgressDialog = ProgressDialog.show( TypesOfDialog.this, "Please wait for 3 seconds...", "you can use this to perform extreme calculations...", true); // create thread to sleep activity for 3 seconds new Thread() { public void run() { try { // used as doing some Fake-Work sleep(3000); } catch (Exception e) { } // Dismiss the Progress Dialog myProgressDialog.dismiss(); } }.start(); } }); // DatePicker Dialog Demo btnDatePickerDialog = (Button) findViewById(R.id.btnDatePickerDialog); btnDatePickerDialog.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // create instance of calendar Calendar c = Calendar.getInstance(); // get year,month and day year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); // get day of month day = c.get(Calendar.DAY_OF_MONTH); // show date picker dialog // DatePickerDialog(context,callBack,year,monthOfYear,dayOfMonth) new DatePickerDialog(TypesOfDialog.this, datePickerListener, year, month, day).show(); } }); // Character Picker Dialog Demo btnCharacterPickerDialog = (Button) findViewById(R.id.btnCharacterPickerDialog); btnCharacterPickerDialog.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub cPDialog_init(); cPDialog.show(); } }); } // Set DatePicker Listener always called when Set Button is clicked // private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() { // this method will call on close dialog box public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; // set selected date into tvDemo tvDemo.setText(new StringBuilder().append(day).append("-") .append(month + 1).append("-").append(year).append(" ")); // set selected date into date picker dialog also // dpResult.init(year, month, day, null); } }; // initialize Character Picker Dialog void cPDialog_init() { EditText eText = new EditText(this); eText.setLayoutParams(new LinearLayout.LayoutParams(-1, -2)); // set all hexa-decimal options final String options = "0123456789ABCDEF"; cPDialog = new CharacterPickerDialog(this, new View(this), null, options, false) { public void onClick(View v) { tvDemo.setText("selected character:" + ((Button) v).getText().toString()); dismiss(); } }; } }3. note that:
- you can use these Dialog Boxes on button on click,on action_down,on the fly etc.
- detailed info about Alert Dialog
- detailed info about Pickers,Date picker Dialog
- you can create custom dialogs.
- also you can add custom styles and child controls .
4. conclusion:
- Some information about Pickers (Date picker ,character picker).
- Some information about alert dialogs.
- Know how to create alert dialogs and pickers (Date picker ,character picker).
- know how to interact with them.
- how to get user selections and use them accordingly.
5. About the post:
- The code seems to explain itself due to comments, and is very easy to understand.
- Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
- Hope you enjoy it!
Hamad Ali Shaikh