String Manipulation Advice - android

I would like to include the UserName in the AppBar label / title area at the top of the layout. To my understanding res\values\strings.xml is a great place to put some constant values like the AppName. So I have
<resources>
<string name="app_name">MyFirstApp</string>...
In the manifest I have #string/app_name for the Label field.
So how do I go about getting the username added to the label?
I thought about creating a second string res constant named str_UserName. The value would be blank until authentication then programatically update str_UserName after succesful login. The trouble I have there is concatenating in the manifest. It doesn't like any combination I come up with. (for example #string/app_name + #string/str_UserName or #string/app_name + str_UserName etc.)
Then Googling I read up on string arrays. But in my attempts to use a string array it apears to not properly work for the manifest. I'm assuming because you can't just reference the array without and indexer for which element to display??
So in the end what I want is for the App Name to be left justified and preferrably use a constant value and the username be right justified and of course dynamic based upon the user.
TIA
JB

The getString method takes parameters, and works similarly to the String.format method. This is not very often used, but it is documented and works fine. So you can do something like that:
<string name="my_custom_title">MyFirstApp for user %s</string>
And, once you have your username:
setTitle(getString(R.string.my_custom_title, username));
Source: The Context API

You can't change values / strings in res/values/ and you can't concatenated them either.
But it's not required. Once you have the username:
setTitle(getString(R.string.app_name) + " " + username);
and the title will change.

Related

Android-Studio: How to make a resource string value the same as its name

I want to use the string resources to save some key values. So, I have something like this:
<string name="key_name">key_name</string>
The resource name and the resource value are the same. There is no need for me to make a difference between them, as it is just to cleanly store one value.
To reduce redundant information,
is there a way to just tell android-studio that the name equals the value? Something like this?
<string name="key_name"/>
I think you can't do it. It looks like you create a variable, you must name it and when you need you can set its value.
You can't create a variable with same values with name at the same time.
I think you have the wrong resource type. A string resource is used for mapping a name to a text. From what you're describing here you only need the name (and then can extrapolate the text from it).
Using strings with matching name and value shouldn't be necessary:
If you're going to be referencing them from Java or Kotlin code, you still need to know the name (i.e. R.string.key_name), so you might as well just use the string value there.
If it's for using inside XML code (where you want to pass the value of a string, e.g. text="#string/key_name") most of the time you can just use the raw string there (i.e. text="key_name").

How to including a link in a body of text in android string resource

I have a string resource named large_text as
<string name="large_text">
"lots of texts"
...
"lots more text\n\n"
...
"I was not kidding"
"so here is the phrase I want to make a hyperlink"
</string>
So how do I make a short phrase within that large body of text into a hyperlink? The suggestion here is not working: string.xml will not even compile. It gives the error that
Error: The content of elements must consist of well-formed character
data or markup.
Even i faced this issue while using it from strings.xml so i ended up using it like this and it works for me.
useageTerms.setText(Html.fromHtml("By tapping register, you agree to the\n " +
"Terms & Conditions for Google"));
useageTerms.setMovementMethod(LinkMovementMethod.getInstance());

Copy string to strings.xml

Everybody knows that if we have:
ekran3.setText("VAT Tax:");
We may (or even we SHOULD) convert it to:
ekran3.setText(getString(R.string.kwotaVat));
and add in strings.xml:
<string name="kwotaVat">VAT Tax:</string>
But is there some kind of trick to do it automatically? For example by clicking RMB on text and selecting some option? It would be nice to know it in fact it will save us a lot of time than while we're doing it manually.
If you are using Eclipse you may extract the string directly into the strings.xml file by placing the mouse within the string and hitting Ctrl + 1. It will bring up the dialog as followed and you may select "Extract String". You then give it a name (Ex: kwotaVat) and you're done.
hey you do not need to use getString() to convert it to string the values xml file is already having data in string form so you just need to use the following code to set the string
ekran3.setText(R.string.kwotaVat);
where ekran3 is the object of your text view
and kwotaVat is the id of your value string
for more detail od android codes have look here http://grabcodes.blogspot.com/

string formatting issue in android

I am facing an issue with "string.format" in android application. In my application when the user changes his language preferences from default (english) to any other foreign language (japanese,german etc) the variable string positioning is giving a force close error. Please refer the code below:
temp = String.format(locale,getResources().getString(R.string.temp_string), value, name);
where, temp_string = "The parcel number %1$d belongs to %2$s" for default selection (english)
when other languages are selected in some of them %2$s comes before %1$d . Due to which the the application force closes. Is there a way around to dynamically handle the variable strings(value,name).
I'd do something like:
temp = getResources().getString(R.string.temp_string, value, name);
As you see, getString() method can also receive parameters to format. Then, place different strings resources on different folders. For instance:
res/
values/
string.xml <--- here you put "The parcel number %1$d belongs to %2$s"
values-de/
string.xml <--- here you put "The parcel number %2$d belongs to %1$s"
I'm just giving you an example; I actually do not know how germany order is. I just want to explain what you actually have to try.

Dynamically referencing a String in Strings.xml in Android

Say I have the following my strings.xml file:
<string name="string0">Lorem</string>
<string name="string1">ipsum</string>
<string name="string2">dolor</string>
In my activity an ID is set based on the clicking of a button. If the top button is clicked the id is 0, middle is 1 and bottom button is 2.
What would the syntax look like for referencing one of the three strings?
I know R.string.string0 works but I want to do something equivalent to:
R.string["string"+currentID]
where I derive the string to use based on the ID.
Just not sure what the syntax would look like in Java/Android.
Thanks in advance,
Tony
Could you not just use a string array in your resources instead of separate string entries?
That's a bad approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs.
If you really insist on using a string-based approach, use Resources.getIdentifier().

Categories

Resources