getString() from within java code - android

I'm trying to acces my strings.xml from within the java code, but for some reason I can only acces some default android strings.
This is what I do:
private static Context context;
context = getApplicationContext();
context.getString(R.string.somthing);
The R.string contains some strings but not what I have in strings.xml.

Did you import android.R or your generated R file ?

I think you imported android.R so just remove it and import your R.class like as
import your.package.name.R;
here your.package.name is your package name which you write in your Manifest.xml file.

Use this code instead
Resources resources = context.getResources();
resources.getString(R.string.something)

Related

How to get Integer value from res/integers.xml?

Following is my integers.xml file,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="LOCATION_ALARM_INTERVAL">60000</integer>
<integer name="MID_NIGHT_ALARM_INTERVAL">86400000</integer>
</resources>
if it was strings.xml, i could have access variable like below,
getString( R.string.<variable_name> );
but how can I do same to get value from integeres.xml file ?
when I write getResources().getInteger(R.integer. it is showing me 3 variable which I haven't declared.
So How can I access the variable which I declared in integers.xml file ?
It should be done like this:
Resources res = getResources();
int i= res.getInteger(R.integer.int_value);
where int_value is the variable name given in your xml
You're looking at the android.R.integer instead of your.namespace.R.integer.
Eclipse probably imported the wrong one; it does that sometimes, it's rather annoying.
Go to your imports at the top of the file and remove:
import android.R;
Then you should be able to use the quick-fix to add the correct import.
May be you have to clean your project once before accessing these file. I do it like this and it always works.
Resources r = getResources();
int i = r.getInteger(R.integer.<variable_name>)
Try to clean your project and restart your eclipse because this is the right way of doing it.
simple code :
int maximum = getContext().getResources().getInteger(R.integer.maximum);

Error in using string resources in IntentService

I am trying to get string resource value from IntentService as follows :
String errorTitle = getResources().getString(R.string.no_Internet_connection_error_title);
But it shows an error no_Internet_connection_error_title cannot be resolved or is not a field.
When I type R.string., eclipse shows a list of android defined strings, not defined by me.
I use string resource in activity, but not able to use in IntentService.
I thought one of the guys who commented will post an answer. But as they have not I am posting it.
The solution was either to import R.java file using package name :
import com.android.myApp.R;
Or to use whole package name to get string :
getResources().getString(com.android.myApp.R.string.no_Internet_connection_error_title);

Getting strings from the strings.xml file

I'm currently working on internationalization to my android application. This is the way I try to get the strings from the string.xml
tv.setText(context.getResources().getString(R.string.));
But Eclipse does not suggest any of my pre defined strings, just the standard Android Strings. My Folder structure look like this:
res/values/strings.xml
and
res/values-NO/strings.xml
I can see that the strings in these to files has some memory addresses in the R.java file, like this one for instance:
public static final int enterPassword=0x7f06003c;
I tried to Clean the Project, but its strange that eclipse doesnt suggest anything, the string exist in the R.java
Where is the problem ?
You should import you R class.
import xxx.xxx.R;
not
import android.R;
your xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="yellow">Yellow</string>
</resources>
and in java code:
tv.setText(getResources().getString(R.string.yellow));
and import android.R eventhough error exists than try to run the program than it will go off
import R with your Package Path instead of android.
like import com.example.R
instead of import android.R;
try this
Resources resources = getResources();
String string = resources.getString(R.string.text);
TextView tv= (TextView)findViewById(R.id.text);
tv.settext(string);
Please verify the imported R file path
Link for more information
Use this:
String tab_label = getResources().getString(R.string.));
and ensure that you are not imported android.R

Set package name at top of java files with a string reference (android)?

The package name on top of the java files, can it be set in one spot such as the strings file?
pseudo code
package com.website.app_name; <=======
imports....
public class Main extends Activity{}
No You can not do that, Like you can not change import statement like import com.xyz.ABC with a string.
And why one would even need this.
No, Java doesn't allow that...

The import com.android.internal.R cannot be resolved

Hi i am working with Gestures and i need to import but i m getting error
com.android.internal.R;
The import com.android.internal.R cannot be resolved
kindly help me , please
You don't say why you need access to com.android.internal.R, but the sad fact is that you simply cannot import it (the "internal" is a clue that it's not part of the public API). Google doesn't expose this because it is subject to change.
It is possible to get to the internal resources by calling Resources.getSystem(). To get the value of a particular resource identifier, you have to know its name and then use code like the following to find the value:
Resources res = Resources.getSystem();
int id = res.getIdentifier("resource name", "resource type", "android");
Be aware that any name that you use could disappear in future versions of Android.
I have a couple suggestions:
1) Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.
2) Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.
Yes you can use the internal R with some dirty trick (dirty trick = Java reflection).
Just an example:
Class clasz = Class.forName("com.android.internal.R$styleable")
Field field = clasz.getDeclaredField("TextAppearance");
field.setAccessible(true);
int[] textAppearanceStyleArr = (int[])field.get(null);
field = clasz.getDeclaredField("TextAppearance_textSize");
field.setAccessible(true);
int textSizeStyle = (Integer)field.get(null);
First of all, what is Gestures ?
Do you have a package named com.android.internal in your gen folder ? Doest it contain R.java ?
If not, try Project->Clean in Eclipse. If it still doesn't work, you may have an error in your XML layout files.

Categories

Resources