Recently I was trying to make some adjustments to a screen size for Android Wear so the watch would match the screen size. Most of the ways to do this always seem roundabout to me. I wanted a way to call a simple function like isRound()and get the information I need. I found a custom view someone made on the forms and modified it to return whether the screen is round or square as well.
Here is the custom view class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | public class WearLinearLayout extends LinearLayout { private int chin; private boolean isRound; public WearLinearLayout(Context context) { super(context); } public WearLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } public WearLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public int getChinSize() { return chin; } public boolean isRound() { return this.isRound; } @Override public WindowInsets onApplyWindowInsets(WindowInsets insets) { this.chin = insets.getSystemWindowInsetBottom(); this.isRound = insets.isRound(); return insets; } } |
It can then be implemented in the layout just like a LinearLayout.
Here is an example of how I used it to change the padding size of the bottom of the layout if the watch wasn’t round or had a chin:
1 2 3 | if(!linearLayout.isRound() || linearLayout.getChinSize() > 0) { int paddingBottom = getResources().getDimensionPixelSize(R.dimen.square_chin_padding_bottom); controls.setPadding(0,0,0, paddingBottom); |
Another Way…
Here’s a method I got from Sterling Udell on the Android Wear Developers Community.
Declare two resource values in bools.xml
1 2 3 4 5 6 7 8 9 | values/bools.xml: <resources> <bool name="is_round">false</bool> </resources> values-round/bools.xml: <resources> <bool name="is_round">true</bool> </resources> |
and call getResources().getBoolean()
You can then check if the height is smaller than the width of the screen on a round screen to see if it has a chin. I made a class I use in my projects called DimenHelper to do just that:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class DimenHelper { public static int GetScreenWidth(Context context) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.widthPixels; } public static int GetScreenHeight(Context context) { DisplayMetrics displayMetrics = new DisplayMetrics(); ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.heightPixels; } } |
What do you think? Does this seem like an acceptable method, or is there an easier way to do this?