Alert Dialog क्या है?
Alert Dialog एक छोटा पॉप-अप विंडो है जो उपयोगकर्ता को निर्णय लेने के लिए प्रेरित करता है (जैसे "हाँ/नहीं", "ओके/कैंसिल")। यह वर्तमान एक्टिविटी के ऊपर दिखाई देता है और उपयोगकर्ता के इंटरैक्शन की प्रतीक्षा करता है।
AlertDialog.Builder के मुख्य मेथड्स
- setTitle(CharSequence) - डायलॉग का शीर्षक सेट करता है
- setMessage(CharSequence) - संदेश सामग्री सेट करता है
- setIcon(int resId) - आइकन सेट करता है
- setPositiveButton(text, listener) - सकारात्मक बटन (जैसे "हाँ")
- setNegativeButton(text, listener) - नकारात्मक बटन (जैसे "नहीं")
- setNeutralButton(text, listener) - तटस्थ बटन (जैसे "बाद में")
- setView(View) - कस्टम लेआउट सेट करता है
- setCancelable(boolean) - बैक बटन से बंद होने की अनुमति
उदाहरण: बेसिक Alert Dialog
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/showDialogBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="डायलॉग दिखाएं"
android:layout_gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button showDialogBtn = findViewById(R.id.showDialogBtn);
showDialogBtn.setOnClickListener(v -> showAlertDialog());
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("पुष्टि करें");
builder.setMessage("क्या आप वाकई बाहर निकलना चाहते हैं?");
builder.setIcon(R.drawable.ic_warning);
builder.setPositiveButton("हाँ", (dialog, which) -> {
Toast.makeText(this, "हाँ पर क्लिक किया गया", Toast.LENGTH_SHORT).show();
finish(); // एक्टिविटी बंद करें
});
builder.setNegativeButton("नहीं", (dialog, which) -> {
Toast.makeText(this, "नहीं पर क्लिक किया गया", Toast.LENGTH_SHORT).show();
dialog.dismiss();
});
builder.setNeutralButton("बाद में", (dialog, which) -> {
dialog.dismiss();
});
builder.setCancelable(false); // बैक बटन से बंद न हो
AlertDialog dialog = builder.create();
dialog.show();
}
}
कस्टम लेआउट के साथ Alert Dialog
// custom_dialog.xml लेआउट बनाएं
View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
EditText input = customView.findViewById(R.id.inputField);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(customView)
.setTitle("जानकारी दर्ज करें")
.setPositiveButton("सबमिट", (dialog, which) -> {
String text = input.getText().toString();
// डेटा प्रोसेस करें
})
.setNegativeButton("रद्द", null)
.show();
महत्वपूर्ण टिप्स
- हमेशा
AlertDialog.Builderका उपयोग करें, सीधेnew AlertDialog()न बनाएं। - डायलॉग दिखाने के बाद
dialog.dismiss()याdialog.cancel()का उपयोग करें। - लंबे ऑपरेशन के लिए ProgressDialog (डिप्रिकेटेड) के बजाय ProgressBar के साथ कस्टम डायलॉग उपयोग करें।