android dialogs pdf by apk855

VIEWS: 20 PAGES: 22

More Info
									            DIALOGS


                 Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-1
                                                     reserved
                                  Introduction

     • A dialog is usually a small window that appears in
       front of the current UI of your application.
     • The underlying activity loses its focus and the dialog
       will interact with the user.
     • Dialogs are normally used for notifications that
       should interrupt the the user and to perform short
       tasks that directs relate to the application progress.
     • Dialogs are useful in many situations.
             Inform the user of some event or progress. ( Example :“you
              have a mail”, “Downloading Message 1 of 1000” etc…)
             Force the user to confirm an action ( Example: “Are you
              sure you want to delete” etc)
             Prompt the user for further information and collect it.

                                                  Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-2
                                                                                      reserved
                                     Dialog Class

     • Dialog class is the base class for creating dialogs.
     • The Dialog class is having the following sub classes.
               AlertDialog.
               Progress Dialog.
               DatePicker Dialog.
               TimePicker Dialog.
     • We can create any one of the above dialogs to
       interact with our activity.
     • The difference between the Toast message and the
       Dialog is : Toast message is not interact able.
     • Toast messages are called as a light weight
       informational notifications.

                                                    Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-3
                                                                                        reserved
                                Dialog structure

     • Dialogs have a number of components, most of
       which are optional.
     • A basic dialog has:
             A title
             A message
             Buttons to collect user responses ( ex: Yes , No , or Cancel )




                                                     Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-4
                                                                                         reserved
                                   AlertDialog
     • AlertDialog is used to display some alerts to the user
       before performing some important functionality by
       the user.
     • AlertDialog is a subclass of Dialog.
     • We have the following types of AlertDialogs.
             Simple AlertDialog.
             AlertDialog with a list of selected items.
             AlertDialog with checkboxes and RadioButtons
     • Creating an AlertDialog has the following steps.
             Overriding the createDialog(int) method of Dialog class.
             Using the AlertDialog.Builder object
             Use the public methods of AlertDialog.Builder object to set
              the properties of the Dialog.
             Call the create() method which returns the AlertDialog.
             Call showDialog(int) method to show the dialog in the
              activity.
                                                   Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-5
                                                                                       reserved
                 Simple AlertDialog Example
   public AlertDialog onCreateDialog(int id){
   AlertDialog ad=null;
   switch(id){
   case 1:
   AlertDialog.Builder adb=new AlertDialog.Builder(this);
   adb.setTitle("Alert Dialog Box");
   adb.setMessage("This is an Example of Alert Dialog Box");
   adb.setIcon(R.drawable.icon);
   adb.setPositiveButton("OK", new
     DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface dialog, int which) {
   // TODO Auto-generated method stub
   Toast.makeText(AlertDialgActivity.this, "OK is pressed",
     Toast.LENGTH_SHORT).show();
   }
   }); //setPositvieButton
                                          Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-6
                                                                              reserved
     adb.setNegativeButton("CANCEL", new
       DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int
        which) {
     // TODO Auto-generated method stub
     Toast.makeText(AlertDialgActivity.this, "Cancel is
        Pressed", Toast.LENGTH_SHORT).show();
     }
     });
       ad=adb.create();
     } //switch
     return ad;
     }
                                       Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-7
                                                                           reserved
            Output on the Emulator




                            Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-8
                                                                reserved
            AlertDialog with a list of Selected Items
   final CharSequence[] items = {"Red", "Green",
   "Blue"};

   AlertDialog.Builder builder = new
   AlertDialog.Builder(this);
   builder.setTitle("Pick a color");
   builder.setItems(items, new
   DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int
   item) {
           Toast.makeText(getApplicationContext(),
   items[item], Toast.LENGTH_SHORT).show();
       }
   });
   AlertDialog alert = builder.create();

                                       Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-9
                                                                           reserved
                 AlertDialog with a single choice

     • To have a single choice, instead of calling setItems() , we ll
       call setSingleChoiceItems() method.
     CharSequence []cs={"Excellent","Good","Average","Bad"};
     adb.setSingleChoiceItems(cs, 1, dialogOnClickListener);

     DialogInterface.OnClickListener dialogOnClickListener =new
        DialogInterface.OnClickListener() {

     @Override
     public void onClick(DialogInterface dialog, int which) {
     // TODO Auto-generated method stub

     Toast.makeText(AlertSingleChoiceActivity.this, "AlertSingle
        choice selected :"+which, Toast.LENGTH_SHORT).show();
     }
     };
                                               Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-10
                                                                                   reserved
             Output on the Emulator




                             Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-11
                                                                 reserved
              AlertDialog with Multiple Selected Items
     •   To have multiple choices, instead of calling setItems() , we ll call
         setSingleChoiceItems() method.
     CharSequence []cs={"B.Tech","M.TEch","MCA","BCA"};
     adb.setMultiChoiceItems(cs, null, dialogOnClickListener);

     DialogInterface.OnMultiChoiceClickListener mMultipleSelected = new
        DialogInterface.OnMultiChoiceClickListener() {
     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
     if(isChecked){
     Toast.makeText(AllSortsOfDialogs.this,"Item = "+Integer.toString(which)+"is
        checked ",Toast.LENGTH_LONG).show();
     issel[which]=true;
     }

     else{
     Toast.makeText(AllSortsOfDialogs.this,"Item = "+Integer.toString(which)+"is
        UN checked ",Toast.LENGTH_LONG).show();
     issel[which]=false;
     }
     }
     };


                                                       Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-12
                                                                                           reserved
                               Progress Dialog
     •   A progress dialog is displayed to the user, when any
         application or any task is in progression.
     •   A progress dialog is displayed as a progress animation in the
         form of spinning wheel, when the progress of the of a task is
         undefined, meaning exactly we don’t know howmuch time it will
         take.
     •   A progress bar is displayed to the user as a horizontal view , for
         a task when the progress is defined.
     •   The dialog can also provide buttons, such as one to cancel to
         download.
     • ProgressDialog                        ProgressBar




                                                   Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-13
                                                                                       reserved
                                 ProgressDialog

     • ProgressDialog class is an extension of the
       AlertDialog class that can display a progress bar.
     • Opening a progress dialog can be as simple as
       calling ProgressDialog.show().
     • ProgressDialog is not needed to be configured by
       calling onCreateDialog(int) method.
     • Example:
             ProgressDialog dialog=ProgressDialog.show(Activity.this,” “,”
               Loading.. Please Wait..”,”true”);
              First Parameter: applicationContext()
              Second Parameter : Title for the dialog.
              Third Parameter : Message
              Fourth Parameter : Whether the progress is intermediate. It
                                  is mostly used with progressbar.
                                                    Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-14
                                                                                        reserved
                                   ProgressBar
     • The above example shows the Progress dialog with
       the spinning wheel, without the guarantee of
       progression.
     • If you want to display the progress with horizontal
       bar, we use the progress bar.
     • Working with progress bar
              Initialize the ProgressDialog with the class Constructor.
              ProgressDialog(Context).
              Set the progress style to “ STYLE_HORIZONTAL ” with
               setProgressStyle(int) and set any other properties ,such as
               the message.
              Call show() dialog to show the dialog.
              Return ProgressBar object to onCreateDialog(int) method.


                                                    Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-15
                                                                                        reserved
                               Custom Dialog
     •   If you want a customized design for a dialog, you can create your
         own layout for the dialog window with layout and widgets element.
     •   After defining your layout, pass the root View object or layout
         resource id to setContentView(View).
     •   <LinearLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/layout_root"
                 android:orientation="horizontal"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
                 android:padding="10dp"
                 >
            <ImageView android:id="@+id/image"
                  android:layout_width="wrap_content"
                  android:layout_height="fill_parent"
                  android:layout_marginRight="10dp"
                  />
            <TextView android:id="@+id/text"
                 android:layout_width="wrap_content"
                 android:layout_height="fill_parent"
                 android:textColor="#FFF"
                 />
         </LinearLayout>
                                                   Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-16
                                                                                       reserved
                             Activity file

     Context mContext = getApplicationContext();
       Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle("Custom Dialog");

        TextView text = (TextView)
        dialog.findViewById(R.id.text);
        text.setText("Hello, this is a custom dialog!");
        ImageView image = (ImageView)
        dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.android);


                                             Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-17
                                                                                 reserved
                             DatePickerDialog

   • DatePickerDialog is a predefined class in android.app
     package.
   • It provides the user to display a date picker component to
     the activity.
   • This component provides the facility to the user to select
     the date .i,e. day, month and year.
   • We can add this date component in two ways:
         By creating an object of DatePickerDialog.
         By creating a widget by configuring in .xml file
   • Creating an object of DatePickerDialog by using the
     following statement.
   • DatePickerDialog dpd=new DatePickerDialog(Activity.this,
     mDateListener, 2011, 12, 28);
                                                  Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-18
                                                                                      reserved
     • Parameters:
              Activity.this : specifies the current Activity object.
              mDateListener : specifies the OnDateSetListener object
                               reference.
              2011          : The year
              12            : The month
              28            : The Day
     • We create the OnDateSetListener object by using the
       following statement.
     DatePickerDialog.OnDateSetListener mDateListener = new
               DatePickerDialog.OnDateSetListener()
     {
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
         Toast.makeText(AllSortsOfDialogs.this, "Selected
               Date="+Integer.toString(year)+" Mon= "+Integer.toString(monthOfYear)+"
        Day= "+Integer.toString(dayOfMonth),Toast.LENGTH_LONG).show();
     }
     };
                                                              Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-19
                                                                                                  reserved
              DatePickerDialog by using .xml file


     • Date Picker Dialog can also be configured to our
       activity through the .xml file.
     • We use <DatePickerDialog> tag to display
       DatePickerDialog.
     • The following example shows the syntax:
     <DatePicker android:id="@+id/datePicker1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:startYear="1998"
           android:endYear="2030“>
     </DatePicker>

                                         Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-20
                                                                             reserved
                                TimePickerDialog

     • TimePickerDialog is used to set the Time.
     • It also can be configured to an activity in two styles:
             • By creating an object of TimePickerDialog object.
             • By using <TimePickerDialog> tag in Activity.xml file.
     • Creating the object.
     TimePickerDialog tpd=new TimePickerDialog(Activity.this,
        mTimeSetListener, 16, 30, true);
     • Parameters:
              Activity.this    : specifies the object of the current activity.
              mTimeSetListener : specifies the OnTimeSetListener object
                                  reference.
              16               : specifies Hours
              30               : specifies Minutes
              True             : specifies whether 24 hours view or not


                                                      Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-21
                                                                                          reserved
                     OnTimeSetListener

     • We use the following code for setting
       OnTimeSetListener object
       TimePickerDialog.OnTimeSetListener
       mTimeSetListener =new
       TimePickerDialog.OnTimeSetListener() {
       public void onTimeSet(TimePicker view, int
                       hourOfDay, int minute) {

        Toast.makeText(AllSortsOfDialogs.this,"Time
        Set to = "+Integer.toString(hourOfDay)+"HRS
        and "+Integer.toString(minute)+"Minutes
        ",Toast.LENGTH_LONG).show();
              }
          };
                                     Copyright © 2012, apkdownloads.inl. All rights
DIALOGS-22
                                                                         reserved

								
To top