[TinyTut] - Get ResourceName by its ID

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

[TinyTut] - Get ResourceName by its ID

Postby plusminus » Sat Jan 12, 2008 5:30 pm

[TinyTut] - Get ResourceName by its ID


What you learn: You will learn how get Name of a Resource if you only know the ID. This Tutorial uses the Java Reflection API (no need for you to code on your own ;) ).

Difficulty: 1.5 of 5 :)

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

Description: I was not aware of a function returning the name of a Resource (what is the filename without the file-ending) of a known resource (knowing only its ID), so I coded one myself...
Example-Call:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Example-Call:
  2. String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);
  3. // resName would be 'icon'.
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

The Source:
Using the Java Reflection API this was not hard to do.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         /**
  2.          * Determines the Name of a Resource,
  3.          * by passing the <code>R.xyz.class</code> and
  4.          * the <code>resourceID</code> of the class to it.
  5.          * @param aClass : like <code>R.drawable.class</code>
  6.          * @param resourceID : like <code>R.drawable.icon</code>
  7.          * @throws IllegalArgumentException if field is not found.
  8.          * @throws NullPointerException if <code>aClass</code>-Parameter is null.
  9.          * <br><br>
  10.          * <b>Example-Call:</b><br>
  11.          * <code>String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.icon);</code><br>
  12.          * Then <code>resName</code> would be '<b>icon</b>'.*/
  13.         public String getResourceNameFromClassByID(Class<?> aClass, int resourceID)
  14.                                                 throws IllegalArgumentException{
  15.                 /* Get all Fields from the class passed. */
  16.                 Field[] drawableFields = aClass.getFields();
  17.                
  18.                 /* Loop through all Fields. */
  19.                 for(Field f : drawableFields){
  20.                         try {
  21.                                 /* All fields within the subclasses of R
  22.                                  * are Integers, so we need no type-check here. */
  23.                                
  24.                                 /* Compare to the resourceID we are searching. */
  25.                                 if (resourceID == f.getInt(null))
  26.                                         return f.getName(); // Return the name.
  27.                         } catch (Exception e) {
  28.                                 e.printStackTrace();
  29.                         }
  30.                 }
  31.                 /* Throw Exception if nothing was found*/
  32.                 throw new IllegalArgumentException();
  33.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Let us know if it helped you :)

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

Re: [TinyTut] - Get ResourceName by its ID

Postby Craigo » Fri Aug 07, 2009 9:58 pm

Thank you! This helped me out a lot!!! :)
Craigo
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Mar 17, 2009 3:30 am
Location: Sydney, Australia

Re: [TinyTut] - Get ResourceName by its ID

Postby RathaKrishna » Tue Dec 25, 2012 9:50 am

Wow.. awesome code.. thanks a lot.
RathaKrishna
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Dec 25, 2012 9:48 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests