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

অ্যান্ড্রয়েডে ডায়ালগের বাইরে ক্লিক করলে ডায়ালগ বন্ধ করা

AlertDialog-এ বাইরে ক্লিক করলে ডিসমিস করা

এই উদাহরণটি দেখাবে কীভাবে AlertDialog-এ setCanceledOnTouchOutside(true) ব্যবহার করে ডায়ালগের বাইরে ক্লিক করলে ডায়ালগ বন্ধ করা যায়।

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

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>

ধাপ ৩: মেইন এক্টিভিটিতে কোড (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.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);
        alertDialog.setTitle("AlertDialog");
        alertDialog.setMessage("Do you want to close it?");
        alertDialog.setIcon(R.drawable.logo);
        alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(MainActivity.this, "You clicked yes", Toast.LENGTH_LONG).show();
            }
        });
        alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // কিছু করবেন না
            }
        });
        AlertDialog alert = alertDialog.create();
        alert.setCanceledOnTouchOutside(true); // বাইরে ক্লিক করলে বন্ধ হবে
        alert.show();
    }
}

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

অ্যাপ চালু করুন। টেক্সট ভিউতে ক্লিক করলে AlertDialog দেখাবে। ডায়ালগের বাইরে ক্লিক করলে সেটি বন্ধ হয়ে যাবে।

অ্যান্ড্রয়েডে ডায়ালগের বাইরে ক্লিক করলে ডায়ালগ বন্ধ করা

অ্যান্ড্রয়েডে ডায়ালগের বাইরে ক্লিক করলে ডায়ালগ বন্ধ করা

  1. এন্ড্রয়েডে DatePicker ডায়ালগের রঙ পরিবর্তন করার পদ্ধতি

  2. অ্যান্ড্রয়েডে ডায়লগের অবস্থান (Position) স্ক্রিনে পরিবর্তন করা

  3. অ্যান্ড্রয়েডে ডাটাবেস সহ RecyclerView কীভাবে ব্যবহার করবেন?

  4. নেটফ্লিক্স যেকোনো জায়গা থেকে দেখুন: NordVPN ব্যবহার করে USA-এর ভিতর ও বাইরে স্ট্রিমিং গাইড