I need to know if there is a way to get String type data from strings.xml (nested in resource tag)
<resources>
<string name="app_name">One World</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="productInfo_lanIP">LAN IP Address :\t</string>
</resources>
and I need to get productInfo_lanIP from this and use it in my MainActivity.java to perform insertion of another string into this one.
Note: I am updating the value so I can use it to display it just after through an XML page. And I am using Android 4.2.2
you can get the strings like:
String productInfo_lanIP = getString(R.string.productInfo_lanIP);
go through this link
String mystring = getResources().getString(R.string.productInfo_lanIP);
Accept the answer if it helped you :)
Related
I have a number picker and I can populate it with no problem from a regular array; however when I try to localize my app and place the array in the strings.xml file, that's when the frustration starts.
This is the strings.xml (partially)
<!--Set up arrays for the Number Pickers-->
<string name="pieces">Pieces</string>
<string name="kgs">Kgs</string>
<string name="lbs">Lbs</string>
<string name="oz">Oz</string>
<string name="gal">Gal</string>
<string name="liters">Liters</string>
<string name="pints">Pints</string>
<string name="quarts">Quarts</string>
<string name="fl_oz">Fl Oz</string>
<string-array name="all_units">
<item>#string/pieces</item>
<item>#string/kgs</item>
<item>#string/lbs</item>
<item>#string/oz</item>
<item>#string/gal</item>
<item>#string/liters</item>
<item>#string/pints</item>
<item>#string/quarts</item>
<item>#string/fl_oz</item>
</string-array>
Then in the MainActivity.kt, this is what I have:
val allUnits = arrayOf("Pieces","Kgs", "Lbs", "Oz", "Gal", "liters", "pints", "Quarts", "Fl oz")
npUnitsA.minValue = 0
npUnitsA.maxValue = allUnits.size -1
npUnitsA.displayedValues = allUnits
this works fine, but if I change that last line to:
npUnitsA.displayedValues = R.array.all_units
I get an error message:
Type mismatch.
Required:Array<(out) String!>!
Found:Int
Needless to say I'm very green at Android. Any help will be greatly appreciated.
R.java contains identifiers in int so its expected to give this error. What you can do here is
getResources().getStringArray(R.array.all_units)
to get the resource array
The Code A is a string resource, I hope to merge two string resources into one, just like Code B, can I do it in Android Studio 3.01?
Now I have upgraded to kotlin.
Code A
<string name="app_name">Backup Settings</string>
<string name="FeedbackEmailSubject">Report bug and suggestion about Backup Settings</string>
Code B
<string name="app_name">Backup Settings</string>
<string name="FeedbackEmailSubject">Report bug and suggestion about <string name="app_name"/></string>
I don't think there is such a way. Best way right now is below.
<string name="app_name">Backup Settings</string>
<string name="FeedbackEmailSubject">Report bug and suggestion about %1$s</string>
Observe %1$s in second string and in Code you do
getString(R.string.FeedbackEmailSubject,getString(R.string.app_name))
Which should give you out put like
Report bug and suggestion about Backup Settings
Im trying to read an array from my strings.xml to my code but R. is not giving me the array option, maybe it has something to do with the package? for example when i tried android.R it gave me a array option but I couldnt find the name of the strings-array object i created in xml.
Here is my code:
this.context = context;
Resources res = context.getResources();
thumbnailNames = res.getStringArray(R.array.thumbnail);
here is my xml file:
<string name="app_name">Wallpapers: Art of Autism</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="donate_sidebar">Donate for Charity</string>
<string name="who_sidebar">Who are we?</string>
<string name="mission_sidebar">What is our mission?</string>
<string name="autism_sidebar">What is autism?</string>
<string name="developer_sidebar">About the developer</string>
<string name="categories_main">Categories:</string>
<string name="aoaart_main">AoAart</string>
<string name="featured_radiobtn">Our Favorites</string>
<string name="abstract_radiobtn">Abstract Art</string>
<string name="scenery_radiobtn">Scenery</string>
<string name="animals_radiobtn">Animals</string>
<string-array name = "thumbnail">
<item >one</item>
<item >two</item>
<item >three</item>
<item >four</item>
</string-array>
Try this way,hope this will help you to solve your problem.
When you import android.R which provide android default resource.
In you case you have define your custom array in string so you can access your custom resources using YourPackageName.R reference so try to import YourPackageName.R instead of android.R
Found the answer Can't get array string resource from XML
wasnt compiling, so i just changed the name of my strings.xml file and clean build and then changed it back and clean build
I am unable to see my values in my strings.xml
When I try to see it in R.id it doesnt exist
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">BPhonelist</string>
<string name="action_add_contact">Add Contact</string>
<string name="action_phone_contact">Phone Contact</string>
<string name="action_work_contact">Phone Work Contact</string>
<string name="action_home_contact">Phone Home Contact</string>
<string name="action_email_contact">Email Contact</string>
<string name="hello_world">Hello world!</string>
<string name="detail_toast_add">Contact Added</string>
</resources>
Please if you know how I can get access it tell me.
When I try to see it in R.id it doesnt exist
that's because these are in your strings.xml. They aren't id resources. Try accessing with something like
String someString = getResources().getString(R.string.app_name);
This is assuming you are already in the Activity Context. If not, you will need to have a Context before getResources()
R.id.* values will be generated from layout files using android:id="+#id\myid"
You can reference your strings via the R.string. prefix.
I wonder if it's possible to reference a XML string value in another XML String resource.
Things like the following are possible:
<string name="title">MyTitle</string>
<string name="activityTitle">#string/title</string>
But in case of an concated resource string, I found no solution yet, so what I'm searching for is the following:
<string name="title">MyTitle</string>
<string name="subTitle">#string/title - mySubTitle</string>
So far I was only able to solve it programmically via:
<string name="title">MyTitle</string>
<string name="subTitle">%1$s - mySubTitle</string>
getResources().getString(R.string.subTitle, getResources().getString(R.string.title));
I would like to keep the string references in the string.xml file itself.
What you want is not supported at this time, AFAIK.