in my android app i have into my strings.xml the following value:
<string name="Email">mail#domain.com</string>
in my main.class i have this log.e:
Log.e("-->", ""+R.string.Email);
The output is:
2131099691
Why?
R.string.email is a string resource id. In order to get the value associated with it, you need to call getResources().getString() & pass in the resource's id as an argument:
Log.e("-->", ""+getResources().getString(R.string.Email));
R.string.Email is actually a resource id of an string in xml resource.
That's why it is printing it inside log.
If you want to log actual strung value , you have to use:
context.getString(R.string.Email);
Hope it helps.
Try this:
Log.e("-->", ""+getResources().getString(R.string.Email));
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 need to get the name of a Button which I gave it with
android:id="#+id/button2"
I tried with button.getId() but then I only get this number: 2311296256.
Also .getTag() doesn´t work (I get a null value)
Does anybody know the solution?
getResources().getResourceEntryName(int id)
is what you are looking for. From the doc:
Return the entry name for a given resource identifier.
here the documentation.
So you want to get resource identifier name from resource identifier number. This should work:
getResources().getResourceEntryName(button.getId());
If you want to use getTag() you have to give the Button a tag first like this:
android:tag="sometag"
I have a little problem but I have no answer. I use the strings.xml to store all texts from my application. If i want save the next value : '??' so I have an error when I execute my app.
<string name="unknown_text">??</string>
error: Error: No resource found that matches the given name (at 'unknown_text' with value '??').
Is there a solution to save this value in xml or I must use setText() for all widgets which need it ?
Thx
It's good habit to always quote whole string:
<string name="unknown_text">"??"</string>
use <string name="questions">\??</string>
check FormattingAndStyling and this link for more detail.
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/
App version 2.3.3
Here is what i am looking for...
I have few strings in "values/strings.xml". I wish to retrieve the values depending on the string's "name" attribute and use it my application. The input to which string in the xml should be used comes dynamically. Here is an example...
My input from other file :: "A" => This value changes. It can be any value in A,B,C,D.
I have different strings in strings.xml, like...
<string name="A">AforApple</string>
<string name="B">BforBall</string>
<string name="C">CforCat</string>
<string name="D">DforDog</string>
Now, how do i programmatically get the value(AforApple) of the String with name="A".
String str = context.getResources().getString(R.string.A)
or u can use
textBox.setText(R.string.A);
do not forget to import the package com.yourpackackage.R.
You need to use getResources() method:
String a = getResources().getString(R.string.A);
Just for the record, you can also dynamically generate it using reflection.
int stringRes=R.string.class.getField("Here you put the dynamically generated input, such as A").getInt(null);
textView.setText(stringRes);
This will return the resource int value from string XML based on the input, as long as the input value "A" matches string name in the XML, this will retrieve them dynamically.
To access a String resource/value that you’ve defined in an XML file from your Android/Java code, use the Android getString method, like this:
String A = getString(R.string.a_string);
If your code gets a string like "A" and you are trying to dynamically find the string in your resources that matches that name, I don't think you can do that.
Instead of using the strings.xml, you might want to use arrays.xml and build a HashMap from that before you need to access those strings
Try this:
String word = getResources().getString(R.string.A);
Check out the link here.
You can use this code:
getText(R.string.A);
Basically, you need to pass the resource id as a parameter to the getText() method.