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,602
  • 12
  • 44
  • 88
nifCody
  • 2,394
  • 3
  • 34
  • 54
  • It is not possible to do so normally. – Bidhan May 26 '15 at 17:40
  • http://stackoverflow.com/questions/7251595/reference-a-specific-item-in-a-string-array-from-a-preferences-xml see here – einschnaehkeee May 26 '15 at 17:44
  • we can access string directly like android:text="@string/app_name" so hope may be there is any ways available too. – nifCody May 26 '15 at 17:45
  • @einschnaehkeee I have seen that earlier that get the thing from string to array. I ask for getting the thing from array to a compnent ( TextView ) in another my layout call home.xml – nifCody May 26 '15 at 17:47
  • Its not have a direct way so need to thing how to get it directly ? Got Answer temporarily – nifCody May 26 '15 at 18:01

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,858
  • 2
  • 17
  • 20
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,592
  • 1
  • 25
  • 43
  • 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