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

অ্যান্ড্রয়েড নোটিফিকেশনের ব্যাকগ্রাউন্ড রঙ পরিবর্তন করা


RemoteViews দিয়ে কাস্টম লেআউট ও ব্যাকগ্রাউন্ড কালার সেট করা

স্ট্যান্ডার্ড নোটিফিকেশনের ব্যাকগ্রাউন্ড রঙ সরাসরি পরিবর্তন করা যায় না। কিন্তু `RemoteViews` এবং কাস্টম লেআউট ব্যবহার করে সম্পূর্ণ কাস্টম নোটিফিকেশন (ব্যাকগ্রাউন্ড কালার, ফন্ট, ইমেজ ইত্যাদি) তৈরি করা যায়।

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

Android Studio-তে নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট (Main)

<?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">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_margin="16dp"
        android:onClick="createNotification"
        android:text="কাস্টম ব্যাকগ্রাউন্ড নোটিফিকেশন" />

</RelativeLayout>

ধাপ ৩: কাস্টম নোটিফিকেশন লেআউট

res/layout/custom_notification_layout.xml — এখানে `android:background="@color/colorAccent"` দিয়ে ব্যাকগ্রাউন্ড রঙ সেট করা হয়েছে।

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:background="@color/colorAccent" <!-- ★ কাস্টম ব্যাকগ্রাউন্ড কালার -->
    android:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="10dp"
        android:contentDescription="@string/app_name"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/image"
        android:text="টেস্টিং"
        android:textColor="#000"
        android:textSize="13sp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/title"
        android:layout_toEndOf="@id/image"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s..."
        android:textColor="#000"
        android:textSize="13sp" />

</RelativeLayout>

ধাপ ৪: MainActivity.java - RemoteViews ব্যবহার

package com.example.custombackgroundnotification;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends AppCompatActivity {

    public static final String CHANNEL_ID = "custom_bg_channel";
    private static final String DEFAULT_CH = "default";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void createNotification(View view) {
        // ★ RemoteViews দিয়ে কাস্টম লেআউট লোড
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);

        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, DEFAULT_CH)
                .setContent(contentView) // ★ কাস্টম ভিউ সেট
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setAutoCancel(true);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel ch = new NotificationChannel(CHANNEL_ID, "Custom BG Channel", NotificationManager.IMPORTANCE_HIGH);
            builder.setChannelId(CHANNEL_ID);
            if (nm != null) nm.createNotificationChannel(ch);
        }

        if (nm != null) nm.notify((int) System.currentTimeMillis(), builder.build());
    }
}

ধাপ ৫: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.custombackgroundnotification">

    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.CustomBackgroundNotification">

        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

নোট: Marquee টেক্সট আনার জন্য

কাস্টম লেআউটের TextView-এ `android:ellipsize="marquee"` এবং `android:singleLine="true"` দিয়ে এবং Java-এ `textView.setSelected(true)` করলে (RemoteViews-এ সরাসরি সম্ভব নয়), মার্কি কাজ করে। কিন্তু নোটিফিকেশনে মার্কি সমর্থন সীমিত।

পরীক্ষা

অ্যাপ রান করুন → বাটন চাপুন → নোটিফিকেশন দেখুন। এর ব্যাকগ্রাউন্ড `colorAccent` রঙের হবে (সাধারণত গোলাপি/লাল)।

অ্যান্ড্রয়েড নোটিফিকেশনের ব্যাকগ্রাউন্ড রঙ পরিবর্তন করা

  1. ActionBarActivity-এর ActionBar-এর ব্যাকগ্রাউন্ড রঙ কীভাবে পরিবর্তন করবেন?

  2. অ্যান্ড্রয়েডে অপশন মেনুর ব্যাকগ্রাউন্ড রঙ পরিবর্তন করার পদ্ধতি

  3. Android-এ কীবোর্ড কীভাবে পরিবর্তন করবেন?

  4. অ্যান্ড্রয়েডে স্ন্যাপচ্যাট নোটিফিকেশন সাউন্ড পরিবর্তন করুন: ধাপে ধাপে গাইড