Tuesday 28 August 2012

Android All type of Built In Dialog Demo - Examples

This example include the following types of dialogs as follows
  • Alert Dialog Demo
  • List Dialog Demo
  • Progress Dialog Demo
  • DatePicker Dialog Demo
  • Character Picker Dialog Demo

Download Android All Dialog Demo :Example Code

  1. main.xml for this Project which Contain 5 button to call each Dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    android:id="@+id/tvShow"
    />
    <Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Alert Dialog"
    android:id="@+id/btnAlert" 
    />
    
    <Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="List Dialog"
    android:id="@+id/btnList" 
    />
    
    <Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="DatePicker Dialog"
    android:id="@+id/btnDtPicker" 
    />
    
    <Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Progress Dialog"
    android:id="@+id/btnProgress" 
    />
    
    <Button android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Character Picker Dialog"
    android:id="@+id/btnChPicker" 
    />
    
</LinearLayout>
 
  1. Following Java Code which will display different DialogBox  when you call them using clicking Buttons. they are create as follows..
 
package com.widget;

import java.util.Calendar;

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.graphics.Color;
import android.os.Bundle;
import android.text.method.CharacterPickerDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AllDialogActivity extends Activity {
 /** Called when the activity is first created. */
 TextView tv;
 Button btnAlert, btnList, prgDilog = null, btnDtPicker, btnChButton;
 int year, month, day;
 CharacterPickerDialog cpd;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  tv = (TextView) findViewById(R.id.tvShow);
  cpd_init();
  /********
   * Alert Dialog Demo
   **********/
  btnAlert = (Button) findViewById(R.id.btnAlert);
  btnAlert.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(
      AllDialogActivity.this);

    builder.setMessage("Are you Sure you want to Exit?");
    builder.setCancelable(false);
    builder.setPositiveButton("Yes",
      new DialogInterface.OnClickListener() {

       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        AllDialogActivity.this.finish();
       }
      });

    builder.setNegativeButton("No",
      new DialogInterface.OnClickListener() {

       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        dialog.cancel();
       }
      });

    AlertDialog alert = builder.create();
    alert.show();
   }
  });
  /********
   * List Dialog Demo
   **********/
  btnList = (Button) findViewById(R.id.btnList);
  btnList.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    // TODO Auto-generated method stub
    AlertDialog.Builder builder = new AlertDialog.Builder(
      AllDialogActivity.this);

    CharSequence[] items = { "Red", "Green", "Blue" };
    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:
       tv.setTextColor(Color.RED);
       break;
      case 1:
       tv.setTextColor(Color.GREEN);
       break;
      case 2:
       tv.setTextColor(Color.BLUE);
       break;
      }
     }
    });

    AlertDialog alert = builder.create();
    alert.show();
   }
  });

  /********
   * Progress Dialog Demo
   **********/
  prgDilog = (Button) findViewById(R.id.btnProgress);
  prgDilog.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    // TODO Auto-generated method stub
    final ProgressDialog myProgressDialog = ProgressDialog.show(
      AllDialogActivity.this, "Please wait...(5sec)",
      "Doing Extreme Calculations...", true);

    new Thread() {
     public void run() {
      try {
       // Do some Fake-Work
       sleep(5000);
      } catch (Exception e) {
      }
      // Dismiss the Dialog
      myProgressDialog.dismiss();
     }
    }.start();
   }
  });

  /********
   * DatePicker Dialog Demo
   **********/
  btnDtPicker = (Button) findViewById(R.id.btnDtPicker);
  btnDtPicker.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {
    // TODO Auto-generated method stub

    showDialog(0);
   }
  });

  /********
   * Character Picker Dialog Demo
   **********/
  btnChButton = (Button) findViewById(R.id.btnChPicker);
  btnChButton.setOnClickListener(new View.OnClickListener() {

   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    cpd_init();
    cpd.show();
   }
  });

 }

 //*** Overrided for Intialize DatePicker Dialog***/
 @Override
 protected Dialog onCreateDialog(int id) {
  Calendar c = Calendar.getInstance();
  year = c.get(Calendar.YEAR);
  month = c.get(Calendar.MONTH);
  day = c.get(Calendar.DAY_OF_MONTH);
  // TODO Auto-generated method stub
  switch (id) {
  case 0:
   return new DatePickerDialog(AllDialogActivity.this,
     datePickerListener, year, month, day);
  }
  return new DatePickerDialog(AllDialogActivity.this, datePickerListener,
    year, month, day);
 }

 //**** DatePicker Set Listener which call when we click on Set Button ****//
 private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

  // when dialog box is closed, below method will be called.
  public void onDateSet(DatePicker view, int selectedYear,
    int selectedMonth, int selectedDay) {

   year = selectedYear;
   month = selectedMonth;
   day = selectedDay;

   // set selected date into textview
   tv.setText(new StringBuilder().append(day).append("-")
     .append(month + 1).append("-").append(year).append(" "));

   // set selected date into datepicker also
   // dpResult.init(year, month, day, null);

  }
 };

 // Character Picker Dialog initialization
 void cpd_init() {
  EditText et = new EditText(this);
  et.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
  final String options = "0123456789ABCDEF";
  cpd = new CharacterPickerDialog(this, new View(this), null, options,
    false) {
   public void onClick(View v) {
    tv.setText("????:"+((Button)v).getText().toString());
    dismiss();
   }
  };

 }

} 

