close
The Wayback Machine - https://web.archive.org/web/20160924051336/https://developer.android.com/samples/WearDrawers/src/com.example.android.wearable.wear.weardrawers/MainActivity.html
Skip to content

Most visited

Recently visited

navigation
WearDrawers / src / com.example.android.wearable.wear.weardrawers /

MainActivity.java

1
/*
2
Copyright 2016 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
package com.example.android.wearable.wear.weardrawers;
17
 
18
import android.app.Fragment;
19
import android.app.FragmentManager;
20
import android.content.Context;
21
import android.graphics.drawable.Drawable;
22
import android.os.Bundle;
23
import android.support.wearable.activity.WearableActivity;
24
import android.support.wearable.view.drawer.WearableActionDrawer;
25
import android.support.wearable.view.drawer.WearableDrawerLayout;
26
import android.support.wearable.view.drawer.WearableNavigationDrawer;
27
import android.util.Log;
28
import android.view.Gravity;
29
import android.view.LayoutInflater;
30
import android.view.Menu;
31
import android.view.MenuInflater;
32
import android.view.MenuItem;
33
import android.view.View;
34
import android.view.ViewGroup;
35
import android.widget.ImageView;
36
import android.widget.Toast;
37
 
38
import java.util.ArrayList;
39
 
40
/**
41
 * Demonstrates use of Navigation and Action Drawers on Android Wear.
42
 */
