1

I have tried various ways asked and answered here about this with no luck, such as: android how to use string resource in a java class

In an android project I have an app class named configuration, where several static variables are stored and used in my activities etc.

For a

public static final String MYVARIABLE

I want to load a string value depending on localised language from

values/strings.xml

I have tried importing:

 import com.myappdomain.R

Then to get the string

context.getString(R.string.my_string_in_values_xml)

so that:

String FORMYVARIABLE = context.getString(R.string.my_string_in_values_xml);

Also I have tried:

String FORMYVARIABLE = 
getResources().getString(R.string.my_string_in_values_xml);

apk compiles without error, but the string is not passed in the java class producing Fatal exception, in all the java activities that utilise the static string.

Any pointers? I dont want to use extra configuration locales etc in my class just to set a string that already exists in several languages in res/values/strings.xml unless there is no other way because I want to add/delete/edit them from there.

qwertyg
  • 127
  • 10
  • you can check this https://stackoverflow.com/questions/29913722/final-variable-from-resources-file#targetText=The%20simple%20answer%20is%20that,use%20a%20non%2Dfinal%20variable. – Ankit Oct 04 '19 at 13:08
  • I have seen that answer, but i dont want to make multiple calls to the resources. – qwertyg Oct 04 '19 at 13:44

3 Answers3

0

You can try using

Resources.getSystem().getString(android.R.string.cancel)
Sanil Khurana
  • 1,129
  • 9
  • 20
0

Try below ,

String s = getActivity().getResource().getString(R.string.my_string_in_values_xm);
Sajith
  • 713
  • 6
  • 21
0

You can solve this using couple of techniques.

1.If you really want to keep the variables final, you can create a static resource variable in your app class. Like this:

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="x.x.x">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    ....


    <application
        android:name=".MyApplication"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ....

MyApplication.java

public class MyApplication extends MultiDexApplication {

    public static Resources resources;

    @Override
    public void onCreate() {
        super.onCreate();

        resources=getResources();
    }

Utility.java

public class Utility {


    public static final String TODAY_MUST_BE_BIG= MyApplication.resources.getString(R.string.today_must_be_bigger);

2.If it is not mandatory to keep those variables, final, the better solution would be to initialize them inside the Application class or MainActivity class.

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • Thank you for start, From your example i understand that I have to do this in all my activities that utilise TODAY_MUST_BE_BIG, correct? I have many activities, classes that use this, that's why I keep them all in a configuration class (I dint plan from the start that I will need localization). I thought that would be a simpler way. However, I will try your way too. – qwertyg Oct 04 '19 at 13:20
  • @qwertyg not at all! if you follow method 1, you have already the value `TODAY_MUST_BE_BIG` available to you. if you follow method 2, you have to do it once, as the values are static, it will remain as long as your app is not stopped. – touhid udoy Oct 04 '19 at 13:40
  • sorry, now i get it. And how can I resolve MultiDexApplication ? It is not recognised – qwertyg Oct 04 '19 at 14:25
  • @qwertyg check here https://stackoverflow.com/a/27284064/7360848 – touhid udoy Oct 04 '19 at 14:27
  • In my case it does not work, even if I include the multidex support library it cannot be resolved. Also I am using minSdkVersion 22 so MultiDexApplication should be enabled by default as it is stated here:https://developer.android.com/studio/build/multidex#mdex-gradle . However, this method does not work for me. – qwertyg Oct 04 '19 at 17:16
  • @qwertyg you have to reference the application class from Manifest, check the updated answer. – touhid udoy Oct 04 '19 at 17:31
  • also `MultidexApplication` is not mandatory, you can also simply use `Application` class here and not enable multidex – touhid udoy Oct 04 '19 at 17:33