[TinyTut] - Getting all DrawableIDs (or StringIDs, ...)

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

[TinyTut] - Getting all DrawableIDs (or StringIDs, ...)

Postby plusminus » Thu Jan 17, 2008 7:37 pm

[TinyTut] - Getting all DrawableIDs (or StringIDs, ...)


What you learn: You will learn how to get all Resources of the resource-classes within the R.java (like all DrawableIDs or all StringIDs). 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: Up to now there is no function within the Android SDK to retrieve all IDs of a resource-classes of the R.java. So I coded one for you:

Example-Call:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int[] allDrawableIDs = getAllResourceIDs(R.drawable.class);
  2. // or...
  3. int[] allStringIDs = getAllResourceIDs(R.string.class);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


The Source:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Necessary Imports:
  2. import java.lang.reflect.Field;
  3. // End of imports
  4.  
  5.         /**
  6.          * Retrieve all IDs of the Resource-Classes
  7.          * (like <code>R.drawable.class</code>) you pass to this function.
  8.          * @param aClass : Class from R.XXX, like: <br>
  9.          * <ul>
  10.          * <li><code>R.drawable.class</code></li>
  11.          * <li><code>R.string.class</code></li>
  12.          * <li><code>R.array.class</code></li>
  13.          * <li>and the rest...</li>
  14.          * </ul>
  15.          * @return array of all IDs of the R.xyz.class passed to this function.
  16.          * @throws IllegalArgumentException on bad class passed.
  17.          * <br><br>
  18.          * <b>Example-Call:</b><br>
  19.          * <code>int[] allDrawableIDs = getAllResourceIDs(R.drawable.class);</code><br>
  20.          * or<br>
  21.          * <code>int[] allStringIDs = getAllResourceIDs(R.string.class);</code>
  22.          */
  23.         private int[] getAllResourceIDs(Class<?> aClass) throws IllegalArgumentException{
  24.                 /* Get all Fields from the class passed. */
  25.                 Field[] IDFields = aClass.getFields();
  26.  
  27.                 /* int-Array capable of storing all ids. */
  28.                 int[] IDs = new int[IDFields.length];
  29.  
  30.                 try {
  31.                         /* Loop through all Fields and store id to array. */
  32.                         for(int i = 0; i < IDFields.length; i++){
  33.                                 /* All fields within the subclasses of R
  34.                                  * are Integers, so we need no type-check here. */
  35.                                
  36.                                 // pass 'null' because class is static
  37.                                 IDs[i] = IDFields[i].getInt(null);
  38.                         }
  39.                 } catch (Exception e) {
  40.                         /* Exception will only occur on bad class submitted. */
  41.                         throw new IllegalArgumentException();
  42.                 }
  43.                 return IDs;
  44.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


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

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests