Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static access to an Android app's resources?

I have a problem re. an Android application's resources:

My application has misc. modes (edit/plan/exec), which I would like to describe using an enumeration type. I would, however, like to populate the enumeration-values with strings that stem from the resource string.xml file, i.e. instead of

enum Mode {
    EDIT ("edit"), 
    PLAN ("plan"), 
    EXEC ("exec");

    String name;
    Mode(String name) { this.name = name; }
    @Override
        public String toString() { return this.name; }
};

I would like to write something like:

enum Mode {
    EDIT (getResources().getText(R.string.mode_edit).toString()),
    PLAN (getResources().getText(R.string.mode_plan).toString())),
    EXEC (getResources().getText(R.string.mode_exec).toString()));

    String name;
    Mode(String name) { this.name = name; }
    @Override
        public String toString() { return this.name; }
}

which would e.g. allow to modify the modes' names using the resource file and thus allow for later name modifications without code changes, internationalization, etc.

The problem is, that the standard access to the resources is via an Activity's getResources()-method which is only available in the constructor (and during instance methods). The enumeration declaration, however, is part of a class' static initialization code. Is there any way to access an app's resources in a static way?

Michael

like image 220
mmo Avatar asked Aug 24 '10 19:08

mmo


People also ask

What type of content is included in resources that are used in Android static dynamic?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more.

How do I manage resources in Android?

Accessing Resources When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.

What is purpose of externalizing of resources in Android?

Externalizing your resources also allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations.


1 Answers

enum Mode {
    EDIT (R.string.mode_edit),
    PLAN (R.string.mode_plan),
    EXEC (R.string.mode_exec);

    String id;
    Mode(String id) { this.id = id; }

    public String getName(Resources r){ return r.getText(id); }

    @Override
    public String toString() { return this.name; }
}

Alternatively you can do following:

public class ClassName {
    public static Resources res;
}

In your Application.onCreate() or Activity.onCreate():

ClassName.res = getResources();

and in your enum Mode:

    @Override
    public String toString() { 
        Resources res = ClassName.res;
        if (res==null){ return super.toString(); }
        else { return res.getText(id); }
    }
like image 148
plugmind Avatar answered Sep 25 '22 18:09

plugmind