Properly insert text into Android Application - java

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.

Related

Edit strings.xml file content in Android

I want to allow the users to enter in text using the settings template in android studio.
And display this in a textview.(which is already set)
settings_activity_settings.xml
is
<resources>
<string name="title_activity_settings">Settings</string>
<!-- General settings -->
<string name="pref_header_general">General</string>
<string name="pref_title_display_name">Enter Team 1</string>
<string name="pref_title_display_name_2">Enter Team 2</string>
</resources>
how to i retrieve this data
First of all your approach is not correct.
Because, you can't change resource files during runtime. Strings are hard-coded in the string.xml file and hence can't be changed during runtime.
I suggest you read this, it will help you understand why android framework don't want you to edit the file in runtime.
Now, the solution to your problem? Simple, save the string you get from user in to Shared Preference and load it int text views from there.

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
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.

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?

How to make android app with option for Tamil and English?

In android app, within that app has settings option such as English and Tamil
If user select Tamil, with in the app only display in Tamil, in all activities all labels should in Tamil. if the user select English all labels are changed to English. How to make that?
Please give any idea....
Follow this Link: Android : translate the language of whole app on click
Then Do like this, Add the string values for each locale into the appropriate file.
English (default locale), /values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">GoodMorning</string>
</resources>
Tamil, /values-tl/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="title">My Application</string>
<string name="hello_world">Kaalai vanakkam</string>
</resources>
Link: http://developer.android.com/training/basics/supporting-devices/languages.html
Have a look at this question.
Basically you have to put the translated Strings in a folder in the res folder.

Difference between android:text="#string/hello" and normal right click--> Text view component--> EditText

While I am working with help of tutorial I found this in textview android:text-"#string/hello" it shows some error.
Then I went through graphical view and right click the component and enter the text. Then that error removes and notifies me that
**Hardcoded string hello should use String resources**
In android, "#string/" refer to the string.xml in Project>res>values> location in your package explorer.
String.xml contains a xml file which refer to a string with an id.
Eg:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World</string>
<string name="app_name">My app</string>
</resources>
Here name="hello" is the id & "Hello World" is its value. When #string/hello is used, the value will be shown.
Similar is the case with "#drawable/". It will refer to the images used. and many more.
You can set text in text view programmatically. Eg:
TextView tv = (TextView)findViewById(R.id.text1); //text1 is the id u provide in xml file
tv.setText("Hello World");
I hope it helped you.
#string/hello simply notice Android to load the String in a XML file located into the /values directory (strings.xml).
hello is the ID of the String specified by name="hello" into that XML.
Android Studio is very unstable. Previously, it auto generated the corresponding statement in the string.xml . Tutorials using previous versions get beginners stuck because of that.
How string.xml works
Android Studio expects you to write all texts in one place, in the string.xml file. In all other files, you just put abbreviations. e.g.
android:text="#string/Hello_world"
means that find the abbreviation Hello_world in the string.xml file and replace it by the intended text.
In string.xml, the corresponding statement which should be written is:
<string name="Hello_world">Hello world!</string>
and that should do the job!
It is possible to just write android:text="Hello world!" (This is what hardcoding is). If you intend to be an android dev, then you would not maximize efficiency.
Why? string.xml
Having all texts in one file eases many things. Like translation if needed and easy change. However, you should choose meaningful abbreviations!

Categories