एंड्रॉइड में शीर्षक रहित डायलॉग बॉक्स बनाना
यह उदाहरण दिखाता है कि AlertDialog का उपयोग करके बिना शीर्षक वाला डायलॉग कैसे बनाया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML
res/layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="180dp"
android:layout_height="50dp"
android:text="AlertDialog डेमो" />
<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"
android:text="डेटा साफ़ करें" />
</RelativeLayout>
चरण 3: MainActivity में डायलॉग लॉजिक
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button btnClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnClear = findViewById(R.id.btnClear);
btnClear.setOnClickListener(v -> showConfirmDialog());
}
private void showConfirmDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// शीर्षक सेट न करके हम शीर्षक रहित डायलॉग बनाते हैं
// builder.setTitle("पुष्टि"); // यह लाइन हटाएं या कमेंट करें
builder.setMessage("क्या आप टेक्स्ट साफ़ करना चाहते हैं?");
builder.setPositiveButton("हाँ", (dialog, which) -> {
editText.setText("");
Toast.makeText(this, "टेक्स्ट साफ़ हो गया", Toast.LENGTH_SHORT).show();
});
builder.setNegativeButton("रद्द", (dialog, which) -> dialog.dismiss());
AlertDialog dialog = builder.create();
dialog.show();
}
}
बिना शीर्षक के डायलॉग के लिए अतिरिक्त विकल्प
यदि आप कस्टम लेआउट के साथ पूरी तरह से शीर्षक रहित डायलॉग चाहते हैं:
// कस्टम लेआउट के साथ View customView = getLayoutInflater().inflate(R.layout.custom_dialog, null); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(customView); // कोई शीर्षक सेट न करें AlertDialog dialog = builder.create(); dialog.show();
चरण 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run पर क्लिक करें। "डेटा साफ़ करें" बटन दबाने पर बिना शीर्षक वाला पुष्टि डायलॉग दिखाई देगा।