Reputation: 47965
Might be I missed something but is there any flag for knowing if the clock is round or square?
I could imagine that this is important if you want to design the background of the notifications.
Upvotes: 23
Views: 4882
Reputation: 1720
Following IonSpin's answer, the following lines work just fine:
if (getResources().getConfiguration().isScreenRound()) {
//code for round wearables
} else {
//code for notround wearables
}
Upvotes: 1
Reputation: 3593
I'd like to add my own two cents here because I think I found a solution, although a bit dummy, rather simple and straightforward.
Why ? First, the two solutions exposed by @IonSpin are perfectly working, but the first require SDK 23 minimum, and the other is asynchronous...
Using the -round
and -notround
qualifiers, simply add a dummy empty view in your Activity XML Layout that is inside the layout-round
folder as followed :
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/isRound"/>
Then in your Activity onCreate()
, simple do:
Log.d(TAG, "is not round ? " + (findViewById(R.id.isRound) == null));
And it should print on a square smartwatch:
MainActivity: is not round ? true
As a new developer on Android Wear, I find this solution a bit unbelievable but it does work with my Sony SmartWatch 3 and on a Virtual Round Android Wear device.
The only shortcoming / trade-off here is that you have to place a dummy view in all your layouts...
Can you guys confirm it works for you as well ?
Upvotes: 0
Reputation: 1252
Update for API 23:
To determine if the screen is round you can use
context.getResources().getConfiguration().isScreenRound()
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
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
}
}
Upvotes: 23
Reputation: 47965
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>
Upvotes: 3
Reputation: 3571
The windowinsets approach failed in my devices, so I did a small trick, ugly but functional.
If you need to check inside your activity then add in your layout a hidden textview with the value "1"
in the round layout and "0"
in the rect layout.
<TextView
android:id="@+id/isRound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:text="1" />
Then in the activity:
TextView isRoundText = (TextView) view.findViewById(R.id.isRound);
boolean isRound = "1".equals(isRoundText.getText());
Upvotes: 0
Reputation: 4766
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).
Upvotes: 3
Reputation: 2685
If I understood correctly it'll be using standard Android resources, meaning you can just put your layout in layout-circle and you're done.
Upvotes: -1