Android Question Java code in B4A Class

Johan Schoeman

Expert
Licensed User
Longtime User
The below code compiles when included in the main activity of a B4A project. But when added to a B4A class it complains about getResources(); How do I get around this?

B4X:
    public int dp2px(int dip) {
        Resources resources = getResources();
        int px = Math
                .round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                        dip, resources.getDisplayMetrics()));
        return px;
    }
 

Johan Schoeman

Expert
Licensed User
Longtime User
Thanks. This solved it:

B4X:
        public int dp2px(int dip) {
        Resources resources = BA.applicationContext.getResources();
        int px = Math
                .round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                        dip, resources.getDisplayMetrics()));
        return px;
    }
 
Last edited:
Upvote 0
Top