How to design Layout for both Rectangular and Round
Android Watch?
Android watches comes in two shapes, round and rectangular.Therefore
WatchViewStub is available .It is a class that can inflate a specific layout,
depending on the shape of the device’s screen.
Use WatchViewStub to inflate the view as below.
public class MainActivity extends Activity {
private RelativeLayout mRectBackground;
private RelativeLayout mRoundBackground;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main_activity);
WatchViewStub stub = (WatchViewStub) findViewById(R.id.stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener()
{
@Override
public void onLayoutInflated(WatchViewStub stub) {
mRectBackground = (RelativeLayout)
findViewById(R.id.rect_activity_my_wear);
mRoundBackground = (RelativeLayout)
findViewById(R.id.round_activity_my_wear);
}
});
}
.
.
.
}
In the layout file of the main activity (main_activity.xml) you may specify the
layout for each watch type as below.It will automatically pick the correct
layout as required by the device.Also design two separate layout files for
each type of shapes.In this case rect_activity_my_wear.xml and
round_activity_my_wear.xml
Later if we want to animate the views as per their shapes on touch we can
have the logic as below
public void onLayoutClicked(View view) {
if (mRectBackground != null) {
// Logic for Rectangular shaped watch
}
if (mRoundBackground != null) {
// Logic for round shaped watch
}
}