Ajinkya
Ajinkya

Reputation: 147

How do I create a navigation drawer with non english items?

This code is stored in res/values/strings.xml

    <string-array name="planets_array">
      <item>एक काम</item>
      <item>दो काम</item>
      <item>तीन काम</item>
      <item>चार काम</item>
    </string-array>

The array will be passed onto ArrayAdapter which will be used in Navigation Drawer. However, I want these Items to be referenced using an english name.

EDIT


This code was done according to changes suggested by Dave.

<resources>
    <string name="Mercury">एक काम</string>
    <string name="Venus">दो काम</string>
    <string name="Earth">तीन काम</string>
    <string name="Mars">चार काम</string>

    <string name="app_name">Navigation Drawer Example</string>

    <string-array name="planets_array">
      <item>@string/Mercury</item>
      <item>@string/Venus</item>
      <item>@string/Earth</item>
      <item>@string/Mars</item>
      <item>Jupiter</item>
      <item>Saturn</item>
      <item>Uranus</item>
      <item>Neptune</item>
    </string-array>

    <string name="drawer_open">Open navigation drawer</string>
    <string name="drawer_close">Close navigation drawer</string>
    <string name="action_websearch">Web search</string>
    <string name="app_not_available">Sorry, there\'s no web browser available</string>
</resources>

When I tap on first four items, the respective images of planets dont show up in Navigation drawer but when I tap on last 4 Items (Starting from Jupiter) , It works fine

Upvotes: 1

Views: 76

Answers (2)

David Gras
David Gras

Reputation: 1028

It's already answered here.

As the Android reference says:

<item>
    ...
    The value can be a reference to another string resource.
    ...

Solution:

First, you have to write simple string elements with your english name:

<string name="english_planet_1_name">एक काम</string>
<string name="english_planet_2_name">दो काम</string>
<string name="english_planet_3_name">तीन काम</string>
<string name="english_planet_4_name">चार काम</string>

And then you can reference them everywhere, even from string-array items:

<string-array name="planets_array">
    <item>@string/english_planet_1_name</item>
    <item>@string/english_planet_2_name</item>
    <item>@string/english_planet_3_name</item>
    <item>@string/english_planet_4_name</item>
</string-array>

Finally you get both: your planet local names and their english reference name.

Upvotes: 0

Ed_Fernando
Ed_Fernando

Reputation: 378

You can set the name attribute for each item but you will have to parse the strings.xml file and use getValue from the available set of attributes. Check this SO question out :

Get name attribute of item of string array

I haven't had the time to try this though.

Upvotes: 1

Related Questions