Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override getResources in Activity

I've tried to extend Resources class, so i can override getString(int id) methode, so can do some effects to every String needed within my Activity, here i provide some my code:

public class CustomResource extends Resources {

    public CustomResource(AssetManager assets, DisplayMetrics metrics, Configuration config) {
        super(assets, metrics, config);
    }

    @Override
    public String getString(int id) throws NotFoundException {
        return super.getString(id) + " $$";
    }

}

and in my Activity :

CustomResource customResource;
    @Override
    public Resources getResources() {
        if (customResource == null) {
            DisplayMetrics metrics = new DisplayMetrics();
            super.getWindowManager().getDefaultDisplay().getMetrics(metrics);
            customResource = new CustomResource(getAssets(), metrics, super.getResources().getConfiguration());
        }
        return customResource;
    }

but there's error shown on Runtime:

Caused by: java.lang.NullPointerException

at com.test.TestActivity.getResources(TestActivity.java:75)

and it points to:

super.getWindowManager().getDefaultDisplay().getMetrics(metrics);
like image 330
Mohammad Ersan Avatar asked Jul 31 '11 13:07

Mohammad Ersan


1 Answers

Found a way:

private CustomResource customResource;

@Override
    public Resources getResources() {
        if (customResource == null) {
            customResource = new CustomResource(super.getAssets(), super.getResources().getDisplayMetrics(), super
                    .getResources().getConfiguration());
        }

        return customResource;
    }

even after adding this resources, its still doesn't override the Strings values coming from the LayoutInflator (ex. setContentView(R.layout.sample))

like image 138
Mohammad Ersan Avatar answered Oct 12 '22 14:10

Mohammad Ersan