0x52616A657368
0x52616A657368

Reputation: 378

use resourse string value in StringDef/TypeDef android

I have a stringDef/ TypeDef class as follows

public class Codes {
      public static final String Code_1 = "Code1";
      public static final String Code_2 = "Code2";
      public static final String Code_3 = "Code3";


      @Retention(RetentionPolicy.SOURCE) @StringDef({
            Code_1, Code_2, Code_3 })

      public @interface CodesMessageDef {
      }
}

I would like to set values of Code_1,2,3 from R.String.code_1 rather than manually entering.

Is there any way to achieve this usecase.

Thanks in advance.........

Upvotes: 1

Views: 565

Answers (1)

Nicolas
Nicolas

Reputation: 7111

You can read this answer for how to get application context from a static method. Note that using this to get the context is not a memory leak because you are using an application context.

Now you can do:

public static final String Code_1 = MyApplication.getAppContext().getString(R.id.code1);
public static final String Code_2 = MyApplication.getAppContext().getString(R.id.code2);
public static final String Code_3 = MyApplication.getAppContext().getString(R.id.code3);

I guess it should work.

Upvotes: 1

Related Questions