Created
March 27, 2010 19:18
-
-
Save shikhar/346303 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fivepumpkins.common; | |
import android.content.res.Resources; | |
public class ResourceByName { | |
private final String pkg; | |
private final Resources res; | |
public ResourceByName(String pkg, Resources res) { | |
this.pkg = pkg; | |
this.res = res; | |
} | |
private int get(String type, String name) { | |
final int id = res.getIdentifier(name, type, pkg); | |
if (id == 0) { | |
final String fullyQualifiedName = pkg + ":" + type + "/" + name; | |
throw new RuntimeException("Invalid resource: " + fullyQualifiedName); | |
} | |
return id; | |
} | |
public int color(String name) { | |
return get("color", name); | |
} | |
public int string(String name) { | |
return get("string", name); | |
} | |
public int drawable(String name) { | |
return get("drawable", name); | |
} | |
public int anim(String name) { | |
return get("anim", name); | |
} | |
public int layout(String name) { | |
return get("layout", name); | |
} | |
public int id(String name) { | |
return get("id", name); | |
} | |
public int menu(String name) { | |
return get("menu", name); | |
} | |
public int xml(String name) { | |
return get("xml", name); | |
} | |
public int styleable(String name) { | |
return get("styleable", name); | |
} | |
public int style(String name) { | |
return get("style", name); | |
} | |
public int raw(String name) { | |
return get("raw", name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment