Android Question Java code in B4A Class

Discussion in 'Android Questions' started by Johan Schoeman, Apr 19, 2015.

  1. Johan Schoeman

    Johan Schoeman Expert Licensed 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?

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

    thedesolatesoul Expert Licensed User

    Try getContext().getResources() ?
     
    NJDude, Peter Simpson and DonManfred like this.
  3. Johan Schoeman

    Johan Schoeman Expert Licensed User

    Thanks. This solved it:

    Code:
    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: Apr 20, 2015
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice