Android dynamically adds an element to an array XML resource

Is it possible to add elements to code in a String-Array resource? For example, if I want to create a counter that shows user values, and I want to allow the user to add their own values.

+5
source share
2 answers

No. this is not supported because resources are packaged in a binary .apk file and therefore cannot be changed.

Do not follow this design pattern, change your approach.

+7
source

JoxTraex probably has no idea about the Android platform or, as they say:

when someone says that this is impossible, there will always be someone who does not know this and will do it :)

so to the point:

Resources - , ResourcesImpl.

( - )

public Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)

:

1)

2) ( ) ( , )

... " " - , LoadedApk, Activity .. , ? , ? : , : , , () / ...

- Android Context ContextImp, Context - , , :)

a) ( , ReourceImpl ( , )

public Resources(@Nullable ClassLoader classLoader) 

)

setResImpl(ResourcesImpl) 

:

Resources.getStringArray(**not existing in APK resource id here**);

, :)

addResourceById(int,Object)

:), :), :)

: - : " - ", ! - :)

:

getString:

    public class MyContext extends Context {
        ....
    // override context get resources method 
    @Override
    public android.content.res.Resources getResources() {
        // get super resources 
        android.content.res.Resources resources = super.getResources();
        // pull assets 
        android.content.res.AssetManager assets = resources.getAssets();
        // pull metrics 
        android.util.DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        // pull configuration 
        android.content.res.Configuration configuration = resources.getConfiguration();
        // construct new anon resource implementation 
        return new android.content.res.Resources(assets, displayMetrics, configuration) {

            // overrride interesting method 
            @android.support.annotation.NonNull
            @Override
            public String getString(int id) throws android.content.res.Resources.NotFoundException {
                return id == pl.ceph3us.base.common.R.string.my_sweet_google
                    ? "fck_you_google";
                    : super.getString(id);
            }
        };
    }

}
+3

All Articles