Is there any way to detect if the clock is round?
Following IonSpin's answer, the following lines work just fine:
if (getResources().getConfiguration().isScreenRound()) {
//code for round wearables
} else {
//code for notround wearables
}
Starting with API 23 you can use the -round
and -notround
resource qualifiers. So there is no need in some weeks to use the WatchViewStub
.
Just as a small example take this layouts:
layout-round/example.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/round_string"/>
layout-notround/example.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/square_string"/>
Of cause you could also use just one string:
layout/example.xml
:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/string"/>
values-round/strings.xml
:
<resources>
<string name="string">I am round</string>
</resources>
values-notround/strings.xml
:
<resources>
<string name="string">I am square</string>
</resources>
source
Update for API 23:
To determine if the screen is round you can use
context.getResources().getConfiguration().isScreenRound()
Android reference
Or you can use the round
and notround
resource qualifiers and let the system apply the proper resources but beware that this will not work on devices running API 22 or lower
Source
Thanks to people who pointed out this change.
Old asnwer
From google I/O talk (https://www.youtube.com/watch?v=naf_WbtFAlY#t=284):
From SDK 20 android.view.View
class has
public void setOnApplyWindowInsetsListener(OnApplyWindowInsetsListener listener)
and OnApplyWindowInsetsListener
has a callback method onApplyWindowsInset
public WindowInsets onApplyWindowInsets(View view, WindowInsets windowInsets){
if (windowInsets.isRound()){
//Do stuff
}
}
You will probably want to use the WatchViewStub, which is included in the wearable-support library. In the stub, you specify a roundLayout and rectLayout, and the correct one will automatically be inflated based on the user's screen shape. See the WatchViewStub sample app that is included in the SDK for an example of how to do this.
EDIT: The source code for the wearable samples is now online. For this case, take a look at the WatchViewStub sample, which "demonstrates how to specify different layouts for round and rectangular screens": https://developer.android.com/samples/WatchViewStub/project.html
Quick reference:
In the layout:
<android.blah.WatchViewStub
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect"
app:roundLayout="@layout/round" />
where rect and round are different layouts for rectangular and round displays.
In the Activity:
setContentView(R.layout.main);
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
@Override
public void onLayoutInflated(WatchViewStub stub) {
mTextView = (TextView) stub.findViewById(R.id.text);
Log.d(TAG, "TextView: " + mTextView.getText() + " view=" + mTextView);
}
});
The onLayoutInflated is optional, but it lets you see what is in the layout that got inflated, so you could do addtional customizations based on which layout the WatchViewStub chose to inflate (rectangular or round).