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

Android-এ ListView আইটেমের ব্যাকগ্রাউন্ড রঙ পরিবর্তন করার পদ্ধতি

ListView আইটেম ক্লিকে ব্যাকগ্রাউন্ড কালার পরিবর্তন

এই উদাহরণে দেখানো হয়েছে কীভাবে ListView-এ কোনো আইটেমে ক্লিক করলে তার ব্যাকগ্রাউন্ড রঙ পরিবর্তন করা যায়। setOnItemClickListener এবং View.setBackgroundColor() ব্যবহার করে এটি বাস্তবায়ন করা হয়।

সীমাবদ্ধতা ও সমাধান

সমস্যা: ListView ভিউ রিসাইকেল করে (convertView)। স্ক্রোল করলে ক্লিক করা আইটেমের রঙ অন্য আইটেমে দেখা যেতে পারে।

সমাধান: কাস্টম Adapter ব্যবহার করুন যেখানে getView()-এ পজিশন অনুযায়ী ব্যাকগ্রাউন্ড সেট করা হয়, বা RecyclerView ব্যবহার করুন (সুপারিশ করা)।

ধাপ ১: প্রজেক্ট সেটআপ

Package: com.example.listviewcolor

ধাপ ২: লেআউট (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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">

    <ListView
        android:id="@+id/mobile_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp"
        android:choiceMode="singleChoice" />

</androidx.constraintlayout.widget.ConstraintLayout>

ধাপ ৩: কাস্টম Adapter (MobileAdapter.java) — সুপারিশ করা পদ্ধতি

package com.example.listviewcolor;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MobileAdapter extends ArrayAdapter {

    private int selectedPosition = -1;

    public MobileAdapter(@NonNull Context context, int resource, @NonNull String[] objects) {
        super(context, resource, objects);
    }

    // সিলেক্টেড পজিশন সেটার
    public void setSelectedPosition(int position) {
        selectedPosition = position;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        String item = getItem(position);
        TextView textView = view.findViewById(android.R.id.text1);
        textView.setText(item);
        textView.setPadding(48, 32, 48, 32);
        textView.setTextSize(18);

        // সিলেক্টেড পজিশন অনুযায়ী ব্যাকগ্রাউন্ড সেট
        if (position == selectedPosition) {
            view.setBackgroundColor(Color.parseColor("#E3F2FD")); // লাইট ব্লু
            textView.setTextColor(Color.parseColor("#1565C0")); // ডার্ক ব্লু টেক্সট
        } else {
            view.setBackgroundColor(Color.TRANSPARENT);
            textView.setTextColor(Color.parseColor("#212121")); // ডিফল্ট টেক্সট
        }

        return view;
    }
}

ধাপ ৪: MainActivity.java (কাস্টম Adapter সহ)

package com.example.listviewcolor;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private String[] mobileArray = {
        "Android", "iPhone", "Windows Mobile", "Blackberry",
        "WebOS", "Ubuntu", "Windows 7", "Mac OS X"
    };

    private ListView mobileList;
    private MobileAdapter adapter;

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

        mobileList = findViewById(R.id.mobile_list);
        
        // কাস্টম Adapter ব্যবহার
        adapter = new MobileAdapter(this, android.R.layout.simple_list_item_1, mobileArray);
        mobileList.setAdapter(adapter);

        mobileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                // Adapter-এ সিলেক্টেড পজিশন আপডেট
                adapter.setSelectedPosition(position);
            }
        });
    }
}

সহজ পদ্ধতি (সহজ কিন্তু রিসাইকেলিং ইস্যু আছে)

যদি শুধু দ্রুত প্রোটোটাইপ করতে চান, নিচের কোডও কাজ করবে (কিন্তু স্ক্রোল ইস্যু থাকবে):


// MainActivity.java (সহজ ভার্সন)
mobileList.setOnItemClickListener((parent, view, position, id) -> {
    // সব আইটেম রিসেট
    for (int i = 0; i < parent.getChildCount(); i++) {
        parent.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
    }
    // ক্লিক করা আইটেম হাইলাইট
    view.setBackgroundColor(Color.parseColor("#E3F2FD"));
});

রঙ রিসোর্স ব্যবহার (colors.xml) — বেস্ট প্র্যাকটিস

<!-- res/values/colors.xml -->
<resources>
    <color name="list_item_selected_bg">#E3F2FD</color>
    <color name="list_item_selected_text">#1565C0</color>
    <color name="list_item_default_bg">@android:color/transparent</color>
    <color name="list_item_default_text">#212121</color>
</resources>

Adapter-এ ব্যবহার:


if (position == selectedPosition) {
    view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.list_item_selected_bg));
    textView.setTextColor(ContextCompat.getColor(getContext(), R.color.list_item_selected_text));
} else {
    view.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.list_item_default_bg));
    textView.setTextColor(ContextCompat.getColor(getContext(), R.color.list_item_default_text));
}

AndroidManifest.xml

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

    <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/Theme.ListViewColor">

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

ListView vs RecyclerView: কেন RecyclerView ভালো?

ফিচারListViewRecyclerView
ViewHolder প্যাটার্নবাধ্যতামূলক নয়বাধ্যতামূলক (দ্রুত)
LayoutManagerশুধু ভার্টিক্যাল লিস্টLinear, Grid, Staggered, Custom
Item Animationsলিমিটেডবিল্ট-ইন (DefaultItemAnimator)
Item Decorationনাহ্যাঁ (Divider, Spacing, Custom)
DiffUtil (স্মার্ট আপডেট)নাহ্যাঁ (ListAdapter)

RecyclerView এ একই ফিচার (মডার্ন পদ্ধতি)


// build.gradle
implementation "androidx.recyclerview:recyclerview:1.3.2"

// Adapter (ListAdapter + DiffUtil)
class MobileAdapter : ListAdapter(DiffCallback()) {
    var selectedPosition = -1
        private set

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(android.R.id.text1)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
        ViewHolder(LayoutInflater.from(parent.context)
            .inflate(android.R.layout.simple_list_item_1, parent, false))

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        holder.textView.text = item
        holder.textView.setPadding(48, 32, 48, 32)
        
        if (position == selectedPosition) {
            holder.itemView.setBackgroundResource(R.color.list_item_selected_bg)
            holder.textView.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.list_item_selected_text))
        } else {
            holder.itemView.setBackgroundResource(R.color.list_item_default_bg)
            holder.textView.setTextColor(ContextCompat.getColor(holder.itemView.context, R.color.list_item_default_text))
        }
    }

    class DiffCallback : DiffUtil.ItemCallback() {
        override fun areItemsTheSame(old: String, new: String) = old == new
        override fun areContentsTheSame(old: String, new: String) = old == new
    }
}

// Activity-এ
recyclerView.adapter = adapter
recyclerView.addOnItemTouchListener(object : RecyclerView.SimpleOnItemTouchListener() {
    override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
        val view = rv.findChildViewUnder(e.x, e.y)
        if (view != null) {
            val pos = rv.getChildAdapterPosition(view)
            adapter.selectedPosition = pos
            adapter.notifyDataSetChanged()
        }
        return false
    }
})

পরীক্ষা চেকলিস্ট

  1. অ্যাপ চালু করুন → লিস্ট আইটেমে ক্লিক করুন
  2. ক্লিক করা আইটেমের ব্যাকগ্রাউন্ড ব্লু হবে, টেক্সট ডার্ক ব্লু হবে
  3. নিচে স্ক্রোল করুন → আগে ক্লিক করা আইটেমের রঙ থাকছে কিনা (Custom Adapter-এ থাকবে)
  4. আরেকটি আইটেমে ক্লিক করুন → পুরানো আনসিলেক্ট, নতুন সিলেক্ট
  1. আউটলুক ক্যালেন্ডারের ব্যাকগ্রাউন্ড রঙ পরিবর্তন করার উপায়

  2. Tkinter-এ Treeview (Listbox) ব্যাকগ্রাউন্ড রঙ পরিবর্তন

  3. Tkinter Listbox-এ পূর্ণ রঙ পরিবর্তন কীভাবে করবেন?

  4. iOS-এ নোটস অ্যাপের ব্যাকগ্রাউন্ড কালার পরিবর্তন করার পদ্ধতি