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

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

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.

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.

Related

Properly insert text into Android Application

I recently started an Android project as some sort of tutorial, but rather than the same boring List/Web View most android offline tutorial app make use of, I decided to make a it a bit better using Recycler View and card View.
The main problem so far(even it may not be to the end user as long as the app works anyway) is putting the text in cardview(had no problem with images and other res) as my strings.xml is filled up and everything looks rough.
<resources>
<string name="lesson_galaxy_one_intro"> <h1><span style="color:red">Welcome to My ..... Course</span></h1>]]> </string>
<string name="lesson_galaxy_two_intro"> <![CDATA[
<h1><span style="color:red">Getting to know .....</span></h1> ]]> </string>
<string name="galaxy_one_main_neration"> <![CDATA[<h1> <span style="color:red"> ]]> </string>
<string name="galaxy_one_main_Overview"> </string>
<string name="b"> <![CDATA[ ]]></string>
<string name="c"> <![CDATA[ ]]></string></resources>
This is more of my first real project, I just want to ask how do developers package their Text. Is strings.xml the right option for apps that require many texts? Please any tip could be helpful.

Localization does not work for string array in android application

I have defined a string array in my values folder in this way for default English language
<string-array name="movie">
<item>11001#Action#1</item>
<item>11002#Animation#1</item>
<item>11003#Comedy#1</item>
<item>11004#Documentary#1</item>
<item>11005#Family#1</item>
<item> 11006#Film-Noir#1</item>
<item>11007#Horror#1</item>
<item>11008#Musical#1</item>
<item>11009#Romance#1</item>
</string-array>
My applicaiton is suported for many language like chainese simplified thats why i have defined another string-array in values-zh-rCN/string.xml for chainese simplified langue like this way
<string-array name="movie">
<item>11001#动作片#1</item>
<item>11002#动画片#1</item>
<item>11003#喜剧#1</item>
<item>11004#记录片#1</item>
<item>11005#家庭#1</item>
<item> 11006#黑色电影#1</item>
<item>11007#恐怖片#1</item>
<item>11008#音乐剧#1</item>
<item>11009#爱情片#1</item>
</string-array>
when my app is running and seleted Default english langue shown the array data but when i have seleted phone language in chainese simplified or other langue instead of english the array data does not shown. what are the possible solutionn for these problem ?
Write your texts in string.xml as strings items, then use them into arrays.xml, example:
string.xml (English):
<string name="array_power_unit_item_00">By Watt.</string>
<string name="array_power_unit_item_01">By KiloWatt.</string>
<string name="array_power_unit_item_02">By Ampere and Volt.</string>
<string name="array_power_unit_item_03">By MilliAmpere and Volt.</string>
string.xml (Arabic):
<string name="array_power_unit_item_00">بالواط.</string>
<string name="array_power_unit_item_01">بالكيلو واط.</string>
<string name="array_power_unit_item_02">بالأمبير و الفولت.</string>
<string name="array_power_unit_item_03">بالميلي أمبير و فولت.</string>
arrays.xml:
<string-array name="arraypowerunit" translatable="false">
<item>#string/array_power_unit_item_00</item>
<item>#string/array_power_unit_item_01</item>
<item>#string/array_power_unit_item_02</item>
<item>#string/array_power_unit_item_03</item>
</string-array>
<string name="abcd">喜剧</string>
<string name="efgh">记录片</string>
<array name="array_abcd">
<item>#string/abcd</item>
<item>#string/efgh</item>
</array>
Use this method for get resource and get array from this resource.
this will work 100%
public static Resources getResourcesBasedOnLanguage(Activity activity) {
Configuration confTmp =new Configuration(activity.getResources().getConfiguration());
confTmp.locale = new Locale("EN");
DisplayMetrics metrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
return new Resources(activity.getAssets(), metrics, confTmp);
}

80+ R cannot be resolved to a variable errors and No resource found that matches the given name

All of a sudden I'm getting 80+ R cannot be resolved to a variable errors after modifying my strings.xml file. I'm not sure exactly what I've done to cause the issue but I've wrecked my project somehow and when I check the network drive where my workspace resides - the strings files is there and I can open it in eclipse and see the supposed "missing strings" however eclipse states: No resource found that matches the given name regardless of the fact the strings are infact there.
Steps taken:
Clean the project
Examine strings.xml for abnormalities (none found)
Restart Eclipse
STRINGS.XML
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Welcome to <b>Straight Talk Wireless!</b></string>
<string name="app_name">Straight Talk Data Settings</string>
<string name="display_name">Data Settings</string>
<string name="initial_sm_dialog">The APN Settings on your phone will now be updated to the Straight Talk Wireless Network so you can use DATA and PICTURE MESSAGING.</string>
<string name="unchanged_sm_dialog">The APN Settings have not been updated to the <b>Straight Talk Mobile Network</b></string>
<string name="changed_sm_dialog">The APN Settings have been updated to the <b>Straight Talk Mobile Network</b></string>
<string name="insert_sm_dialog">Error: \r\nPlease insert a Straight Talk SIM into an unlocked GSM phone and re-run the app.</string>
<string name="incomp_sm_dialog">Error: Incompatible device.\r\nPlease use an unlocked GSM phone with a Straight Talk SIM.</string>
<string name="security_sm_dialog">Please manually enter the Straight Talk APN Settings on your device with the below information:\r\nName:\straighttalk\r\nAPN:\straight talk\r\nMMSC:\thttp://smpl.mms.msg.eng.t-mobile.com/mms/wapenc\r\nMCC:\t310\r\nMNC:\t260</string>
<string name="security_instr">It looks like you’re running an Android OS Ice Cream Sandwich or later.
\r\nNo worries, please follow the below steps:
\r\n1. Write down these settings: </string>
<string name="under_settings">2. Click the button below that says “Take me to Update my Settings”
\r\n3. Click the menu button that you’ll see on the next screen
\r\n4. Click “New APN” and enter the fields you just wrote down</string>
<string name="security_sm_dialog_t">Please manually enter the Straight Talk APN Settings on your device.\r\n<table><tr><td>Name:</td><td>Straight Talk</td></tr><tr><td>APN:</td><td>straighttalk</td></tr><tr><td>MMSC:</td><td>thttp://smpl.mms.msg.eng.t-mobile.com/mms/wapenc</td></tr></table></string>
<string name="assited_title">Assisted Data Settings</string>
<string name="assited_steps">1. Create a new data profile by selecting Menu>New APN.
\n\n2. Select the Straight Talk notification and paste the value in the corresponding field in the new data profile.
\n\n3. Continue doing so until there are no more Straight Talk Mobile notifications.
\n\n4. Save the new profile and exit.
\n\n5. Select the Straight Talk profile as default and you are DONE.</string>
<string name="assisted_button">Create new data profile</string>
<string name="finish_alert_title">Almost Complete</string>
<string name="complete_steps">1. Press Menu>Save
\n\n2. Select the Straight Talk profile as default and you are DONE.</string>
<string name="update_text">By choosing to update, the APN settings on your cell phone will be automatically updated and you will be able to access data on the Straight Talk Network.\n\n\n\n</string>
<string name="updating_text">Loading APN settings...\n\n\n\n</string>
<string name="error_text">We couldn’t retrieve your settings. Please try again later. You might get better results in about an hour.\n\n\n\n</string>
<string name="visit_text">If that doesn’t work, please visit</string>
<string name="oops_text">Oops!</string>
<string name="updated_text">Your settings are updated. Just reboot your phone. If this doesn’t work, go to http://apn.straighttalk.com to find specific instructions for your handset.</string>
<string name="updated_text2">Your settings are updated.</string>
<string name="tryagain_text1"><font color="#29abf2">Oops!</font></string>
<string name="tryagain_text2"> <font color="#00000">We couldn\'t retrieve your settings. Please try again later.You might get better results in about an hour.</font </string>
<string name="tryagain_text3"><font color="#00000">If that doesn\'t work, please visit</font> <font color="#29abf2"><b>
<string name="url_text">http://apn.straighttalk.com</string>
<string name="start_text">Easily set up your Data Settings with this APN mobile app.</string>
<string name="start_text2">Easily set up your Data Settings\n with this APN mobile app.</string>
<string name="done_text">Reboot your phone and you’ll have access to the web and picture messaging.</string>
<string name="apn_app_text_cta2">You’re going to need to edit a few of your phone’s settings. Enhanced security on newer versions of Android will not allow APN settings to be automatically updated. Don’t worry; we will walk you through step by step.</string>
<string name="apn_app_text_instr">We will send you a series of (%1$s) <font color="#FF000000"><b>NOTIFICATIONS</b></font> to your cell phone. You need to complete each step in order to update your settings.<br><br>Please take a moment to read each of the following instructions. Once you’re finished reading the instructions click the Let’s Get Started button to begin receiving the notifications</string><string name="apn_app_text_instr2">1. You will need to add a <font color="#FF000000"><b>NEW APN PROFILE</b></font> before you start with the notifications. If you need instructions on how to do this, please refer to your device’s user or manufacture’s manual. <br><br>2. <font color="#FF000000"><b>PULL DOWN</b></font> the notification screen. Once you have done this the information that needs to be updated will be copied to your clipboard. <br><br>3. You must then <font color="#FF000000"><b>TAP AND HOLD</b></font> the field presented to you. <font color="#FF000000"><b>RELEASE,</b></font> and the copied information will be pasted into the field. <br><br>4. <font color="#FF000000"><b>PULL DOWN</b></font> the the notication screen. Once you have done this, the information that needs to be updated will be copied to your clipboard.<br></string><string name="apn_app_text_instr3">5. Complete the series of (%1$s) notification steps. <br><br>6. <font color="#FF000000"><b>SAVE</b></font> the new profile and exit. <br><br>7. <font color="#FF000000"><b>SELECT</b></font> the Straight Talk APN profile as default. <br><br>8. Be sure you <font color="#FF000000"><b>DELETE</b></font> all other APNs, if possible.</string>
<string name="delete_notif_title">(Delete other APNs)</string>
<string name="delete_notif_content">SELECT them, Press \"menu\" and delete them if your phone allows it.</string>
<string name="reboot_notif_title">(Reboot your Phone)</string>
<string name="reboot_notif_content">If this doesn\'t work, go to:\nwww.straighttalk.com/support</string>
<string name="read_again_text"><b><u>READ INSTRUCTIONS AGAIN</u></b></string>
<string name="mcc_label">MCC</string>
<string name="mcc">310</string>
<string name="type_label">APN Type</string>
<string name="type">default,mms,supl</string>
<string name="config_name_label">Name</string>
<string name="config_sm_name">Straight Talk Wireless</string>
<string name="apn_label">APN</string>
<string name="apn_sm_addr">straighttalk</string>
<string name="mmsc_label">MMSC</string>
<string name="mmsc_sm_url">http://smpl.mms.msg.eng.t-mobile.com/mms/wapenc</string>
<string name="mnc_label">MNC</string>
<string name="mnc_tmo">260</string>
<string name="mnc_att">410</string>
<string name="numeric_tmo">310260</string>
<string name="proxy_sm_addr"></string>
<string name="proxy_sm_port"></string>
<string name="mmsproxy_sm"></string>
<string name="mmsport_sm"></string>
<string name="authtype"></string>
<string name="user"></string>
<string name="password"></string>
<string name="server"></string>
<string name="protocol"></string>
<string name="authtype_sm">0</string>
<string name="numeric_att">310410</string>
<string name="instructions_1">TEST STRING</string>
<string name="done_text1">TEST STRING2</string></string>
</resources>
There's a redundant closing </string> tag on the last entry and no closing tag for tryagain_text3. Other than that, have you checked that the file is XML valid?

