एंड्रॉइड में AlertDialog को बाहर क्लिक करके बंद करना
यह ट्यूटोरियल दर्शाता है कि setCanceledOnTouchOutside(true) का उपयोग करके डायलॉग के बाहर टच करने पर उसे कैसे डिसमिस किया जाए।
लेआउट (activity_main.xml)
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="डायलॉग दिखाने के लिए क्लिक करें"
android:textSize="24sp" />
</LinearLayout>
MainActivity.java
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.click);
textView.setOnClickListener(v -> showAlertDialog());
}
private void showAlertDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("पुष्टि");
builder.setMessage("क्या आप इस डायलॉग को बंद करना चाहते हैं?");
builder.setPositiveButton("हाँ", (dialog, which) ->
Toast.makeText(this, "आपने 'हाँ' पर क्लिक किया", Toast.LENGTH_SHORT).show());
builder.setNegativeButton("नहीं", (dialog, which) ->
dialog.dismiss());
AlertDialog dialog = builder.create();
// महत्वपूर्ण: बाहर क्लिक करने पर डिसमिस इनेबल करें
dialog.setCanceledOnTouchOutside(true);
// वैकल्पिक: बैक बटन पर भी डिसमिस (डिफ़ॉल्ट true)
dialog.setCancelable(true);
dialog.show();
}
}
महत्वपूर्ण मेथड्स
| मेथड | विवरण | डिफ़ॉल्ट |
|---|---|---|
setCanceledOnTouchOutside(true) |
डायलॉग के बाहर टच करने पर बंद करता है | false |
setCancelable(true) |
बैक बटन दबाने पर बंद करता है | true |
कस्टम डायलॉग (DialogFragment) के साथ
import android.app.Dialog;
import android.os.Bundle;
import androidx.fragment.app.DialogFragment;
public class CustomDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("कस्टम डायलॉग")
.setMessage("यह एक DialogFragment है")
.setPositiveButton("OK", null);
Dialog dialog = builder.create();
dialog.setCanceledOnTouchOutside(true);
return dialog;
}
}
// उपयोग:
new CustomDialogFragment().show(getSupportFragmentManager(), "custom_dialog");
कस्टम व्यू के साथ डायलॉग
View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
AlertDialog dialog = new AlertDialog.Builder(this)
.setView(customView)
.create();
dialog.setCanceledOnTouchOutside(true);
// कस्टम व्यू में बटन क्लिक
customView.findViewById(R.id.btn_close).setOnClickListener(v -> dialog.dismiss());
dialog.show();
नोट: फुल-स्क्रीन डायलॉग के लिए
यदि डायलॉग फुल-स्क्रीन है (उदाहरण के लिए, android:theme="@style/Theme.AppCompat.Dialog.NoActionBar" के साथ), तो बाहर कोई क्षेत्र नहीं होता, इसलिए setCanceledOnTouchOutside का कोई प्रभाव नहीं होगा।