23

I am new in Android development and facing a problem with managing Android resources. I want to create a listView with an ImageView and a TextView.

Following is my implementation which works fine, but actually I wanted to use arrays which I created before like this:

int[] img = getResources().getIntArray(R.Array.img);
package com.simplelistviewwithlistactivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

public class ListActivityS extends ListActivity {
    int[] img = { R.drawable.r1, R.drawable.r2, R.drawable.skycubemap1,
            R.drawable.skycubemap1, R.drawable.skycubemap2,
            R.drawable.skycubemap3, R.drawable.skycubemap4,
            R.drawable.skycubemap5 };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getListView().setDividerHeight(2);
        getListView().setAdapter(new BindDataAdapter(this, img, item));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(item[position] + " is clicked.");
        builder.setPositiveButton("OK", null);
        builder.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_list, menu);
        return true;
    }

    private String item[] = { "This is list Item1", "This is list Item2",
            "This is list Item3", "This is list Item4", "This is list Item5",
            "This is list Item6", "This is list Item8", "This is list Item8"
2
  • You can use colors in an array by following this example: stackoverflow.com/a/17584066/560600 Commented Jul 11, 2013 at 2:24
  • Hi Jannis, did you find any of the given answers useful to solve your problem? If so, please consider marking the answer accepted. your small action will help others to find the solution to a similar problem. Commented Jan 11, 2019 at 12:29

2 Answers 2

54

Create an XML like below and put it in res/values/arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

Then use code like this:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);

Source: http://developer.android.com/guide/topics/resources/more-resources.html

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to call recycle() of icons and colors after using.
18

You can use resources from res/values/arrays.xml.

For drawables

<integer-array name="your_images">
    <item>@drawable/ic_active_image</item>
    <item>@drawable/ic_visited_image</item>
</integer-array>

val position = 1 // Position in array.
val drawables = resources.obtainTypedArray(R.array.your_images)
val drawable = drawables.getResourceId(position, -1)
image.setImageResource(drawable)
drawables.recycle()

For colors

<array name="your_colors">
    <item>#365374</item>
    <item>#00B9FF</item>
</array>

val position = 1
val colors = resources.obtainTypedArray(R.array.your_colors)
val color = colors.getColor(position, -1)
title.setTextColor(color)
colors.recycle()

For strings

<string-array name="your_strings">
    <item>Active</item>
    <item>Visited</item>
</string-array>

val position = 1
val strings = resources.getStringArray(R.array.your_strings)
title.text = strings[position]

Plurals:

<plurals name="proposal_plurals">
    <item quantity="zero">No proposals</item>
    <item quantity="one">%1$d proposal</item>
    <item quantity="two">%1$d proposals</item>
    <item quantity="few">%1$d proposals</item>
    <item quantity="many">%1$d proposals</item>
    <item quantity="other">%1$d proposals</item>
</plurals>

val count = 117
val proposals = count.takeIf { it != 0 }?.let {
    resources.getQuantityString(R.plurals.proposal_plurals, it, it)
} ?: "No proposals"

2 Comments

Your code for color looks like you could also use getColor() directly on resources... and save yourself recycle(). Is that right?
@TheincredibleJan, why not? It is a common scenario. In Kotlin we use use operator instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.