Skip to content Skip to sidebar Skip to footer

AppWidget Screen Orientation

Here is my question. I have an appwidget which works perfectly in a portrait screen orientation. However, When i changed my screen orientation to landscape my appWidget's width and

Solution 1:

When the screen orientation changes your AppWidgetProvider onUpdate method gets called. You can use the following function to get if you are in portrait or landscape mode and then update your remote views based on that.

private static boolean isPortrait (Context cx) {
Display d = ((WindowManager) cx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

if (d.getWidth() == d.getHeight()) {
    return false;
} else if (d.getWidth() < d.getHeight()) {
    return true;    
} else {
    return false;
}
}

Hope this helps.


Solution 2:

  1. Define bools.xml in both values-port and values-land

    In values-port/bools.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait">true</bool>
    </resources>
    

    In values-land/bools.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <bool name="portrait">false</bool>
    </resources>
    
  2. Get boolean by Context.getResources() in code

    private static boolean isPortrait(Context context) {
        return context.getResources().getBoolean(R.bool.portrait);
    }
    

Post a Comment for "AppWidget Screen Orientation"