Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6702495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:03:39+00:00 2026-05-26T07:03:39+00:00

I’m just getting started with Android dev and playing around. The documentation for getResources()

  • 0

I’m just getting started with Android dev and playing around.

The documentation for getResources() says that it will [r]eturn a Resources instance for your application's package.

In code examples I’ve seen this used to access the resources in res, but it seems like you can just access them directly.

For example to retrieve my_string from res/values/strings.xml:

<!-- strings.xml -->
...
<string name="my_string">This is my string</string>
...

You can use these two ways

The first way:

// MyActivity.java //  
... 
// No need to use getResources()
String msg = getString(R.string.my_string);
...

The second way:

// MyActivity.java //
...
// What is the point of getResources() here?
// It works but it requires "import android.content.res.Resources;" and is longer
Resources the_resources = this.getResources();
String msg = the_resources.getString(R.string.my_string);
...

So, when would you be required to use getResources() to access a resource? Seems like it is not necessary, or is it being call implicitly, or must it be called to access certain other types of resources or to access resources in other ways?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:03:39+00:00Added an answer on May 26, 2026 at 7:03 am

    Resources have many helper methods that we may require.

    R.id, R.drawable all return dynamic int assigned by android during build time. Suppose we have a requirement where we require to access a coutries flag image based on its name.

    If we have image name as us.png and the value we have is ‘us’. The 2 ways to handle it are

    if(countryName.equals("us")){
        imageview.setImageRsource(R.drawable.us);
    }
    

    OR

    Resources res = getResources();
    int imageId = res.getIdentifier(getIdentifier(countryName, "drawable"
            ,"com.myapp");
    imageview.setImageRsource(imageId);
    

    The second method will be the way to go especially when there are more than 50 countries or you will end up with a very long if-else or switch statement.

    The resources object is also used when you need to access the contents in the Assets folder

    res.getAssets().open(YOUR FILE);
    

    Resource instance can also be passed to other class files to access the the resources. These are some of the scenarios that you can use it for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.