Android dialogs


Android dialogs 


  • Introduction to android dialogs in Hindi
  • Creating android dialogs in Hindi 
  • Adding buttons to android dialogs in Hindi
  • Adding lists to android dialogs in Hindi


Introduction to android dialogs 

Dialog एक छोटी सी window होती है जिससे यूज़र कोई decision ले सकता है या कुछ information input कर सकता है। Dialog पूरी screen को cover नहीं करता है।

 Android dialogs 3 तरह के होते है -
  1. Alert dialog - ये एक simple alert dialog होता है। इस तरह के dialog में आप title शो कर सकते है, 3 बटन add कर सकते है और items की list भी शो कर सकते है जिन्हे यूज़र select कर सकता है। ये dialog क्रिएट करने के लिए आपको AlertDialog क्लास को extend करना पड़ेगा।  
2.

               2.Date picker dialog - इस तरह के dialog से आप यूज़र को एक window शो कर सकते है जिससे यूज़र date select कर सकता है। इस तरह का dialog क्रिएट करने के लिए आपको DatePickerDialog क्लास को extend करना होगा।   

3.



             3.Time picker dialog  - इस तरह के dialog से यूज़र time select कर सकता है। इस तरह का dialog क्रिएट करने के लिए आपको TimePickerDialog क्लास को extend करना होगा।    



ये सभी classes Dialog क्लास को extend करती है। इन सभी dialog क्लास को यूज़ करने से पहले आपको अपने dialog के लिए एक container क्रिएट करना होगा।  इसके लिए आप DialogFragment क्लास को extend कर सकते है। DialogFragment क्लास dialog create करने और उसको मैनेज करने के लिए जरुरी methods प्रोवाइड करती है। 


Creating android dialogs   

Android में dialogs क्रिएट करना बहुत ही आसान है। इसके लिए आपको 2 या 3 statements लिखने होते है।

Example

AlertDialog.Builder yrobj = new AlertDialog.Builder(getActivity()); //creating alert dialog

yrobj.setMessage(R.string.yrmessage); //setting message

yrobj.setTitle(R.string.yrtitle); // setting title 

AlertDialog ad = yrobj.create(); // getting alert dialog from AlertDialog.Builder 

Dialog क्रिएट करने से पहले आपको dialog fragment क्रिएट करना होगा। इसके लिए आप DialogFragment क्लास को extend करते है। इस क्लास में आप onCreateDialog() मेथड को implement करते है। और इसी मेथड में आप dialog क्रिएट करते है। Dialog क्रिएट करने के लिए आप Builder क्लास यूज़ कर सकते है।

Example         

public class yrclass extends DialogFragment
{

    @Override
      public Dialog onCreateDialog(Bundle savedInstanceState)
      {

            // create yr dialog here as shown above  

      }

}

जब आप इस क्लास के object पर show() मेथड कॉल करते है तो dialog शो हो जाता है।

  

Adding buttons to android dialogs

Android alert dialogs में आप 3 तरह के buttons add कर सकते है-

  1. Positive button  - ये button positive response के लिए यूज़ किया जाता है। जैसे की ok या yes। इस तरह का बटन create करने के लिए आपको setPositiveButton() method को call करना होगा। इस मेथड को आप alert dialog के object पर कॉल करते है। इस मेथड में 2 arguments पास होते है एक तो button का नाम और दूसरा DialogInterface का object। ये interface onClickListener() मेथड provide करता है। जिसको आप implement करते है।       
  2. Negative button - ये button negative response के लिए यूज़ किया जाता है। जैसे की no या cancel। इस तरह का button dialog में add करने के लिए आपको alert dialog के ऑब्जेक्ट पर setNegativeButton() call करना होता है। 
  3. Neutral button - ये एक simple button होता है जिससे आप कोई दूसरा action ले सकते है। इस तरह के button को जैसे simply android में button create किया जाता है वैसे ही create करते है। 


Example

DialogInterface.onClickListerner di = new DialogInterface.onClickListener(); // creating dialog interface object for event handling

yrobj.setPositiveButton(R.string.button-name,  di); // setting positive button

public void onClick(DialogInterface di, int id)
{

     //handle on click event here 

}

इसी तरह आप negative button भी क्रिएट कर सकते है।



Adding lists to android dialogs  

Android alert dialogs में आप 3 तरह की list add कर सकते है -
  1. Single choice list - इस तरह की list में से यूज़र कोई सा भी एक item select कर सकता है। Item select पर event generate होता है और code के according action लिया जाता है।
 

2.Single choice radio list - इस तरह की लिस्ट में यूज़र को list radio buttons के साथ show की जाती है। User कोई सा भी एक यतीम select कर सकता है। 


3.Multiple choice check-box list - इस तरह की list में से यूज़र एक साथ कई items select कर सकता है। यूज़र को list checkboxes के साथ show होती है। 

Alert dialogs में लिस्ट क्रिएट करने के लिए आपको setItems() method कॉल करना होगा। इस method में 2 arguments पास किये जाते है एक तो list items का array और दूसरा DialogInterface.onClickListener interface को object जैसा की हमने ऊपर button add करने के लिए किया था।

DialogInterface.onClickListener इंटरफ़ेस की onClick मेथड को implement करके आप उसमे select event को हैंडल करते है। जैसा की ऊपर setPositiveButton() method के साथ आपने किया था।  आइये setItems मेथड का structure देखते है -

yrobj.setItems(R.Array.yrarray, DialogInterface.onClickListener di);

Comments

Add2

Popular posts from this blog

Android menus

Android broadcast receivers