error opening trace file: no such file or directory (2), also strings.xml not updated

I'm creating an 8 ball application and I'm using reflection to grab the strings out of the strings.xml file and in to an ArrayList but all its doing is finding the default strings in there (even the hello_world-- one which I deleted) also, I don't know how to get the string value out of the Field class that it's in.
the first System.out.println() [the amount of fields] comes out as 4.
they are all the default values of the default strings.xml file, it hasn't updated or seen the added/deleted resources.
also it's coming up with that weird trace file error
in the debugger, ansStuff is coming up with a size of 23... [the amount its suppose to be [ not 4..]] so weird
Field[] ansStuff = R.string.class.getFields();
System.out.println(R.string.class.getFields().length);
for(Field x :ansStuff){
System.out.println(x);
System.out.println(x.getName());
System.out.println(x.toString());
if(x.getName().startsWith("ans")){
choices.add(/*the goddamned string*/"poop");
}
}
which after taking out the debugging mess just translates to this
for(Field x :R.string.class.getFields())
if(x.getName().startsWith("ans"))
choices.add(/*what do I add here to get the string value from the field in to the arraylist */);
here is the resource file
<resources>
<string name="app_name">8ballRotate</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="ans0">It is certain</string>
<string name="ans1">It is decidedly so</string>
<string name="ans2">Without a doubt</string>
<string name="ans3">Yes – definitely</string>
<string name="ans4">You may rely on it</string>
<string name="ans5">As I see it, yes</string>
<string name="ans6">Most likely</string>
<string name="ans7">Outlook good</string>
<string name="ans8">Yes</string>
<string name="ans9">Signs point to yes</string>
<string name="ans10">Reply hazy, try again</string>
<string name="ans11">Ask again later</string>
<string name="ans12">Better not tell you now</string>
<string name="ans13">Cannot predict now</string>
<string name="ans14">Concentrate and ask again</string>
<string name="ans15">Don\'t count on it</string>
<string name="ans16">My reply is no</string>
<string name="ans17">My sources say no</string>
<string name="ans18">Outlook not so good</string>
<string name="ans19">Very doubtful</string>
</resources>

Android how to create a nested menulist

I want to create a menulike app where each page is a subcategory of the previous selection and the last page shows the nutrition factor of the selected item, I have already created all the arrays in the arrays.xml :
<string-array name="food_values">
<item>Veggies</item>
<item>Fruits</item>
...
<item>
</string-array>
<string-array name="veg_values">
<item>Cucumber</item>
<item>Celery</item>
...
</string-array>
<string-array name="fruit_values">
<item>Apple</item>
<item>Banana</item>
...
</string-array>
I wanted to create a listview and use set onclicklistener to the list to open the next subcategory but I don't know how to implement it without using a lot of if and else statements and also generating the next listview with the same template list_item.xml.
I also tried Maps but it got too complicated with the nested lists.
Thank you for your help

Categories