Skip to content

Most visited

Recently visited

navigation
GridViewPager / src / com.example.android.wearable.gridviewpager /

MainActivity.java

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

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)