[TinyTut] - Get Resources by Name ( getIdentifier(...); )

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] - Get Resources by Name ( getIdentifier(...); )

Postby plusminus » Mon Jan 07, 2008 5:58 pm

[TinyTut] - Get Resources by Name ( getIdentifier(...); )


What you learn: You will learn how get Resource-IDs by their name, using the getResources().getIdentifier(...); Method

Possible Scenario: You have a sequence of resource-images with almost the same name and want to get the IDs of all of them.

Difficulty: 1 of 5 :)

:idea: Questions/Problems: Simply post below...

Description:
For example: I have a file bug.png in the "/res/drawable/", so i get its ResourceID with the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
  2. // or
  3. int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

[hr]
[hr]
The function we are using in general:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public int getIdentifier(String name, String defType, String defPackage)
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

It can be used in two ways (depends on which way you prefer) as you can see above:
1. "name" can be the 'full path' to the resource, built up like this (the params defType and defPackage remain null)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. full_package:type/filename_without_ending
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

2. Split up filename, type and package

Thats it :)

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby murmeister » Thu Mar 20, 2008 8:05 pm

How would you reference this from a different class?

I have a class of my own definition and I want to access images in the drawable directory by name. When I try to make use of the the getResource() method, I get an error saying it's undefined for my class. I've included the import for android.content.Context. The code I'm trying to use is:

int resId = getResources().getIdentifier(fileName, null, null);
_image = BitmapFactory.decodeResource(getResources(), resID);

The filename is a fully qualified string including the package name and drawable path.

I'm basically trying to read images from the resources into memory for subsequent quick access. The images are named according to a defined pattern that I can load based on program parameters.

Do I need to pass the base context to my sub class? I see that all other examples preface the getResources() method with the context passed in the onCreate() override. I don't really want to do that unless I absolutely have to.
murmeister
Freshman
Freshman
 
Posts: 2
Joined: Sun Mar 16, 2008 6:53 pm

Postby gymshoe » Mon Oct 20, 2008 10:34 pm

HI,
It has been a while, so maybe you have figured out a slick solution to this problem.
If so, could you post it? :D

If not, the only workaround I could find was what you mentioned regarding context:
In my main application (which launches) I declare a variable appContext: "public static Context appContext;"
Then, in the onCreate() method for this main application, I assign: "appContext = this;"

Because appContext is public, any other class in this package can then use appContext to track down the XML resources without the error you described.

Thanks, for any better solution.

Jim
gymshoe
Junior Developer
Junior Developer
 
Posts: 15
Joined: Wed Sep 10, 2008 6:07 am

Postby Manfred » Tue Feb 10, 2009 2:33 pm

Hi!

I need to get resources dynamically in my application, therefore i need this! But if i make it like following:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String iconName = "k_9";
  2.  
  3. int res = getResources().getIdentifier("com.andriod.blackjack:drawable/"+iconName, null, null);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


it doesnt work. res has the value 0. Also if i try this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int res = getResources().getIdentifier("com.andriod.blackjack:drawable/k_9", null, null);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


And in the R.java there is this image:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     public static final class drawable {
  2.  
  3.         public static final int k_9=0x7f02001f;
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Can anyone give me a hint!?

Thanks
Manfred
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Feb 10, 2009 2:13 pm

Postby Manfred » Tue Feb 10, 2009 2:49 pm

I have started Eclipse and my Emulator new, now it works!?
Manfred
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Feb 10, 2009 2:13 pm

Re: [TinyTut] - Get Resources by Name ( getIdentifier(...);

Postby csengyee » Mon Jul 13, 2009 3:22 pm

plusminus wrote:
[TinyTut] - Get Resources by Name ( getIdentifier(...); )


What you learn: You will learn how get Resource-IDs by their name, using the getResources().getIdentifier(...); Method

Possible Scenario: You have a sequence of resource-images with almost the same name and want to get the IDs of all of them.

Difficulty: 1 of 5 :)

:idea: Questions/Problems: Simply post below...

Description:
For example: I have a file bug.png in the "/res/drawable/", so i get its ResourceID with the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int resID = getResources().getIdentifier("org.anddev.android.testproject:drawable/bug", null, null);
  2. // or
  3. int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

[hr]
[hr]
The function we are using in general:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public int getIdentifier(String name, String defType, String defPackage)
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

It can be used in two ways (depends on which way you prefer) as you can see above:
1. "name" can be the 'full path' to the resource, built up like this (the params defType and defPackage remain null)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. full_package:type/filename_without_ending
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

2. Split up filename, type and package

Thats it :)

Regards,
plusminus


Thanks for sharing. I am currently writing a free application that allows user to end any running task.
I managed to pull out all the running tasks (limited to 20) but how do I get the name of the application. Any clue on this?

Context context = getApplicationContext();
Resources appR = context.getResources();
ActivityManager actmgr=(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> appList = actmgr.getRunningTasks(20);

for (int i=0;i<appList.size(); i++) {
String pkname = ((RunningTaskInfo)(appList.get(i))).baseActivity.getPackageName();
CharSequence txt = appR.getText(appR.getIdentifier("app_name", "string", pkname));
Log.v ("MyApp", txt.toString());
}

So far, I manage to retrieve my application name since I have indicated "app_name" in my androidmanifest.xml. However, there are some applications that do not indicate app_name(eg. Android native application). Any clue on this will be greatly appreciated. Thanks.
csengyee
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Jul 13, 2009 3:07 pm

Top

Postby Hachaso » Thu Jan 14, 2010 3:11 pm

Hi!

You wrote in the example:

Possible Scenario: You have a sequence of resource-images with almost the same name and want to get the IDs of all of them.

How exactly could you do this.
I have been trying but I can't get the API to return all the resource files with the same beginning of name.
For instance if I have.

test1.png, test2.png and test3.png

I would like it to give the ID's of all the 3 resources. Maybe loop through to get them.
But how?

Thanks
Hachaso
Developer
Developer
 
Posts: 43
Joined: Mon Jan 26, 2009 11:44 am

Postby karnbo » Sun Feb 14, 2010 1:05 pm

Tnx. Amazing this is not described better in the API. This example helped me out.
karnbo
Freshman
Freshman
 
Posts: 3
Joined: Fri Feb 12, 2010 3:40 pm

Postby sheikh.aman » Thu May 06, 2010 2:09 pm

Ahh!
Thanks you soooooooooooo much
i had 300 of such resources, i never wanted to type out their names! :-)
u saved me brother!
Hats off to u!
Impossible is Nothing
User avatar
sheikh.aman
Developer
Developer
 
Posts: 40
Joined: Sat Mar 20, 2010 7:52 am

Problem to use Try catch with this

Postby netmonster » Sun Jul 11, 2010 12:40 pm

Hello every body. I'm a Newbie in Android development and I'm trying to create a simple dictionary-like application where i have a list of specific words (Ecodes used in food: like E100, E200...) and the explanation of their composition. So the application should simply show the explanation of the typed word. So i tried your trick by using the string XML and defining all these words then access them by creating a small method that takes the Code and returns its Explanation in an array (because i used the array structure in the XML in order to put different informations and i know their meaning through their indexes):
Code: Select all
private String[] getInfo(String CODE) {
                                try
            {
               Resources res=getResources();
               int resid = res.getIdentifier(CODE, "array", "com.saifsoft.ecodes");
               String[] Info= res.getStringArray(resid);
               return Info;
            }
            catch (Exception e) {
               // TODO: handle exception
               return new String[]{"Not Found"};
            }
}

This works great and i get the right answer whenever i type a CODE that exists in my string.xml.
The problem comes when the user types a wrong or an Ecode that doesn't exist in my file. All the application crashes and it seems that the try catch is not really working. Does anyone have an Idea why?

Thanks
netmonster
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Jul 11, 2010 12:29 pm

Re: [TinyTut] - Get Resources by Name ( getIdentifier(...);

Postby sheikh.aman » Tue Jul 13, 2010 4:55 pm

did you try printing or logging something out in the catch block?
I mean, I want to know, what is the exception?
Maybe this thing works okay but something else is creating a problem!?
I am not sure about what it returns when nothing matching is found. is it an array with length=0?
Impossible is Nothing
User avatar
sheikh.aman
Developer
Developer
 
Posts: 40
Joined: Sat Mar 20, 2010 7:52 am

Re: [TinyTut] - Get Resources by Name ( getIdentifier(...);

Postby Mikey30000 » Sun Sep 19, 2010 3:53 pm

These tutorials have been sooooo damn useful

Keep em coming plusminus!
Mikey30000
Freshman
Freshman
 
Posts: 2
Joined: Tue Aug 10, 2010 6:46 pm

Re: [TinyTut] - Get Resources by Name ( getIdentifier(...);

Postby uibi » Mon Sep 20, 2010 11:27 pm

No idea what files you typed this code in to. You lost me on first row of code... is it the main.xml or what?
Being completely new to programming i find it very hard to follow most guides (in my book as well) since the name of the file edited is left out. I might be a complete R-tard but being new to the Eclipse environment I'm overwhelmed as it is.
uibi
Freshman
Freshman
 
Posts: 3
Joined: Mon Sep 20, 2010 7:56 pm

Re: [TinyTut] - Get Resources by Name ( getIdentifier(...);

Postby tomtom81 » Fri Sep 24, 2010 4:24 pm

Thank you so much for this help.
How could i get a drawable from a int ?
tomtom81
Freshman
Freshman
 
Posts: 2
Joined: Wed Jul 07, 2010 1:24 am

Re: [TinyTut] - Get Resources by Name ( getIdentifier(...);

Postby Calamera » Fri Mar 04, 2011 10:39 am

Instead of typing your own packagename why not using context.getPackageName() ?
Calamera
Freshman
Freshman
 
Posts: 3
Joined: Fri Dec 17, 2010 8:52 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests