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

Android-এ Alert Dialog প্রদর্শনের পদ্ধতি


Alert Dialog সম্পর্কে জানার আগে, আমাদের জানতে হবে Alert Dialog কী। Alert Dialog একটি পপ-আপের মতো যেখানে ব্যবহারকারী "OK" বা "Cancel" বাটনে ক্লিক করে অ্যাকশন বেছে নিতে পারে।

Alert Dialog-এ ব্যবহৃত মেথডগুলো

  • setView(View view) − Alert Dialog-এ কাস্টম ভিউ সেট করতে ব্যবহৃত হয়

  • setTitle(CharSequence title) − Alert Dialog-এ টাইটেল সেট করতে ব্যবহৃত হয়

  • setMessage(CharSequence message) − Alert বক্সে কন্টেন্ট হিসেবে সিম্পল কল

  • setIcon(int resId) − Alert বক্সের জন্য আইকন সেট করতে ব্যবহৃত হয়

  • setButton(int whichButton, CharSequence text, Message msg) − Alert Dialog-এ বাটন সেট করতে ব্যবহৃত হয়

  • getListView() − Alert Dialog-এর ভিতরে ব্যবহৃত লিস্ট ভিউ পাওয়ার জন্য ব্যবহৃত হয়

এই উদাহরণটি Android Alert Dialog ইমপ্লিমেন্ট করার পদ্ধতি প্রদর্শন করে।

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

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

ধাপ ২: লেআউট ফাইল (res/layout/activity_main.xml)

<?xml version = "1.0" encoding = "utf-8"?> 
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android = "https://schemas.android.com/apk/res/android" 
    xmlns:app = "https://schemas.android.com/apk/res-auto" 
    xmlns:tools = "https://schemas.android.com/tools" 
    android:layout_width = "match_parent" 
    android:layout_height = "match_parent" 
    tools:context = ".MainActivity"> 
    <Button 
        android:id = "@+id/button" 
        android:layout_width = "wrap_content" 
        android:layout_height = "wrap_content" 
        android:text = "Click" 
        app:layout_constraintBottom_toBottomOf = "parent" 
        app:layout_constraintLeft_toLeftOf = "parent" 
        app:layout_constraintRight_toRightOf = "parent" 
        app:layout_constraintTop_toTopOf = "parent" /> 
</androidx.constraintlayout.widget.ConstraintLayout>

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

package com.example.andy.myapplication;

import android.content.DialogInterface;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        Button button = findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                alertDialog();
                break;
        }
    }

    private void alertDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setMessage("অনুগ্রহ করে যেকোনো অপশন সিলেক্ট করুন");
        dialog.setTitle("ডায়ালগ বক্স");
        
        dialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "Yes ক্লিক করা হয়েছে", Toast.LENGTH_LONG).show();
            }
        });
        
        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "Cancel ক্লিক করা হয়েছে", Toast.LENGTH_LONG).show();
            }
        });
        
        AlertDialog alertDialog = dialog.create();
        alertDialog.show();
    }
}

উপরোক্ত কোডে আমরা একটি বাটন তৈরি করেছি, যখন ব্যবহারকারী বাটনে ক্লিক করবে তখন একটি Alert Dialog বক্স দেখাবে, ব্যবহারকারী প্রয়োজন অনুযায়ী OK বা Cancel সিলেক্ট করতে পারবে।

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

আপনার Android ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে অনুমান করে। Android Studio-তে প্রজেক্টের যেকোনো Activity ফাইল খুলে টুলবার থেকে Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিফল্ট স্ক্রিন দেখুন।

Android-এ Alert Dialog প্রদর্শনের পদ্ধতি

এখন উপরের বাটনে ক্লিক করুন, এটি Alert Dialog দেখাবে:

Android-এ Alert Dialog প্রদর্শনের পদ্ধতি

এখন Yes/Cancel বাটন সিলেক্ট করুন, এটি নিচের আউটপুট দেবে:

Android-এ Alert Dialog প্রদর্শনের পদ্ধতি


Android-এ Alert Dialog প্রদর্শনের পদ্ধতি

  1. অ্যান্ড্রয়েডে অ্যালার্ট ডায়ালগকে স্ক্রিনের ৫০% আকারে সেট করা

  2. অ্যান্ড্রয়েডে একটি অ্যাক্টিভিটি শুরু করার আগে প্রোগ্রেস ডায়ালগ কীভাবে দেখাবেন?

  3. অ্যান্ড্রয়েড অ্যাপে নোটিফিকেশন কাউন্ট কীভাবে দেখাবেন?

  4. অ্যান্ড্রয়েডে Always-On Display এক্টিভেট করুন: কুইক ও ইজি সেটআপ