Skip to main content

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. Date picker dialog - इस तरह के dialog से आप यूज़र को एक window शो कर सकते है जिससे यूज़र date select कर सकता है। इस तरह का dialog क्रिएट करने के लिए आपको DatePickerDialog क्लास को extend करना होगा।   
  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 लिखने होते है।


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 क्लास यूज़ कर सकते है।


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 करते है। 



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 कोई सा भी एक item 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

Popular posts from this blog

Android Services

Android Services Introduction to  android services in Hindi  Declaring  services in Hindi Creating a  android service in Hindi Introduction to Android Services इससे पहले की आप जाने की service क्या होती है आइये जानते है की service क्या नहीं होती है - एक service separate प्रोसेस नहीं होती है। ये आपकी एप्लीकेशन का ही पार्ट होती है।  एक service thread नहीं होती है।  आइये अब देखते है service क्या होती है - Service एक facility होती है जिससे आप android system को बता सकते है की आप कोई काम background में करते रहना चाहते है।  Service एक facility होती है जिससे एक एप्लीकेशन अपनी कुछ functionality दूसरी applications को expose कर सकती है।    हर service क्लास का <service> declaration AndroidManifest.xml file में होना चाहिए। Services को आप context.startService() और context.bindService() मेथड्स से स्टार्ट कर सकते है।            जब service component क्रिएट होता है तो android syst...

Android Activity

Android Activity Activity life cycle in Hindi Starting an  android activity in Hindi Creating a new activity in Hindi Destroying an  android activity in Hindi Introduction to Activity आप की application में एक से ज्यादा activity हो सकती है। जब आप इन activities के through navigate करते है, यानि एक activity से दूसरी activity में जाते है तो activity अपनी life cycle की different stages में जाती है। उदाहरण के लिए जब आपकी activity start होती है तो ये foreground में होती है और user का पूरा focus इस पर होता है। इस process के दौरान  android  system कुछ life cycle methods को call करता है। यदि user कोई दूसरी activity start करता है तो android system उस activity के life cycle methods को call करता है और आपकी पहले वाली activity background में चली जाती है। life cycle methods में आप decide कर सकते है की जब user जाता है या वापस आता है तो आपकी activity कैसे behave करेगी।  उदाहरण के लिए यदि आप एक वीडियो player बना रहे है तो जब user किसी दूसरी activity पर जाये तो...

Android Spinners

Android Spinners  Introduction to  android spinners in Hindi Defining  android spinners in Hindi Creating spinner items array in Hindi Responding to spinner selections in Hindi    Introduction to Android Spinners Android spinners आपकी एप्लीकेशन में एक drop down menu होती है। इस drop down menu में बहुत से items होते है जिन्हे user choices कहते है। जब user spinner पर क्लिक करता है तो ये items शो होते है। यूज़र अपने लिए appropriate choice choose करता है। By default spinner में सबसे top वाला element शो होता है। Spinners को XML layout file में क्रिएट किया जाता है। और items की लिस्ट string resource file में क्रिएट की जाती है। जिसमे आप एक string array क्रिएट करते है।  Spinner और items को जोड़ने के लिए एक array adapter क्रिएट किया जाता है। इसके बाद selection events को respond करने के लिए OnItemSelectedListener interface को  इम्प्लीमेंट किया जाता है।  Defining Android Spinners Android spinners को आप XML l...