The Execution of Code as follows..

Initially Run the Project display the following Screen...
 Then When You Click on "Alert Dialog" Button that will show the following Dialog which ask for "Are you Sure you want to Exit" if you Press "Yes" than you will exit from this apps and if you say "No" than you will continue with this apps..
 This dialog Box will appears when you click on "List Dialog"  Button that ask you to Select any Color when you chose any color that Color will be set as Text color of TextView above displayed
 This dialog box allows you to Select a date when you select date and say "Set" that Date date set in TextView.
 This Progress Dialog box will simple wait for 5sec you can not perform any activity during this time and you can not cancel this dialog.
 This dialog Allows to you to Select any Character From this..

19 comments:

  1. ok thanks for this post it's quite informative and I have learned new things.
    alternatives to kissanime

    ReplyDelete
  2. Tech Gadgets reviews and latest Tech and Gadgets news updates, trends, explore the facts, research, and analysis covering the digital world.
    You will see Some Tech reviews below,

    lg bluetooth headset : You will also wish to keep design and assorted features in mind. The most essential part of the design here is the buttonsof lg bluetooth headset .

    Fastest Car in the World : is a lot more than the usual number. Nevertheless, non-enthusiasts and fans alike can’t resist the impulse to brag or estimate according to specifications. Fastest Car in the World click here to know more.

    samsung galaxy gear : Samsung will undoubtedly put a great deal of time and even more cash into courting developers It is looking for partners and will allow developers to try out
    different sensors and software. It is preparing two variants as they launched last year. samsung galaxy gear is very use full click to know more.

    samsung fridge : Samsung plans to supply family-oriented applications like health care programs and digital picture frames along with games It should stick with what they know and they
    do not know how to produce a quality refrigerator that is worth what we paid. samsung fridge is very usefull and nice product. clickcamera best for travel: Nikon D850: Camera It may be costly, but if you’re trying to find the very best camera you can purchase at this time, then Nikon’s gorgeous DX50 DSLR will
    probably mark each box. The packaging is in a vibrant 45.4-megapixel full-frame detector, the picture quality is simply wonderful. However, this is just half the story. Because of a complex 153-point AF system along with a brst rate of 9 frames per minute. camera best specification. click here to know more.

    visit https://techgadgets.expert/ this site to know more.

    ReplyDelete
  3. ez battery reconditioning reviews - How to Recondition a battery .

    ez battery reconditioning reviews - You can now easily revive your old batteries with
    this ez battery reconditioning pdf which provides step by step instructions ... for How to recondition a battery

    For more information Visit https://ezbatteryreconditioninginfo.com/ here .


    ReplyDelete
  4. Talk with Strangerstalk to strangers in Online Free Chat rooms where during a safe environment.
    From friendships to relationships.omegle teen Talk With Stranger is that the best online chatting site.
    Its the simplest alternative to airg chat, Badoo , omegle & mocospace. If you're keen on speaking
    with people on the web ,chat random or want to seek out omegle girls, do free texting or sexting, make new friends.
    you'll find your omegle lady here. Please note this is often not a sexting site so you can't do sexting
    online. this is often a familychatous friendly chat site. we've voice chat if you would like to try to to phone
    chat online. Our most viral is that the 1-1 one on one random chat.talkwithstranger No check in on login needed.
    we've teengers also asanonymous chat older people that want to satisfy new people. Online random chat is that the best
    chatrandom alternative.

    ReplyDelete
  5. Compre documentos en línea, documentos originales y registrados.
    Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

    Comprar permiso de residencia,
    permiso de residenciareal y falso en línea,
    Compre licencia de conducir en línea,
    Compre una licencia de conducir española falsa en línea,
    Comprar tarjeta de identificación,
    Licencia de conducir real y falsa,
    Compre pasaporte real en línea,

    Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
    Address: 56 Guild Street, London, EC4A 3WU (UK)
    Email: contact@permisodeespana.com
    WhatsApp: +443455280186

    ReplyDelete