Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Wear type detection - Round Or Square

I am curious to know about the wear type, whether it's round or square, because on the basis of device type I want to make different views for round and square. From This Post I got to know that in SDK-20 android.view.View class has a new listener to detect device type, using this I am trying to get the device type but nothing is printing on logcat. Below is the code I tried.

public class WearSplash extends Activity implements OnApplyWindowInsetsListener {
private WatchViewStub _stub = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_wear_splash);

    _stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    _stub.setOnApplyWindowInsetsListener(this);
    _stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

        @Override
        public void onLayoutInflated(WatchViewStub mStub) {
            // here on the basis of device type i want to inflate views.
        }
    });

}
@Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
    System.out.println(" in testing phase");
    if (insets.isRound())
        System.out.println("in round");
    else
        System.out.println("in square");
    return null;
  }
}

xml files.

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="@layout/rect"
app:roundLayout="@layout/round"
tools:context="com.app.WearSplash"
tools:deviceIds="wear" >

</android.support.wearable.view.WatchViewStub>

Round:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.WearSplash"
tools:deviceIds="wear_round" >

<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:contentDescription="@string/hello_world" />

</RelativeLayout>

Square:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.app.WearSplash"
tools:deviceIds="wear_square" >

<ImageView 
    android:id="@+id/image"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:contentDescription="@string/hello_world"
    android:background="@drawable/ic_launcher"/>

</LinearLayout>

I am new to wear development, let me know where I am going wrong. Helping hands will be highly appreciated.

like image 909
Pankaj Arora Avatar asked Jul 23 '14 10:07

Pankaj Arora


4 Answers

API 23 introduced:

isScreenRound()

like image 181
rmgoncalo Avatar answered Oct 15 '22 17:10

rmgoncalo


Unofficial way (without using WatchViewStub) - but for me it was way way easier. https://github.com/tajchert/ShapeWear

Just copy ShapeWear.java class, and subscribe to screen shape detection event setOnShapeChangeListener() or call method ShapeWear.isRound() (can throw error is shape is not yet determined) or ShapeWear. getShape() - which can result in ShapeWear.SHAPE_UNSURE in same situation.

like image 31
Michał Tajchert Avatar answered Oct 15 '22 17:10

Michał Tajchert


Use WatchViewStub to inflate the view.Later in the layout file you may specify the layout for each watch type as below.It will automatically pick the correct layout as required by the device.

<android.support.wearable.view.WatchViewStub
    .
    .
    .
    app:rectLayout="@layout/rect_activity_my_wear"
    app:roundLayout="@layout/round_activity_my_wear"
    .
    .
    .
>
like image 1
Tapas Behera Avatar answered Oct 15 '22 18:10

Tapas Behera


If you need to get this information programmatically, you are correct in adding add a listener for setOnApplyWindowInsetsListener(). However, in order to trigger that, you must also call requestApplyInsets().

In any case, if you only want to know this to switch between two alternative layouts, you can use the WatchViewStub class which does this automatically (docs here).

like image 1
matiash Avatar answered Oct 15 '22 17:10

matiash