I am looking for a technique to determine in Java if the screen of an Android Wear device is round or rectangular. Please note that this is not only about layouts; my code really needs to know what form it works with, because they are handled differently.
As far as I can see from the code examples on the Internet, two different approaches should be possible - but I could not get them to work. I will include them here to eliminate them from work, or for possible troubleshooting (if someone can see a problem with them). Please do not refer me to another SO post which simply repeats solutions that do not work for me here.
Please note that all the code here works on the clock. In addition, I still use Eclipse, FWIW.
The simplest method I've seen involves adding an onApplyWindowInsets() listener to the view in my layout. So I created a listener that looks like this:
@Override public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { if (insets.isRound()) { displayShape = "round"; } else { displayShape = "rectangular"; } return null; }
and added it to the root view of my layout with this code:
view.setOnApplyWindowInsetsListener(this);
in my onCreate () method. It looks good as possible, but the listener was never called. I also found a tip saying that I need to call it manually, as such:
view.requestApplyInsets();
but that doesn't seem to make any difference. I experimented with placing it on different representations, in different methods of the life cycle, etc., but I have never seen that it was really called in my application. This works on my LG G Watch, BTW.
The second approach is a bit of a hack and is based on a helper class published by WatchViewStub . I jumped over hoops to get a downloadable support library imported into an Eclipse project, and then added the following to my root layout:
<android.support.wearable.view.WatchViewStub android:id="@+id/watch_view_stub" android:layout_width="match_parent" android:layout_height="match_parent" app:rectLayout="@layout/rect" app:roundLayout="@layout/round" />
and created rect.xml as such:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout_type" android:text="rectangular" />
and round.xml as follows:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/layout_type" android:text="round" />
Finally, in my onCreate() I added the following Java code:
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { @Override public void onLayoutInflated(WatchViewStub stub) { TextView layoutType = (TextView) findViewById(R.id.layout_type); displayShape = layoutType.getText().toString(); } });
It's a long way around a block, but it should work, right? Not much ... displayShape always set to "rectangular" , indicating that it is always rect.xml used, even when working on a circular emulator. [I do not have round-screen equipment to try it for now.]
So does anyone see where I made a mistake with either of these two approaches? Or can you suggest a third way that really works?