I have updated some string values in strings.xml and my application now shows not the new text but something like #234545201. I have cleaned the projected and rebuilded, there are no import android.R anywhere, just R related to my package. What went wrong?
To obtain a string from your strings.xml file, you can do a few things.
If you need it as a String object, you can use getString(R.string.string_id) to fetch the string, given an ID.
If you're trying to set the text of, say, a TextView, you can actually simply use setText(R.string.string_id) and the OS will obtain the correct string for you.
In other words, the TextView class has a method called setText(int resid), and that's also the reason why you can't write something like the following:
TextView.setText(12345680);
Are you trying to read it directly as R.string.my_string_resource?
Try passing it to getString() as getString(R.string.my_string_resource).
You can put your stings.xml file in the follwing format.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Yamba</string>
<string name="titleYamba">Yamba</string>
<string name="titleStatus">Status Update</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
</resources>
and use the name as reference of your textbox ids like.
android:text="#string/app_name"
Related
Is there a way to set the value of an xml string tag value to include that of another string tag value. The idea is like this:
<string name="tag1">"this is"</string>
<string name="tag2"><tag1 + " what I mean"</string>
Is this possible and if so how do I do it?
Is this possible and if so how do I do it?
Arithmetic, concatenation operations are not possible in android resource files.
You can concatenate strings without writing any Java/Kotlin code, only XML by using this small library I created which does so at buildtime: https://github.com/LikeTheSalad/android-stem
Usage
Based on your example, you'd have to set your strings like this:
<string name="tag1">this is</string>
<string name="tag2">${tag1} what I mean</string>
And then after building your project, you'll get:
<!-- Auto generated during compilation -->
<string name="tag2">this is what I mean</string>
I need to put all codes inside values-zh/strings.xml into values-zh-rCN/strings.xml. How do i do this?
I've tried this <include layout="#values-zh/strings" /> but did not work, Intelij is telling me to declare the file first.
Assuming values-zh/strings.xml is your base string resources, you can put all your strings there, and only provide values for those you need to override in values-zh-rCN/strings.xml.
In values-zh/strings.xml:
<string name="title">default title</string>
<string name="content">default content</string>
In values-zh-rCN/strings.xml:
<string name="title">simplified title</string>
Now when you are on simplified Chinese config, content will be 'default content', and title will be 'simplified title'.
I have resource strings :
<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>
In my application, at few places I am using these strings alone and at few places I am succeeding them by a colon.
I don't what to create another four resource strings :
<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>
Now to achieve this, I have to concatenate my resource strings with colon. I know this is very easy though java code which I can write in my activity class. But what I want is to do the concatenation in my layout file.
Question : Is string concatenation possible in layout file?
This is where I have to perform the concatenation:
android:text="concatenation_if_possible"
Using XML entities it's possible to use the same string multiple places within an XML file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE resources [
<!ENTITY appname "MrQuery">
<!ENTITY author "Oded">
]>
<resources>
<string name="app_name">&appname;</string>
<string name="description">The &appname; app was created by &author;</string>
</resources>
I used this answer: dynamic String using String.xml?
Question : Is string concatenation possible in layout file?
Nope as far as I know.
One solution is to use what #DIVA has answered before.
Another possible solution is to create a custom view that extends TextView (or the view you want to achieve this) and create a custom attribute custom:concatenate which receives a string reference and perform the concatenation automatically. IMHO I think this is the most clean approach.
In code will look as this:
<com.whatever.ConcatenateTextView
android:text="#string/whatever"
custom:concatenate="#string/second_string"/>
Or… you can use the power of Drawables creating a custom TextDrawable (which is explained very well by #Devunwired in this post and the concrete implementation of it in Github).
Copying what #Devunwired has said in his post about it:
With this class, text can now be part of the Drawable world, meaning it can not only be set alone in places where you would normally put an image, it can also be placed together with other Drawables in containers like StateListDrawable or animated with the likes of TransitionDrawable and ClipDrawable. In many cases, we can use this to do a job that would otherwise require multiple views or compound controls just to achieve a given visual effect; thus it can reduce overhead in your view hierarchy.
This combined with your custom TextView as I explained before (or whatever view you want to use) gives you a very powerful option. Again copying the example that #Devunwired wrote in his post:
ImageView mImageOne;
TextDrawable d = new TextDrawable(this);
d.setText("SAMPLE TEXT\nLINE TWO");
d.setTextAlign(Layout.Alignment.ALIGN_CENTER);
mImageOne.setImageDrawable(d);
If you need more help please let me know in the comments and I'll gladly update the answer!
You can so something like this :
<string name="meatShootingMessage">You shot %1$d pounds of meat!</string>
String strMeatMsg = String.format(strMeatFormat, ":");
textview.setText(strMeatMsg);
No, you cannot concatenate several string resources into a single string when directly referencing those strings from a layout file.
XML layout files are simply a template of instructions for Android to build a user interface, and you should consider them as a cleaner and more organized way to generate your UI than using a Java class that manually creates and positions views in a layout. That being said, there are limitations to what you can do with a layout file, and one of them is being able to reference a single string resource from every view, meaning that you can't do anything more complex than that, including concatenating several strings into one.
You can do so using this plugin I've created: https://github.com/LikeTheSalad/android-stem It will concat all of the strings you'd like to and will generate at build time a final XML string that you can reference anywhere as with any other manually string you've added.
For your case, you can do the following:
<string name="address">Address</string>
<string name="city">City</string>
<string name="country">Country</string>
<string name="pincode">Pincode</string>
<string name="address_with_colon">${address}: </string>
<string name="city_with_colon">${city}: </string>
<string name="country_with_colon">${country}: </string>
<string name="pincode_with_colon">${pincode}: </string>
And then the tool will generate:
<!-- Auto generated during compilation -->
<string name="address_with_colon">Address: </string>
<string name="city_with_colon">City: </string>
<string name="country_with_colon">Country: </string>
<string name="pincode_with_colon">Pincode: </string>
And anytime you decide to change either your template or values strings, the plugin will keep the generated strings updated. More info on the repo's page.
I know this is a simple question, but I can't figure out how to reference strings in android.
for example
on this EditText, I don't want to hardcode the string hi, I want it to reference a string in the res folder. How do I do this?
Suppose you have following string in xml.
<string name="string_one">My string</string>
You have to access this in code :
String str = resources.getString(R.string.string_one);
You can also used in xml where you have take EditText :
android:text="#string/string_one"
Create a file strings.xml in res/values folder, and add a text like
<string name="card">Card</string>
then reference the text in layout as android:text="#string/card"
Figured it out. All you need to do is create a string in the res value folder and then reference it
Create a file strings.xml in res/values/ folder, and add a text in it like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="mystring">MyStringFromXML</string>
</resources>
then reference the text in layout as :
android:text="#string/mystring"
And in Java file do it like:
String string = getString(R.string.mystring);
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.