For the below code I intended to get the system date and display it as per the formatting of the current locale, it's just that for the R.string.date. In emulator it always shows up as a long number (something like 821302314) instead of "Date: " which I has already externalized in the string.xml. Can anyone help to have a look why this is so?
final TextView mTimeText = (TextView)findViewById(R.id.mTimeText);
//get system date
Date date = new Date();
java.text.DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
mTimeText.setText(R.string.date + " " + dateFormat.format(date));
layout.xml
<TextView
android:id="#+id/mTimeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/date"
/>
strings.xml
<string name="date">Date:</string>
R.string.date is indeed an int, you're missing the call to getText() or getString():
mTimeText.setText(getText(R.string.date) + " " + dateFormat.format(date));
Even better, don't build the string in your code, but use a template with getString(int resId, Object... formatArgs):
mTimeText.setText(getString(R.string.date, dateFormat.format(date)));
and in your string.xml:
<string name="date">Date: %s</string>
Yes, you will get the ID of the String if you use R.string.date.
As stated in the docs
You can use either getString(int) or getText(int) to retrieve a string. getText(int) will retain any rich text styling applied to the string.
Example:
this.getString(R.string.date);
Read about it here: getString
To get string value from xml, you should call this.getString(R.id.nameOfString). In your case this would be mTimeText.setText(this.getString(R.string.date) + " " + dateFormat.format(date));
To override all "R.string.*" to "getString(R.string.)"* i wrote a little regex.
This regex also ignores the strings who already have a "getString" in front.
((?!getString\() R\.string\.[a-zA-Z1-9_]+)
You just have to press Strg+Shift+R in Android Studio to open the replace terminal and insert the regex above as "Find" and as "Replacement" the regex below.
getString\( $1 \)
Don't forget to set "regular expression" checkbox.
For me this worked perfectly. But the "Find Regex" got one problem it only finds R.string when it starts with a whitespace. I don't know how to solve this because if i delete the whitespace ill find also the R.string that allready have the "getString".
May some can help to improve the regex or has a better way to achieve this.
Related
Does anyone know a solution for my problem?
I made a editor for my in game Shared Preferences as a Activity. There is a list with all values of one Shared Preferences. But when i write for a textView called value_theme:
value_theme.setText(R.string.editor_div_value + settings_theme);
Android uses an other string resource from an other activity.
When i write
value_theme.setText(R.string.editor_div_value + "" + settings_theme);
the app sets the TextView text to: 21312309366. Does anyone know how to fix that?
Use this:
value_theme.setText(context.getString(R.string.editor_div_value) + "" + settings_theme);
Since you are appending the string, you are mistakenly using the wrong setText method which accepts a CharSequence and sets to the TextView as it is.
Or another way is to do String formatting.
<string name="editor_div_value">Your String value %1$s</string>
To get the String in Java code:
value_theme.setText(context.getString(R.string.editor_div_value, settings_theme));
If a TextView has a georgian text, I see only spaces.
i.e
String georgString = "("+"სახლი ქუჩის ბოლოში"+")";
I see:
"( )"
If a TextView has a georgian text, I see only spaces.
I found quick solution. You need to add your gregorian text into strings.xml and then it will work.
<string name="test">(სახლი ქუჩის ბოლოში)</string>
// an usage
textView.setText(getString(R.string.test));
(I just tested it and it works. Probably if you are assigning text directly there is problem with encoding but with an usage of resources you don't have to deal with it). Let me know.
I'm wondering how gmail app is able to set the title of each line in jellybean inbox style notification differently e.g here "(Google+), Google Play, Stack Exchange" etc have brighter color text comparing to their individual details. Any clue ?
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#BigNotify
addLine() on InboxStyle takes a CharSequence. Hence, you can use a SpannedString instead of a regular String, where the SpannedString contains inline markup rules. A convenient (if a bit slow) way to accomplish this is to use Html.fromHtml() to convert simple HTML markup into a suitable CharSequence.
Able to resolve this after getting hint from CommonsWare. Thanks a lot.
Finally method looks like this
private SpannableString getFormattedString(String contact, String lastMessage)
{
String boldContact = "<b>" + contact + "</b>";
return new SpannableString(Html.fromHtml(boldContact + " " + lastMessage));
}
I am using EditText control in android, and I want to know if there is any way get the defaul string of the control(I mean the one that is in String.xml).
I use this when i want to modify its string.
e.setText( e.getText().toString + "something").
Now, the problem is that sometimes i get unnecessary information like :
" Name: JhonName: JhonName: JhonName: JhonName: Jhon "
When I just wanna show:
" Name: Jhon "
If i didnt explain properly, let me know :)
e.getText().toString() is return the string of your EditText....
now for the first time when you execute your code ..the above method will not return anything.. that's why your EditText will like "Name: John"..ok
when second time this code executes... the above method will return "Name: John"
and after that you adding "Something" so that why it happens...
just replace you code with this...
e.setText("something")
You dont need to write editText.setText(editText.getText().toString()+"something"), this will definately add "something" in the previous string. You should only write editText.setText("Something"), if you want only to print "something".
I hope that you know about Tag and Hint attributes of edit text. You can set tag and hint in both the ways XML as well as Java. So set the default (prefix) value in hint, append that value whenever you are settext to edit text.
for ex.:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name : " />
e.setText(e.getHint() + "John");
I am having trouble with setting text in a text view with format and multiple values.
holder.car.setText(R.string.mycar + lm.getCarName() + R.string.year + lm.getYear());
this is giving me " 2143545 Camero 2143213 1977 "
I have tried few other "solutions" from the web
holder.car.setText(getString(R.string.mycar) + lm.getCarName() + getString(R.string.year) + lm.getYear()); << not work, getString undefine>>
I even tried String.valueOf(R.string.mycar); getResources().getText(R.String.mycar), still it didn't work.
It would be great if someone can help me, thanks
Try this
holder.car.setText(getResources().getString(R.string.mycar));
I think you're trying to use parameters in your string.
Try this:
<string name="mycar">Car: %1$s Year: %2$s</string>
String mycar = getString(R.string.mycar);
mycar = String.format(mycar, lm.getCarName(), lm.getYear());
You should get:
Car: Camaro Year: 1977
If you want to set your textview just a string from your string.xml file,
mytextview.setText(R.String.mycar);
If you want to set your textview with combination of some strings or integers, (better than first way)
int number=5;
mytextview.setText(getResources().getString(R.String.mycar) + " " +number + " " + getResources().getString(R.String.mysecondcar));
R.string.mycar and R.string.year are only IDs for resources. For this reason you get the numbers (IDs are numeric).
To get string from resources you need to use this construction:
String myCar = getResources().getString(R.string.mycar);
and now the myCar variable holds the string you put in strings.xml file under the mycar name.
the method getResources() belongs to Context. If you run your code outside an Activity, use the context instance to get the string, like this:
String myCar = context.getResources().getString(R.string.mycar);
Try this. If you're fetching string in class without extending Activity Get using your Context
holder.car.setText(context.getResources().getString(R.string.mycar));
If you're extending Activity
holder.car.setText(yourActivity.this.getResources().getString(R.string.mycar));
Hope this helps you..
You have to retrieve your resources first, and the call the medthod getString(int), not getText, has you have put.
So, it should be:
getResources().getString(R.String.mycar);
The R class contains kind of pointers to your ressources, so you can not directly use them, use getResources().getString(), as others say.