Getting strings from the strings.xml file - android

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

Related

new to android not sure what import info.androidhive.imageslider.R; means

I have been working through this tutorial and came across this code:
import info.androidhive.imageslider.R;
This is important because when i copy these files from this tutorial into a new project - it gives me an error on this line of code.
can you give me a detailed explanation on what it means?
This is the import of your resource library. Resource means where your app store its drawable, layout etc. In most cases, it is under the packages name like:
if your package name is: info.androidhive.imageslider
then, your resource package name will be info.androidhive.imageslider.R
When you open a new android project, your must notice that there is a gen folder where there is a R.java file. This import actually import that file to the class where you may use some resource.
As you copied the code from other places, I think you have changed your project package name and so for that, now the app can't match it with current package for R file. Please, check it and change it according to it.
Hope it may help you.
You got this explained in the tutorial (btw: it's good to read tutorial, not just copy files from it):
I kept my package name as info.androidhive.imageslider
You must have proper import here. If you are using Eclipse, best solution would be to press CTRL+Shift+o.

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);

Main cannot be resolved or is not a field, R.layout.main (Tried other methods)

I've searched for this but I hasn't worked for me. I tried a lot. I've cleaned project, added com.package.name.R. I still get the error after cleaning. I really need help on this since I'm new to Android developing. Please make it understandable for me. Thanks and here is the code:
package com.gunnarco.gunnarscalculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.gunnarco.gunnarscalculator.R;
public class CalculatorActivity extends Activity {
public String str ="";
Character op = 'q';
int i,num,numtemp;
EditText showResult;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showResult = (EditText)findViewById(R.id.result_id);
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/result_id"
android:layout_width="fill_parent"
android:layout_height="120dp"
/>
I'm assuming that the layout file you posted is main.xml. (If it isn't, then that's your problem. The name of each layout file is used to name the fields of R.layout.)
If I'm correct and that is your entire main.xml file, then it has an error: you don't have a closing tag for <RelativeLayout ...>. If you have an error in any of your XML files, then the resource compiler will not generate an R.java file and you will have unresolved field errors.
As an aside, you don't need to import your R class in code that is in your application package (as declared in the manifest).
The one I posted was the activity_calculator
If so then that's your problem. Change your onCreate() to use that file
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
You are trying to inflate a layout file that doesn't exist so you get this error. setContentView() inflates, or shows for imports for lack of a better word, the layout to be used with this Activity. Every View (Button, EditText, etc..) you access for this Activity should be in this file, in basic circumstances.
For better understanding what has happened on your android project, I suggest you can create a new project and try to build it successfully first. Because you don't have too much code here, you can add your code one by one until everything done successfully.
Make sure you don't have any files, pictures, etc in your drawable or assets folder with capital letters, spaces, or dashes. Just use plane old lowercase. Also, try to clean your project. Go to project > clean. Then restart eclipse. Hope this helps. Also eclipse won't reconize any changes in XML files without saving first. Make sure to save after you add a button, ect

getString() from within java code

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)

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