1

I have a string-array in my string.xml file in Android res/value folder like this

my string.xml

<string-array name="books_titles_en">
    <item>Smoking Effeccts on Your Body</item>
    <item>Smoking - effects on your body</item>
    <item>How Tobacco Smoke Causes Disease</item>
    <item>patterns of use, health effects, use in smoking cessation and regulatory issues</item>
</string-array>

I need to retrieve the first element of array like this

my home.xml

 <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/frag_1_text_pad"
                android:textSize="@dimen/frag_1_text_size"
                android:paddingRight="@dimen/frg_1_text_right_pad"
                android:id="@+id/book_link0"
                android:text="@string-array/book_title_en[0]"/>

I know it seems like it will cause an error. How do I retrive these things on the string-array in string.xml in Android

Paradox
  • 4,473
  • 8
  • 39
  • 84
nifCody
  • 2,245
  • 3
  • 27
  • 52

2 Answers2

9

Just to giva a full answer to your problem:

There is no way of accessing an array directly, but you could do something like this:

<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
    <item>@string/books_titles_en_1</item>
    <item>@string/books_titles_en_2</item>
    <item>@string/books_titles_en_3</item>
    <item>@string/books_titles_en_4</item>
</string-array>

and then:

<TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/frag_1_text_pad"
            android:textSize="@dimen/frag_1_text_size"
            android:paddingRight="@dimen/frg_1_text_right_pad"
            android:id="@+id/book_link0"
            android:text="@string/book_title_en_1"/>

So you basically reference the neccessary strings directly, that are referenced in your string array. ;)

einschnaehkeee
  • 1,838
  • 2
  • 16
  • 19
0

Refer to this link Android - retrieve string array from resources

And this link Help in getting String Array from arrays.xml file

Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.

The first link I posted used

for(int i = 0; i < menuArray.length; i++) 
    {
        Button b = new Button(this);
        b.setText(menuArray[i]);
        ll.addView(b);
    }

which is a sample of how to obtain the whole array from the xml. You can modify it for single array value retrieval, or whichever array value you wish to retrieve.

Happy coding :D

Community
  • 1
  • 1
Kevin Murvie
  • 2,452
  • 1
  • 20
  • 42
  • No. I need to get the array value without using the java file. I need to access other xml file from the current xml file – nifCody May 26 '15 at 17:42