Friday, May 3, 2013

[android help] how to access the drawable resources by name in android


You could do something like this.-



public static Drawable getDrawable(String name) {
Context context = YourApplication.getContext();
int resourceId = context.getResources().getIdentifier(name, "drawable", YourApplication.getContext().getPackageName());
return context.getResources().getDrawable(id);
}


In order to access the context from anywhere, you may extend Application class.-



public class YourApplication extends Application {

private static YourApplication instance;

public YourApplication() {
instance = this;
}

public static Context getContext() {
return instance;
}
}


And map it in your Manifest application tag



android:name=".YourApplication"
....


.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...