Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to detect if the clock is round?

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.

like image 766
rekire Avatar asked Mar 20 '14 06:03

rekire


3 Answers

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
    }

}
like image 77
IonSpin Avatar answered Oct 19 '22 04:10

IonSpin


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).

like image 25
Tony Wickham Avatar answered Oct 19 '22 02:10

Tony Wickham


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

like image 3
rekire Avatar answered Oct 19 '22 03:10

rekire