AlertDialog in Android in Hindi
RGPV University / DIPLOMA_CSE / MOBILE COMPUTING
AlertDialog in Android Explained in Hindi
AlertDialog in Android in Hindi
What is AlertDialog in Android in Hindi
Android में AlertDialog एक ऐसा pop-up window होता है जो user को कोई जानकारी देने या user से confirmation लेने के लिए उपयोग किया जाता है। जब हमें user से कोई निर्णय लेना होता है जैसे कि "Yes" या "No", या कोई चेतावनी (warning) दिखानी होती है, तब AlertDialog का उपयोग किया जाता है। यह Dialog स्क्रीन के ऊपर दिखाई देता है और user के interaction का इंतजार करता है।
Use of AlertDialog in Hindi
- जब user से confirmation लेनी हो जैसे "Are you sure you want to delete this?"
- जब किसी action को करने से पहले चेतावनी (warning) देनी हो
- जब कोई जरूरी जानकारी user को दिखानी हो
- जब multiple options दिखाने हों जैसे "Choose from list"
Types of Buttons in AlertDialog in Hindi
- Positive Button – यह "OK", "Yes" जैसे सकारात्मक action के लिए होता है।
- Negative Button – यह "Cancel", "No" जैसे नकारात्मक action के लिए होता है।
- Neutral Button – यह ऐसा button होता है जो किसी भी direction में नहीं जाता जैसे "Remind Me Later"
Features of AlertDialog in Android in Hindi
- Custom Title और Message सेट कर सकते हैं।
- Buttons जैसे Positive, Negative और Neutral जोड़ सकते हैं।
- Custom layout का उपयोग करके अपने UI को personalize कर सकते हैं।
- List या Multi-Choice Items भी AlertDialog में दिखाए जा सकते हैं।
- AlertDialog user को कोई भी action लेने से पहले रोकता है (modal होता है)।
Creating a Simple AlertDialog in Android in Hindi
अब हम एक साधारण AlertDialog बनाना सीखते हैं जो user से confirmation लेता है:
// Java में AlertDialog बनाने का code
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Delete Item");
builder.setMessage("क्या आप वाकई इस item को delete करना चाहते हैं?");
builder.setPositiveButton("हाँ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// delete action यहां होगा
}
});
builder.setNegativeButton("नहीं", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // dialog को बंद कर देता है
}
});
AlertDialog dialog = builder.create();
dialog.show();
ऊपर दिए गए code में AlertDialog.Builder class का उपयोग करके एक dialog बनाया गया है।
setTitle() और setMessage() methods से dialog का title और message सेट किया जाता है।
setPositiveButton() और setNegativeButton() से दो विकल्प दिए गए हैं जिनमें click listener सेट किया गया है।
dialog.show() से dialog को स्क्रीन पर दिखाया जाता है।
Custom Layout AlertDialog in Hindi
कभी-कभी हम AlertDialog के अंदर एक custom layout भी उपयोग करना चाहते हैं जैसे form या कोई complex design। इसके लिए पहले layout XML file बनानी होती है:
// custom_dialog.xml (layout file)
// Java में custom AlertDialog
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.custom_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialogView);
builder.setTitle("Custom AlertDialog");
builder.setPositiveButton("सबमिट करें", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText input = dialogView.findViewById(R.id.inputField);
String value = input.getText().toString();
// value का उपयोग करें
}
});
builder.setNegativeButton("रद्द करें", null);
AlertDialog dialog = builder.create();
dialog.show();
इस तरीके से हम custom input या कोई भी user interaction AlertDialog में शामिल कर सकते हैं। यह approach तब उपयोगी होती है जब user से कोई जानकारी लेनी हो।