43
public class MainActivity extends WearableActivity implements
44
        WearableActionDrawer.OnMenuItemClickListener {
45
 
46
    private static final String TAG = "MainActivity";
47
 
48
    private WearableDrawerLayout mWearableDrawerLayout;
49
    private WearableNavigationDrawer mWearableNavigationDrawer;
50
    private WearableActionDrawer mWearableActionDrawer;
51
 
52
    private ArrayList<Planet> mSolarSystem;
53
    private int mSelectedPlanet;
54
 
55
    private PlanetFragment mPlanetFragment;
56
 
57
    @Override
58
    protected void onCreate(Bundle savedInstanceState) {
59
        super.onCreate(savedInstanceState);
60
        Log.d(TAG, "onCreate()");
61
 
62
        setContentView(R.layout.activity_main);
63
        setAmbientEnabled();
64
 
65
        mSolarSystem = initializeSolarSystem();
66
        mSelectedPlanet = 0;
67
 
68
        // Initialize content to first planet.
69
        mPlanetFragment = new PlanetFragment();
70
        Bundle args = new Bundle();
71
 
72
        int imageId = getResources().getIdentifier(mSolarSystem.get(mSelectedPlanet).getImage(),
73
                "drawable", getPackageName());
74
 
75
 
76
        args.putInt(PlanetFragment.ARG_PLANET_IMAGE_ID, imageId);
77
        mPlanetFragment.setArguments(args);
78
        FragmentManager fragmentManager = getFragmentManager();
79
        fragmentManager.beginTransaction().replace(R.id.content_frame, mPlanetFragment).commit();
80
 
81
        // Main Wearable Drawer Layout that wraps all content
82
        mWearableDrawerLayout = (WearableDrawerLayout) findViewById(R.id.drawer_layout);
83
 
84
        // Top Navigation Drawer
85
        mWearableNavigationDrawer =
86
                (WearableNavigationDrawer) findViewById(R.id.top_navigation_drawer);
87
        mWearableNavigationDrawer.setAdapter(new NavigationAdapter(this));
88
 
89
        // Peeks Navigation drawer on the top.
90
        mWearableDrawerLayout.peekDrawer(Gravity.TOP);
91
 
92
        // Bottom Action Drawer
93
        mWearableActionDrawer =
94
                (WearableActionDrawer) findViewById(R.id.bottom_action_drawer);
95
 
96
        mWearableActionDrawer.setOnMenuItemClickListener(this);
97
 
98
        // Peeks action drawer on the bottom.
99
        mWearableDrawerLayout.peekDrawer(Gravity.BOTTOM);
100
 
101
        /* Action Drawer Tip: If you only have a single action for your Action Drawer, you can use a
102
         * (custom) View to peek on top of the content by calling
103
         * mWearableActionDrawer.setPeekContent(View). Make sure you set a click listener to handle
104
         * a user clicking on your View.
105
         */
106
    }
107
 
108
    private ArrayList<Planet> initializeSolarSystem() {
109
        ArrayList<Planet> solarSystem = new ArrayList<Planet>();
110
        String[] planetArrayNames = getResources().getStringArray(R.array.planets_array_names);
111
 
112
        for (int i = 0; i < planetArrayNames.length; i++) {
113
            String planet = planetArrayNames[i];
114
            int planetResourceId =
115
                    getResources().getIdentifier(planet, "array", getPackageName());
116
            String[] planetInformation = getResources().getStringArray(planetResourceId);
117
 
118
            solarSystem.add(new Planet(
119
                    planetInformation[0],   // Name
120
                    planetInformation[1],   // Navigation icon
121
                    planetInformation[2],   // Image icon
122
                    planetInformation[3],   // Moons
123
                    planetInformation[4],   // Volume
124
                    planetInformation[5])); // Surface area
125
        }
126
 
127
        return solarSystem;
128
    }
129
 
130
    @Override
131
    public boolean onMenuItemClick(MenuItem menuItem) {
132
        Log.d(TAG, "onMenuItemClick(): " + menuItem);
133
 
134
        final int itemId = menuItem.getItemId();
135
 
136
        String toastMessage = "";
137
 
138
        switch (itemId) {
139
            case R.id.menu_planet_name:
140
                toastMessage = mSolarSystem.get(mSelectedPlanet).getName();
141
                break;
142
            case R.id.menu_number_of_moons:
143
                toastMessage = mSolarSystem.get(mSelectedPlanet).getMoons();
144
                break;
145
            case R.id.menu_volume:
146
                toastMessage = mSolarSystem.get(mSelectedPlanet).getVolume();
147
                break;
148
            case R.id.menu_surface_area:
149
                toastMessage = mSolarSystem.get(mSelectedPlanet).getSurfaceArea();
150
                break;
151
        }
152
 
153
        mWearableDrawerLayout.closeDrawer(mWearableActionDrawer);
154
 
155
        if (toastMessage.length() > 0) {
156
            Toast toast = Toast.makeText(
157
                    getApplicationContext(),
158
                    toastMessage,
159
                    Toast.LENGTH_SHORT);
160
            toast.show();
161
            return true;
162
        } else {
163
            return false;
164
        }
165
    }
166
 
167
    private final class NavigationAdapter
168
            extends WearableNavigationDrawer.WearableNavigationDrawerAdapter {
169
 
170
        private final Context mContext;
171
 
172
        public NavigationAdapter(Context context) {
173
            mContext = context;
174
        }
175
 
176
        @Override
177
        public int getCount() {
178
            return mSolarSystem.size();
179
        }
180
 
181
        @Override
182
        public void onItemSelected(int position) {
183
            Log.d(TAG, "WearableNavigationDrawerAdapter.onItemSelected(): " + position);
184
            mSelectedPlanet = position;
185
 
186
            String selectedPlanetImage = mSolarSystem.get(mSelectedPlanet).getImage();
187
            int drawableId =
188
                    getResources().getIdentifier(selectedPlanetImage, "drawable", getPackageName());
189
            mPlanetFragment.updatePlanet(drawableId);
190
        }
191
 
192
        @Override
193
        public String getItemText(int pos) {
194
            return mSolarSystem.get(pos).getName();
195
        }
196
 
197
        @Override
198
        public Drawable getItemDrawable(int pos) {
199
            String navigationIcon = mSolarSystem.get(pos).getNavigationIcon();
200
 
201
            int drawableNavigationIconId =
202
                    getResources().getIdentifier(navigationIcon, "drawable", getPackageName());
203
 
204
            return mContext.getDrawable(drawableNavigationIconId);
205
        }
206
    }
207
 
208
    /**
209
     * Fragment that appears in the "content_frame", just shows the currently selected planet.
210
     */
211
    public static class PlanetFragment extends Fragment {
212
        public static final String ARG_PLANET_IMAGE_ID = "planet_image_id";
213
 
214
        private ImageView mImageView;
215
 
216
        public PlanetFragment() {
217
            // Empty constructor required for fragment subclasses
218
        }
219
 
220
        @Override
221
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
222
                                 Bundle savedInstanceState) {
223
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
224
 
225
            mImageView = ((ImageView) rootView.findViewById(R.id.image));
226
 
227
            int imageIdToLoad = getArguments().getInt(ARG_PLANET_IMAGE_ID);
228
            mImageView.setImageResource(imageIdToLoad);
229
 
230
            return rootView;
231
        }
232
 
233
        public void updatePlanet(int imageId) {
234
            mImageView.setImageResource(imageId);
235
        }
236
    }
237
}
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

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.