কাস্টম লেআউট দিয়ে টোস্ট বানান
স্ট্যান্ডার্ড টোস্ট সাদা এবং সীমিত। কাস্টম লেআউট (XML) ব্যবহার করে আপনি টোস্টে ছবি, রঙ, ফন্ট, ব্যাকগ্রাউন্ড শেপ—সব কাস্টমাইজ করতে পারেন।
১. মেইন লেআউট (activity_main.xml)
<Button
android:id="@+id/btnShowToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Toast"
android:layout_gravity="center" />
২. কাস্টম টোস্ট লেআউট (res/layout/custom_toast.xml)
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:background="@drawable/toast_background"
android:padding="16dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/ic_check_circle"
android:contentDescription="Success" />
<TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="This is a custom toast!"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
৩. ড্রয়েবল ব্যাকগ্রাউন্ড (res/drawable/toast_background.xml)
<shape xmlns:android="https://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="16dp" />
<solid android:color="#4CAF50" /> <!-- গ্রিন ব্যাকগ্রাউন্ড -->
<padding android:left="16dp" android:top="12dp" android:right="16dp" android:bottom="12dp" />
</shape>
৪. MainActivity.java
Button btn = findViewById(R.id.btnShowToast);
btn.setOnClickListener(v -> {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
findViewById(R.id.custom_toast_layout));
TextView text = layout.findViewById(R.id.toastText);
text.setText("কাস্টম টোস্ট সফলভাবে দেখানো হয়েছে!");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
});
ফলাফল: একটি সুন্দর গ্রিন রাউন্ডেড টোস্ট যাতে আইকন এবং বোল্ড সাদা টেক্সট থাকে।