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));
Related
I want to print ŒHI 5¹ in my TextView. So I have simply written :
tv.setText("Welcome to " + R.string.app_name)
where R.string.app_name is <string name="app_name"><b>ŒHI 5¹</b></string>
But the strange thing is textview is showing a number
The number is: 2131230755
I have no idea why this is happening.Please help.
use getString(R.string.app_name)
or
getResources().getString(R.string.app_name);
R.string.app_name is just a long number genereted to identify that resource. The result of string + long is just that long concatenated to that string. so you need to get the string corresponding to that identifier.
Actually every resource (layout, drawable, array, string) gets an identifier, these are put in the R file. layout identifiers are kept together in an inner class called layout, strings in string and so on.
use :
getString(R.string.app_name);
or if you are not in an activity then
mContext.getResource().getString(R.string.app_name);
You can't call direct R.string for your requirement .
Pass getResource().getString
Returns the string value associated with a particular resource ID
Finally
getResource().getString(R.string.your_string);
I am trying to do the following :
t1 is an edit text
a and b are two integers
t1.setText(a+b);
but this is not working in android but it works perfectly with javaswing
A simple solution would be:
t1.setText("" + (a+b));
or maybe:
t1.setText((a+b).ToString());
please do like the following
EditText et=(EditText)findViewById("ID of your EDITTEXT");
int c=a+b;
String s=c.toString();
et.setText(s);
This'll work fine :) If it works vote me :)
but this is not working in android but it works perfactly with
javaswing
It will not, because you're actually calling setText(int resId). See here. Calling this method will search a string in xml resources (E.g. strings.xml). Every id of a string (E.g. #strings/hello) will be compared to resId, If resId matches the string's id that string will be displayed to that widget or else you wil get a ResourceNotFoundException
To display the actual integer, convert it first to String
t1.setText(String.valueOf(a+b))
You are doing like this t1.setText(a+b); it search this id (a+b) in resource file like strings.xml that is not available. So it will throws a exception ResourceNotFoundException..
So to set the number in text view you need to convert it into string.
In Android you need to do like this:-
int sum = a + b;
String sumString = String.valueOf(sum);
t1.setText(sumString);
OR
t1.setText((a+b) + "");
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.
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.
How to Put String Variable in following Code, Please Help me.
parameters.putString("attachment","{\"name\":\"Facebook application By Martin\",\"href\":\"http://www.google.com/\",\"caption\":\"By Google Technology \",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://4.bp.blogspot.com/-9RPH9UGDSsE/TcGjy3fAHlI/AAAAAAAAAeI/kuQXLE_G5Ew/s1600/Flag+Wallpaper+of+India+%25283%2529.jpg\",\"href\":\"http://s.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"for more tips click here\",\"href\":\"http://www.google.com/\"}}}");
I use Above Code for Upload Image on Facebook Wall.But i want to use string variable & pass following path from variable.
http://4.bp.blogspot.com/-9RPH9UGDSsE/TcGjy3fAHlI/AAAAAAAAAeI/kuQXLE_G5Ew/s1600/Flag+Wallpaper+of+India+%25283%2529.jpg
Thanks in Advance.
I suggest you to refer this link: String Resources in Android.
Now Just check this example in the given link:
Define the below string inside the strings.xml:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
Now from activity class, you can get string and pass parameter value as:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
So this is the basic example for your reference.
You can also define your string value inside the strings.xml and pass the parameter as per your requirement.
String myPath = "http://4.bp.blogspot.com/-9RPH9UGDSsE/TcGjy3fAHlI/AAAAAAAAAeI/kuQXLE_G5Ew/s1600/Flag+Wallpaper+of+India+%25283%2529.jpg";
String myUrl = "parameters.putString("attachment","{\"name\":\"Facebook application By Dipak Keshariya\",\"href\":\"http://www.arthisoft.com/\",\"caption\":\"By Google Technology \",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\" + myPath + \",\"href\":\"http://s.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"for more tips click here\",\"href\":\"http://www.google.com/\"}}}");
";
Look at how I have changed the url in your call by using the concatenation operator ' + myPath + '
This is very, very basic. You should start learning java if you want to become an Android developer.
On another note, on your picture, top left corner, are you really sure you are a "PROUND" indian ?? :D