এন্ড্রয়েডে ডায়ালগ বক্সের বাইরে ক্লিক করলে ডিসমিস করার ধাপাবলী
এই উদাহরণটি দেখায় কীভাবে 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="ক্লিক করুন" />
</LinearLayout>
ধাপ ৩: মূল এক্টিভিটি লজিক (src/MainActivity.java)
import android.annotation.TargetApi;
import android.content.DialogInterface;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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
protected 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) {
showAlertBox();
}
});
}
private void showAlertBox() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("আলার্ট ডায়ালগ");
alertDialog.setMessage("কি আপনি ডায়ালগ বক্স বন্ধ করতে চান?");
alertDialog.setPositiveButton("হ্যাঁ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "আপনি হ্যাঁ বাটনে ক্লিক করেছেন", Toast.LENGTH_LONG).show();
}
});
alertDialog.setNegativeButton("না", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// কিছুই করব না
}
});
AlertDialog alert = alertDialog.create();
alert.setCanceledOnTouchOutside(true); // মূল লাইন
alert.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>
অতিরিক্ত তথ্য
setCancelable(false)সেট করলে ডায়ালগ ব্যাক বাটন দিয়েও বন্ধ হবে না।setCanceledOnTouchOutside(true)ডিফল্টভাবে true থাকে কিছু থিমে, কিন্তু একস্প্লিসিট সেট করা ভালো অভ্যাস।
অ্যাপ্লিকেশন চালান
আপনার আসল এন্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে অনুমান করি। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং আপনার ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।