1*90c8c64dSAndroid Build Coastguard Worker /* 2*90c8c64dSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project 3*90c8c64dSAndroid Build Coastguard Worker * 4*90c8c64dSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*90c8c64dSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*90c8c64dSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*90c8c64dSAndroid Build Coastguard Worker * 8*90c8c64dSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*90c8c64dSAndroid Build Coastguard Worker * 10*90c8c64dSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*90c8c64dSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*90c8c64dSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*90c8c64dSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*90c8c64dSAndroid Build Coastguard Worker * limitations under the License. 15*90c8c64dSAndroid Build Coastguard Worker */ 16*90c8c64dSAndroid Build Coastguard Worker 17*90c8c64dSAndroid Build Coastguard Worker package com.example.android.wearable.gridviewpager; 18*90c8c64dSAndroid Build Coastguard Worker 19*90c8c64dSAndroid Build Coastguard Worker import android.app.Activity; 20*90c8c64dSAndroid Build Coastguard Worker import android.content.res.Resources; 21*90c8c64dSAndroid Build Coastguard Worker import android.os.Bundle; 22*90c8c64dSAndroid Build Coastguard Worker import android.support.wearable.view.DotsPageIndicator; 23*90c8c64dSAndroid Build Coastguard Worker import android.support.wearable.view.GridViewPager; 24*90c8c64dSAndroid Build Coastguard Worker import android.view.View; 25*90c8c64dSAndroid Build Coastguard Worker import android.view.View.OnApplyWindowInsetsListener; 26*90c8c64dSAndroid Build Coastguard Worker import android.view.WindowInsets; 27*90c8c64dSAndroid Build Coastguard Worker 28*90c8c64dSAndroid Build Coastguard Worker public class MainActivity extends Activity { 29*90c8c64dSAndroid Build Coastguard Worker 30*90c8c64dSAndroid Build Coastguard Worker @Override onCreate(Bundle savedInstanceState)31*90c8c64dSAndroid Build Coastguard Worker protected void onCreate(Bundle savedInstanceState) { 32*90c8c64dSAndroid Build Coastguard Worker super.onCreate(savedInstanceState); 33*90c8c64dSAndroid Build Coastguard Worker setContentView(R.layout.activity_main); 34*90c8c64dSAndroid Build Coastguard Worker final Resources res = getResources(); 35*90c8c64dSAndroid Build Coastguard Worker final GridViewPager pager = (GridViewPager) findViewById(R.id.pager); 36*90c8c64dSAndroid Build Coastguard Worker pager.setOnApplyWindowInsetsListener(new OnApplyWindowInsetsListener() { 37*90c8c64dSAndroid Build Coastguard Worker @Override 38*90c8c64dSAndroid Build Coastguard Worker public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { 39*90c8c64dSAndroid Build Coastguard Worker // Adjust page margins: 40*90c8c64dSAndroid Build Coastguard Worker // A little extra horizontal spacing between pages looks a bit 41*90c8c64dSAndroid Build Coastguard Worker // less crowded on a round display. 42*90c8c64dSAndroid Build Coastguard Worker final boolean round = insets.isRound(); 43*90c8c64dSAndroid Build Coastguard Worker int rowMargin = res.getDimensionPixelOffset(R.dimen.page_row_margin); 44*90c8c64dSAndroid Build Coastguard Worker int colMargin = res.getDimensionPixelOffset(round ? 45*90c8c64dSAndroid Build Coastguard Worker R.dimen.page_column_margin_round : R.dimen.page_column_margin); 46*90c8c64dSAndroid Build Coastguard Worker pager.setPageMargins(rowMargin, colMargin); 47*90c8c64dSAndroid Build Coastguard Worker 48*90c8c64dSAndroid Build Coastguard Worker // GridViewPager relies on insets to properly handle 49*90c8c64dSAndroid Build Coastguard Worker // layout for round displays. They must be explicitly 50*90c8c64dSAndroid Build Coastguard Worker // applied since this listener has taken them over. 51*90c8c64dSAndroid Build Coastguard Worker pager.onApplyWindowInsets(insets); 52*90c8c64dSAndroid Build Coastguard Worker return insets; 53*90c8c64dSAndroid Build Coastguard Worker } 54*90c8c64dSAndroid Build Coastguard Worker }); 55*90c8c64dSAndroid Build Coastguard Worker pager.setAdapter(new SampleGridPagerAdapter(this, getFragmentManager())); 56*90c8c64dSAndroid Build Coastguard Worker DotsPageIndicator dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator); 57*90c8c64dSAndroid Build Coastguard Worker dotsPageIndicator.setPager(pager); 58*90c8c64dSAndroid Build Coastguard Worker } 59*90c8c64dSAndroid Build Coastguard Worker } 60