Ehsan Ehsan - 6 months ago 33
Android Question

How to access Android resources e.g. dimensions in libgdx?

I need to get dimensions defined in XML files under Android folder from classes in Core directory of libgdx.

I know I can use assets folder for accessing drawables. But how about dimensions or strings?

Answer

You can't access this kind of platform specific information directly in the core project. But this can be achieved by interfacing these functions with a Facade, providing a platform-specific implementation for each target.

In libgdx-wiki there is an example for platform specific interfaces.

public interface PlatformSpecificStuff {
    public String getString();
    public int getDimensions();
}

// In -Android project
public class AndroidSpecificStuff implements PlatformSpecificStuff {
    public String getString(){
        // do whaterver you want with the resources.
        return "string resource";
    }

    public int getDimensions(){
        // do whaterver you want with the resources.
        return 42;
    }
}

// In -Desktop project
public class DesktopSpecificStuff implements PlatformSpecificStuff {
    public String getString(){
        // there is no xml or any andorid specific resource
        return null;
    }

    public int getDimensions(){
        // there is no xml or any andorid specific resource
        return 0;
    }
}

public class MyGame implements ApplicationListener {
    private final PlatformSpecificStuff pss;

    public MyGame(PlatformSpecificStuff pss) {
        this.pss= pss;
    }
}

Update: I don't know what are trying to accomplish by getting the dimensions as android resources. But you are probably in wrong track. If you want to support multiple screen sizes, have a look at viewports