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

XML ভ্যালু ফাইল ব্যবহার করে ড্রয়েবল রিসোর্স আইডি অ্যারে সংরক্ষণ

drawable রিসোর্স আইডি XML অ্যারেতে রাখুন

এই পদ্ধতিতে আপনি @drawable/... রেফারেন্সগুলো XML ফাইলে সংরক্ষণ করে Java/Kotlin কোডে সহজে অ্যাক্সেস করতে পারেন। এটি ইমেজ গ্যালারি, স্লাইডার, বা র‍্যান্ডম ইমেজ দেখানোর জন্য উপযুক্ত।

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

ধাপ ২: arrays.xml ফাইল তৈরি করুন

res/values/arrays.xml এ integer-array হিসেবে সংরক্ষণ করুন:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="random_images">
        <item>@drawable/image1</item>
        <item>@drawable/image2</item>
        <item>@drawable/image3</item>
    </integer-array>
</resources>

ধাপ ৩: লেআউটে ImageViewগুলো রাখুন

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/iv1"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="8dp"
        android:contentDescription="@string/app_name" />
    <ImageView
        android:id="@+id/iv2"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="8dp"
        android:contentDescription="@string/app_name" />
    <ImageView
        android:id="@+id/iv3"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_margin="8dp"
        android:contentDescription="@string/app_name" />
</LinearLayout>

ধাপ ৪: TypedArray ব্যবহার করে লোড করুন

package app.com.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView iv1, iv2, iv3;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        iv1 = findViewById(R.id.iv1);
        iv2 = findViewById(R.id.iv2);
        iv3 = findViewById(R.id.iv3);
        
        // TypedArray দিয়ে রিসোর্স আইডি পান
        TypedArray images = getResources().obtainTypedArray(R.array.random_images);
        
        iv1.setImageResource(images.getResourceId(0, -1));
        iv2.setImageResource(images.getResourceId(1, -1));
        iv3.setImageResource(images.getResourceId(2, -1));
        
        // জরুরি: রিসাইকেল করুন মেমরি লিক এড়াতে
        images.recycle();
    }
}

কেন TypedArray?

  • সাধারণ String array-এ @drawable/... পarske করা যায় না।
  • integer-array এবং TypedArray কম্বিনেশন রিসোর্স আইডি (int) রিটার্ন করে।
  • getResourceId(index, defValue) দিয়ে সুরক্ষিতভাবে আইডি পাওয়া যায়।

সতর্কতা

weirdly, images.recycle() কল করা বাধ্যতামূলক। না করলে মেমরি লিক হবে।

কotlin সংস্করণ

val images = resources.obtainTypedArray(R.array.random_images)
iv1.setImageResource(images.getResourceId(0, -1))
images.recycle()
  1. অ্যান্ড্রয়েডে XML স্টাইল ব্যবহার করে কাস্টম বাটন তৈরি করার পদ্ধতি

  2. অ্যান্ড্রয়েডে ড্রয়েবলের রঙ কীভাবে পরিবর্তন করবেন?

  3. Android-এ SharedPreferences ব্যবহার করে ডেটা সংরক্ষণ, পঠন এবং সম্পাদনা

  4. Android-এ XML ফাইল ব্যবহার করে অ্যানিমেশন তৈরির পদ্ধতি