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

Android-এ AlertDialog-এ কাস্টম ভিউ যোগ করার উপায়

AlertDialog-এ কাস্টম ভিউ

এই উদাহরণে দেখানো হয়েছে কিভাবে AlertDialog-এ কাস্টম ভিউ (Custom Layout) যোগ করা যায়।

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

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

ধাপ ২: লেআউট ফাইল

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/click"
        android:layout_width="wrap_content"
        android:textSize="30sp"
        android:layout_height="wrap_content"
        android:text="Click" />
</LinearLayout>

ধাপ ৩: MainActivity.java ফাইল

src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন:

package com.example.myapplication;

import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    TextView text;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = findViewById(R.id.click);
        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });
    }

    private void showAlertDialog() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
        final View customLayout = getLayoutInflater().inflate(R.layout.custom, null);
        alertDialog.setView(customLayout);

        alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // ডায়লগ থেকে ডেটা এক্টিভিটিতে পাঠানো
                EditText editText = customLayout.findViewById(R.id.editText);
                Toast.makeText(MainActivity.this, editText.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });

        AlertDialog alert = alertDialog.create();
        alert.setCanceledOnTouchOutside(false);
        alert.show();
    }
}

ধাপ ৪: কাস্টম লেআউট (custom.xml)

res/layout/custom.xml ফাইলটি তৈরি করুন (আর্টিকেলে দেওয়া নেই, কিন্তু সাধারণত EditText থাকবে):

<?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">
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter text here" />
</LinearLayout>

অ্যাপ্লিকেশন চালানোর ফলাফল

অ্যাপটি রান করলে প্রথমে নিচের স্ক্রিনটি দেখাবে:

Android-এ AlertDialog-এ কাস্টম ভিউ যোগ করার উপায়

TextView-এ ক্লিক করলে কাস্টম ভিউসহ AlertDialog প্রদর্শিত হবে:

Android-এ AlertDialog-এ কাস্টম ভিউ যোগ করার উপায়

OK বাটনে ক্লিক করলে ইনপুট টেক্সট টোস্ট হিসেবে দেখাবে:

Android-এ AlertDialog-এ কাস্টম ভিউ যোগ করার উপায়

  1. অ্যান্ড্রয়েডে ActionBar-এ কাস্টম ভিউ দেখানোর পদ্ধতি

  2. অ্যান্ড্রয়েডে কাস্টম লিস্ট ভিউতে সার্চ ফাংশনালিটি কীভাবে ব্যবহার করবেন?

  3. অ্যান্ড্রয়েডে কাস্টম AlertDialog ভিউ কীভাবে বাস্তবায়ন করবেন?

  4. অ্যান্ড্রয়েডে কাস্টম জেসচার যোগ করার পদ্ধতি