Cannot get resource from android application

I am building an Android app, and I have a problem to get resource from string.xml.

I need to have a URL in String.xml that will be used several times through the app.

I tried Resources.getText("my_url"), but this does not work. Eclipse complains when I use this.

Do you think this is the best way to do ?


Asked by: Fiona302 | Posted: 24-01-2022






Answer 1

What you probably want is:

String myUrl = getString(R.string.my_url);

The getString() method is in Context which means that it's available directly in your Activity class since Activity is a subclass of Context. (The getString() method is also in Resources but it's easier to call on it directly on your Activity.)

What happens with your XML resources is that each is given a unique integer ID and this is added to the automatically generated R class as a public static final int. You then use these IDs to reference the resources. Have a look in the gen folder in your Eclipse project and you'll find the R class in there.

Answered by: Julia839 | Posted: 25-02-2022



Answer 2

Do you ever refer this page: https://developer.android.com/guide/topics/resources/available-resources.html ?

If you want to retrieve the String represented by a resource ID, you can call the Context.getString() method.

Or, you have to post Eclipse's complains.

Answered by: Connie681 | Posted: 25-02-2022



Similar questions

android - How to access String resource from another application

I have a application 'A' and application 'B'. Say, I have a string resource in the application 'A' <\string name="abc">ABCDEF<\/string> How do I access the value of abc from the Activity in 'B'. I tried the following method. try { PackageManager pm = getPackageManager(); ComponentName component = new ComponentName( "com.android....


java - Launch an android application with different resource file

I have an application which can be launched in 2 versions. The main difference between the 2 application are the resource file. (Some parts are renamed, but the application handle only one language) Is it possible to switch between 2 strings.xml file with @string/hello or R.string.hello with some specific settings in android ? If not, what is the best approach to that problem ? I imagined that I can use som...


Get resource ID of the icon of another android application

Is there a way to get the resource ID of the icon of another android app? (ex. com.android.systemui.R.drawable.ic_xxx) I tried context.getApplicationInfo().icon but it returned a long integer. Is this possible? Thanks


assets - How to provide some resource files for an android application?

I'm writing an android application, which user can download some image files from server. There image files will be stored in android mobile. Now I want to put some of image files inside the apk file, that user can start the application quickly after installing. I found I can put them into assets directory, but the directory is read only. When user download other image files, I need to store them into ...


refer to android library project xml resource in application xml resource

I have an Adnroid library project that defines XML resource. Let's use a selector drawable as an example (but I have problems with strings, styles, etc). LibProject/res/drawable/button_selector.xml ... defines a selector LibProject/res/layout/view.xml .... uses the selector, with no problems `<TextView android:layout_width="wrap_content" android:layout_height="w...


java - Reduce the big size of my Android application resource

I am currently developing an android application that useLibsvm library for data classification. To use the Libsvm I should provide file text describing data the size of my data=1,3G I have placed all my files in assets Folder => copied them in sdCard and then running the classification The problem Now is that my application take a lot of time...


android string resource vs application size

Is there a cost to using the android string.xml resources? If I want to keep my application size down, which of the following is better Declare my string literals within the layout files, as in <TextView android:text="Hello"> Use only one string file to store all strings as resources, as in strings.xml Use one string file per activity (so to speak), as in


Android application not found the resource ID for the activity

Very strange behavior. My application works in landscape mode only. for this i do the following <activity android:name=".CopyOfRecordVideoActivityCopy" android:theme="@android:style/Theme.NoTitleBar" android:screenOrientation="landscape"> My Layout is in layout-land folder When i open this activity first then it open in landscape mode...


aapt - How to change android application resource id value

Generally, AAPT is packaging an application with fixed resource id, and the id value is starting with "0x7f". I want to replace this integer value with other value, like "0x6f" or something. I found the code line to handle this, and modified the aapt code. (path : /frameworks/base/tools/aapt/) But, when building an application, I got the build error message like:


android - Link adb path from resource to execute shell commands in Java application

I'm looking for a way to run adb commands directly from a java application. While search on Stack Overflow I found the following solution for running shell commands, public class Utils { private static final String[] WIN_RUNTIME = {"cmd.exe", "/C"}; private static final String[] OS_LINUX_RUNTIME = {"/bin/bash", "-l", "-c"}; private Utils() { } private static <T> T[] concat(T[] f...


android application memory leaks

I am using kind of results search engine, problem is to remember the searching criteria i made singleton static class which keeps only one instance. In my application there are lots of class level private variables, lots of public static variables, a big util class which contains only static final methods. problem is my application get crash any time any where any spot and interesting thing is crash code a...


Android Application data should not be released by android OS

public class MYApplication extends Application { String property; setter getter } does above code make sure property will not be collectied by android OS if not used for a long period of time.


Android Email application - Is it possible to get all the details which are configured in Email application

Is there any way to get in the code: Email id Password SMTP host SMTP port which the user used to setup his Email account with the Android built in Email application Required because, I am written a MailSender class using JavaMail API there I need to send the mail using the details what user configured in Built in Android Email application.


Android Intent for Twitter application

Is it possible to show a list of applications (with intent.createChooser) that only show me my twitter apps on my phone (so htc peep (htc hero) or twitdroid). I have tried it with intent.settype("application/twitter") but it doesnt find any apps for twitter and only shows my mail apps. Thank you, Wouter


webserver - Retrieve data from a web server for android application

I am developing an application that needs to retrieve some data from a web server. But I have no idea how can this be possible?


Should a legacy Android application be rebuilt using SDK 2.1?

I have an Android application that uses the well known Strategies for Legacy Applications. It is build with the Android SDK 2.0 with manifest settings minSdkVersion="3" (API 1.5) and targetSdkVersion="5" (2.0). Question1: Since maxSdkVersion is not specified, the application should be...


android - Intent to pick an installed application slow in my activity, why?

If you long press on your homescreen and pick to add an application shortcut, you will be greeted with a listview showing all of your instal applications. I needed this same functionality in my application so I copied the intent from the launcher source: Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pic...


web services - How to store data accessible to both a web site and android application?

I’m developing an Android and web application that will function as a service (use the same data). My question is how should the data be stored to allow for both the web and the android application to have access to the same set of data? Should the android application connect to the sites MySQL server to store/access data? If so how do I allow someone to access the data when they are not in ...


Android 1.6 SMS (older application code)

I have HTC Tattoo with Android 1.6. I have maed a Java program for SMS sending. I got the source on the Internet, I think, versions before 1.6. The problem is: SMSs are sent twice. What is a possible cause for this problem? If possible, please simply post sample code what works OK.


How to close Android application?

I want to close my application, so that it no longer runs in the background. How to do that? Is this good practice on Android platform? If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top