কম্পিউটার টিউটোরিয়াল

এন্ড্রয়েডে কোটলিন ব্যবহার করে কাস্টম Alert Dialog তৈরির পদ্ধতি

Dialog ক্লাস ব্যবহার করে কাস্টম আলার্ট ডায়ালগ তৈরি করা

এই উদাহরণটি দেখায় কিভাবে কোটলিন ব্যবহার করে এন্ড্রয়েডে কাস্টম লেআউট দিয়ে Alert Dialog তৈরি করা যায়। একটি পৃথক XML লেআউট ফাইল (customdialog.xml) ব্যবহার করা হয়েছে।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট 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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="MainActivity">

    <Button
        android:id="@+id/buttonShowCustomDialog"
        style="@android:style/Widget.DeviceDefault.Button.Inset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="131dp"
        android:onClick="exit"
        android:text="ক্লিক করুন"
        android:textStyle="normal|bold" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/buttonShowCustomDialog"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="86dp"
        android:gravity="center"
        android:text="কাস্টম অ্যালার্ট ডায়ালগ দেখতে ক্লিক করুন"
        android:textSize="18sp"
        android:textStyle="normal|bold" />

</RelativeLayout>

ধাপ ৩: customdialog.xml লেআউট ফাইল তৈরি করুন

res/layout/customdialog.xml ফাইল তৈরি করে নিচের কোড যোগ করুন:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="এটি একটি কাস্টম ডায়ালগ"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_gravity="center"
        android:layout_marginBottom="16dp"/>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ঠিক আছে"
        android:layout_gravity="center"/>

</LinearLayout>

ধাপ ৪: MainActivity.kt ফাইলে লজিক লিখুন

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private val context: Context = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }

    fun exit(view: View) {
        val dialog = Dialog(context)
        dialog.setContentView(R.layout.customdialog)
        val dialogButton = dialog.findViewById<Button>(R.id.dialogButtonOK)
        dialogButton.setOnClickListener {
            dialog.dismiss()
            Toast.makeText(applicationContext, "Dismissed..!!", Toast.LENGTH_SHORT).show()
        }
        dialog.show()
    }
}

ধাপ ৫: AndroidManifest.xml আপডেট করুন

সাধারণ ম্যানিফেস্ট কনফিগারেশন যথেষ্ট।

অ্যাপ্লিকেশন চালানোর পদ্ধতি

ডিভাইস সংযুক্ত করে Run করুন। "ক্লিক করুন" বাটনে ক্লিক করলে কাস্টম ডায়ালগ দেখাবে। "ঠিক আছে" বাটনে ক্লিক করলে ডায়ালগ বন্ধ হবে।

  1. অ্যান্ড্রয়েড অ্যাপে কাস্টম অ্যালার্ট ডায়ালগ তৈরির পদ্ধতি

  2. অ্যান্ড্রয়েড অ্যাপে TextToSpeech তৈরি করার পদ্ধতি

  3. Facebook-এ Android অ্যাপ তৈরি করার পদ্ধতি

  4. Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি