This works:
Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString(), Toast.LENGTH_LONG).show();
This works:
Toast.makeText(getApplicationContext(), R.string.attemptsRemaining, Toast.LENGTH_LONG).show();
This however, does not work:
Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString() + R.string.attemptsRemaining, Toast.LENGTH_LONG).show();
All it gives me is a long number. Could someone please tell me what I'm doing wrong with the concatenation here?
You're concatenating java variable with a Resource Identifier.
Try using following:
Toast.makeText(getApplicationContext(), attemptsRemainingCount.toString() + getResources().getString(R.string.attemptsRemaining), Toast.LENGTH_LONG).show();
Your first example is using a CharSequence which in turn calls the CharSequence version of makeText:
makeText(Context context, CharSequence text, int duration)
And your second example is using the resId or linked resource id version:
makeText(Context context, int resId, int duration)
That explains why the two examples work.
What you want is a combination of both. This means you need to convert the linked resource id to a String, then concatenate it and use the CharSequence method. For example, like so:
Toast.makeText(getApplicationContext(),
attemptsRemainingCount.toString() +
getResources().getString(R.string.attemptsRemaining),
Toast.LENGTH_LONG).show();
The first two lines may look the same, but they will use different overloaded versions of Toast.makeText. The first uses the CharSequence version (because it is given a string) while the second uses the int version (because it is given a integer resource id.
Concatenating the string with a resource id (a number) still results in a string, but with the resource id converted to string.
Instead you should make the string resource into a format string (You have %1$d attempts left) and use:
getResources().getString(R.string.attemptsRemaining, attemptsRemainingCount);
To get the formatted string. See the section Formatting strings in the string resources documentation for full details.
The advantage of format strings in string resources over simply concatenating the number with a fixed string is that the former can easily be translated into other languages where the number may not be at the same place.
Related
Im trying to display a toast message with integer inside it
This is how i tried to do it:
Toast.makeText(this,bignum,Toast.LENGTH_LONG).show();
But it keeps crash my app.
Thanks for help!
Toast.makeText either takes a CharSequence or an int as its second argument.
However, the int represents a resource ID (such as R.string.hello_world).
The application crashes probably because no resource is found with that ID, since it's not an ID to start with, but an arbitrary integer.
In your case, use Toast.makeText(this,String.valueOf(bignum),Toast.LENGTH_LONG).show();.
you need a String
Toast.makeText(this, String.valueOf(bignum),Toast.LENGTH_LONG).show();
otherwise android will try to look it up for a String with id bignum, in your strings.xml file
Try this to "cast" bignum to string:
Toast.makeText(this,"" + bignum,Toast.LENGTH_LONG).show();
You can do this:
Toast.makeText(getBaseContext(), "" + bignum, Toast.LENGTH_LONG).show();
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);
Recently I had an issue very similar to the one described i.e. here: Convert String containing an ID into an Integer-ID.
In short, I had resource identifiers formatted as: R.drawable.picture and wanted to transform them to integer values so that they could be stored in an array and accessed by methods building layouts of my application.
However, following the clues given by Users didn't help to solve my problem, I always ended up with improperly formatted data.
Looking for a solution, I came to the one described below.
Details:
strings containing resource IDs were kept in a database,
full addresses of resources were used to describe them (so, for example: R.drawable.chopin instead of simple chopin - sometimes you may forget what is actually hidden behind a resource: is it a picture or a string it points to? Keeping full identifiers doesn't let you forget about types of these),
resource IDs were fetched by a method as strings (also kept in the database as text values) and then parsed into integer identifiers.
Firstly, the context had to be defined:
public DBAdapter(Context context) {
this.context = context;
dbHelper = new dbHelper();
}
As I had the context, I could have parsed the strings:
int composerPicture = context.getResources().
getIdentifier(composerPictureId.substring(11), "drawable",
context.getPackageName());
and:
int composerName = context.getResources().
getIdentifier(composerNameAddress.substring(9), "string",
context.getPackageName());
Pay attention to the quantity of characters we cut off using substring() method. When the ID starts with R.drawable., we have 11 characters and this value has to be cut off. If there is R.string., then we cut off only 9 characters. This way we end up with a pure name of our resource (drawable or string, in this case). The methods assign ID and we have an integer. After that, we can store it in an ArrayList<Integer> or wherever else.
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 trying to set String values for ids defined in xmls. I have defined the set of text which can be assigned to different ids in the layout xml files. Thereby I can also see the int values that are associated with different texts in R.java.
I have stored the variable names that I have given to the texts in my database along with the R.id prefix as they appear in R.java file.
For setting text,
TextView messageone = (TextView)findViewById(R.id.textfield1);
Normal Usage:
String message = "Hi, hello";
messageone.setText(Status);
What i want to implement:
public static final int messagestring is present in R.java
R.id.messagestring is stored in sqlite database in text format
messageone.setText(what_here);
what_here = a way to get the value from "R.id.messagestring" string as obtained from database.
I know public int getIdentifier (String name, String defType, String defPackage)
can be used here. The only change would be stored text in database will change from R.id.messagestring to messagestring. But there is a note discouraging this type of implementation.
It says: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
Android Docs getIdentifier
I think although this method seems like a longer implementation, can be efficient when the objects dealt with are not text.
There's a getString(int Id) method you can use inside an Activity. It will return a String from a given R.string Id.
String something = getString(R.string.something);
I hope I helped out a bit here, because I'm not entirely sure if I understand your question.