পূর্ব প্রস্তুতি
এই উদাহরণটি `AlertDialog.Builder` ব্যবহার করে টাইটেল ছাড়া একটি কনফার্মেশন ডায়ালগ তৈরি করার পদ্ধতি দেখায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করুন।
ধাপ ২: 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">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Alert Dialog without title"
android:textAlignment="center"
android:textColor="@android:color/holo_orange_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: MainActivity.kt-এ ডায়ালগ তৈরি করুন
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val builder = AlertDialog.Builder(this)
// builder.setTitle() কল না করলে টাইটেল থাকবে না
builder.setMessage("Do you want to close the alert dialog without title?")
builder.setPositiveButton("Yes") { dialog, _ ->
dialog.cancel()
Toast.makeText(applicationContext, "Your dialog has been closed", Toast.LENGTH_LONG).show()
}
builder.setNegativeButton("No") { _, _ -> }
val alertDialog = builder.create()
alertDialog.show()
}
}
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.dialognotitle">
<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>
পরীক্ষা করুন
অ্যাপ চালান। অ্যাপ লঞ্চ होते ही টাইটেল ছাড়া একটি ডায়ালগ দেখাবে "Yes"/"No" বাটনসহ।
টিপস
কাস্টম লেআউট ব্যবহার করতে `builder.setView(R.layout.custom_dialog)` কল করুন।