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

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

এই টিউটোরিয়ালে আমরা শিখব কীভাবে AlertDialog.Builder এবং কাস্টম লেআউট ব্যবহার করে একটি কাস্টম ডায়লগ বক্স তৈরি করতে হয় যেখানে ব্যবহারকারী ইনপুট দিতে পারবে।

প্রয়োজনীয় ধাপগুলো

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

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

ধাপ ২: মেইন লেআউট ফাইল তৈরি করুন

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">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="My Custom Message"
        android:textSize="22sp"
        android:textAlignment="center"
        android:layout_marginTop="30dp"
        android:id="@+id/myCustommessage"/>
    <Button
        android:layout_width="140dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:text="Show Message"
        android:backgroundTint="@color/colorPrimary"
        android:textSize="20dp"
        android:textColor="#ffffff"
        android:onClick="btn_showMessage" />
</RelativeLayout>

ধাপ ৩: কাস্টম ডায়লগ লেআউট তৈরি করুন

Project প্যানেলে res/layout → Right click → New → Layout resource file → নাম: custom_dialog, Root element: LinearLayout

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

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enter your Message"
        android:textSize="22sp"
        android:textAlignment="center" />
    <EditText
        android:id="@+id/txt_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Enter your Message"
        android:textSize="20sp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimary"
            android:text="Cancel"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:layout_weight="1"
            android:id="@+id/btn_cancel"/>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimary"
            android:text="Okay"
            android:textColor="#ffffff"
            android:textSize="18sp"
            android:layout_weight="1"
            android:id="@+id/btn_okay"/>
    </LinearLayout>
</LinearLayout>

ধাপ ৪: মেইন এক্টিভিটিতে ডায়লগ লজিক বাস্তবায়ন করুন

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

package app.com.sample;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    TextView myCustomMessage;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myCustomMessage = findViewById(R.id.myCustommessage);
    }
    
    public void btn_showMessage(View view) {
        AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
        View mView = getLayoutInflater().inflate(R.layout.custom_dialog, null);
        
        final EditText txtInputText = mView.findViewById(R.id.txt_input);
        Button btnCancel = mView.findViewById(R.id.btn_cancel);
        Button btnOkay = mView.findViewById(R.id.btn_okay);
        
        alert.setView(mView);
        final AlertDialog alertDialog = alert.create();
        alertDialog.setCanceledOnTouchOutside(false);
        
        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        
        btnOkay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myCustomMessage.setText(txtInputText.getText().toString());
                alertDialog.dismiss();
            }
        });
        
        alertDialog.show();
    }
}

ধাপ ৫: ম্যানিফেস্ট কনফিগার করুন

AndroidManifest.xml ফাইল:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.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>

আধুনিক বিকল্প: MaterialAlertDialogBuilder (সুপারিশ করা)

Material Components লাইব্রেরি ব্যবহার করলে:

implementation 'com.google.android.material:material:1.10.0'
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
builder.setView(mView)
       .setCancelable(false)
       .show();

DialogFragment ব্যবহার করা (সর্বোত্তম অনুশীলন)

কনফিগারেশন চেঞ্জ (রোটেশন) হ্যান্ডল করার জন্য DialogFragment ব্যবহার করুন:

public class CustomDialogFragment extends DialogFragment {
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(requireActivity());
        View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
        // ভিউ বাইন্ডিং এবং লিসেনার সেটআপ
        builder.setView(view);
        return builder.create();
    }
}

// দেখানোর জন্য:
new CustomDialogFragment().show(getSupportFragmentManager(), "custom_dialog");

সারাংশ

কাস্টম লেআউট ইনফ্লেট করে AlertDialog.Builder.setView() দিয়ে কাস্টম ডায়লগ তৈরি করা সহজ। প্রোডাকশন অ্যাপে DialogFragment বা MaterialAlertDialogBuilder ব্যবহার করা উচিত।

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

  2. অ্যান্ড্রয়েডে কাস্টম রেটিংবার (RatingBar) তৈরি করা

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

  4. আউটলুকে কাস্টম ভিউ তৈরি করার পদ্ধতি