How to List Drawable Images in Array XML in Android using TypedArray

Hi Everyone,

In this Blog post, I am going to brief about how to list drawable images in array XML in Android using TypedArray. In Many of our projects, we may need to display more images with text. We can save the text as a string in arrays.xml, and we can use it in our Activity class using the array string name. But, Can we do it for images?

Yes we can, It is possible by saving the images array in array.xml.

Let’s see how to do this.

Step 1:

Create a arrays.xml under res/vslues/arrays.xml. And paste the following code in that file.

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <array name="ten_eleven">
        <item>TextView 1</item>
        <item>TextView 2</item>
        <item>TextView 3</item>
    </array>
    <array name="ten_eleven_icon">
        <item>@drawable/image1</item>
        <item>@drawable/image2</item>
        <item>@drawable/image3</item>
    </array>

</resources>

Step 2:

Create layout called imagearray.xml under /res/layout/imagearray.xml. And paste the following code in imagearray.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10sp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="100sp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="100sp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="100sp"
        android:src="@drawable/abc_ab_bottom_solid_dark_holo" />

</LinearLayout>

Step 3:

I am going to use TypedArray for an array of drawables listing.
Create ImageArray.java under Source Folder(src/ImageArray.java), and paste the following code:

package com.example.imagearray;

import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageArray extends Activity{
	TextView textview1,textview2,textview3;
	ImageView imageview1,imageview2,imageview3;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.imagearray);
		textview1 = (TextView) findViewById(R.id.textView1);
		textview2 = (TextView) findViewById(R.id.textView2);
		textview3 = (TextView) findViewById(R.id.textView3);
		imageview1 = (ImageView)findViewById(R.id.imageView1);
		imageview2 = (ImageView)findViewById(R.id.imageView2);
		imageview3 = (ImageView)findViewById(R.id.imageView3);
		
		final TypedArray testArrayIcon = getResources().obtainTypedArray(R.array.ten_eleven_icon);
		String[] testArray = getResources().getStringArray(R.array.ten_eleven);
		
		
			textview1.setText(testArray[0] + "\n");
			imageview1.setImageResource(testArrayIcon.getResourceId(0, -1));
			
			textview2.setText(testArray[1] + "\n");
			imageview2.setImageResource(testArrayIcon.getResourceId(1, -1));
			
			textview3.setText(testArray[2] + "\n");
			imageview3.setImageResource(testArrayIcon.getResourceId(2, -1));

	}

}

TypedArray is used for an array of drawables listing.

Now, run the application and get the following output:
Display Image Array from Array XML in Android

I hope this Blog post was helpful. Do let me know your feedback.

Leave a Reply

Your email address will not be published. Required fields are marked *