1 /* 2 * Copyright 2013 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.navigationdrawerexample; 18 19 import java.util.Locale; 20 21 import android.app.Activity; 22 import android.app.Fragment; 23 import android.app.FragmentManager; 24 import android.app.SearchManager; 25 import android.content.Intent; 26 import android.content.res.Configuration; 27 import android.os.Bundle; 28 import android.support.v4.app.ActionBarDrawerToggle; 29 import android.support.v4.view.GravityCompat; 30 import android.support.v4.widget.DrawerLayout; 31 import android.view.LayoutInflater; 32 import android.view.Menu; 33 import android.view.MenuInflater; 34 import android.view.MenuItem; 35 import android.view.View; 36 import android.view.ViewGroup; 37 import android.widget.AdapterView; 38 import android.widget.ArrayAdapter; 39 import android.widget.ImageView; 40 import android.widget.ListView; 41 import android.widget.Toast; 42 43 /** 44 * This example illustrates a common usage of the DrawerLayout widget 45 * in the Android support library. 46 * <p/> 47 * <p>When a navigation (left) drawer is present, the host activity should detect presses of 48 * the action bar's Up affordance as a signal to open and close the navigation drawer. The 49 * ActionBarDrawerToggle facilitates this behavior. 50 * Items within the drawer should fall into one of two categories:</p> 51 * <p/> 52 * <ul> 53 * <li><strong>View switches</strong>. A view switch follows the same basic policies as 54 * list or tab navigation in that a view switch does not create navigation history. 55 * This pattern should only be used at the root activity of a task, leaving some form 56 * of Up navigation active for activities further down the navigation hierarchy.</li> 57 * <li><strong>Selective Up</strong>. The drawer allows the user to choose an alternate 58 * parent for Up navigation. This allows a user to jump across an app's navigation 59 * hierarchy at will. The application should treat this as it treats Up navigation from 60 * a different task, replacing the current task stack using TaskStackBuilder or similar. 61 * This is the only form of navigation drawer that should be used outside of the root 62 * activity of a task.</li> 63 * </ul> 64 * <p/> 65 * <p>Right side drawers should be used for actions, not navigation. This follows the pattern 66 * established by the Action Bar that navigation should be to the left and actions to the right. 67 * An action should be an operation performed on the current contents of the window, 68 * for example enabling or disabling a data overlay on top of the current content.</p> 69 */ 70 public class MainActivity extends Activity { 71 private DrawerLayout mDrawerLayout; 72 private ListView mDrawerList; 73 private ActionBarDrawerToggle mDrawerToggle; 74 75 private CharSequence mDrawerTitle; 76 private CharSequence mTitle; 77 private String[] mPlanetTitles; 78 79 @Override 80 protected void onCreate(Bundle savedInstanceState) { 81 super.onCreate(savedInstanceState); 82 setContentView(R.layout.activity_main); 83 84 mTitle = mDrawerTitle = getTitle(); 85 mPlanetTitles = getResources().getStringArray(R.array.planets_array); 86 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 87 mDrawerList = (ListView) findViewById(R.id.left_drawer); 88 89 // set a custom shadow that overlays the main content when the drawer opens 90 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 91 // set up the drawer's list view with items and click listener 92 mDrawerList.setAdapter(new ArrayAdapter<String>(this, 93 R.layout.drawer_list_item, mPlanetTitles)); 94 mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 95 96 // enable ActionBar app icon to behave as action to toggle nav drawer 97 getActionBar().setDisplayHomeAsUpEnabled(true); 98 getActionBar().setHomeButtonEnabled(true); 99 100 // ActionBarDrawerToggle ties together the the proper interactions 101 // between the sliding drawer and the action bar app icon 102 mDrawerToggle = new ActionBarDrawerToggle( 103 this, /* host Activity */ 104 mDrawerLayout, /* DrawerLayout object */ 105 R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */ 106 R.string.drawer_open, /* "open drawer" description for accessibility */ 107 R.string.drawer_close /* "close drawer" description for accessibility */ 108 ) { 109 public void onDrawerClosed(View view) { 110 getActionBar().setTitle(mTitle); 111 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 112 } 113 114 public void onDrawerOpened(View drawerView) { 115 getActionBar().setTitle(mDrawerTitle); 116 invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 117 } 118 }; 119 mDrawerLayout.setDrawerListener(mDrawerToggle); 120 121 if (savedInstanceState == null) { 122 selectItem(0); 123 } 124 } 125 126 @Override 127 public boolean onCreateOptionsMenu(Menu menu) { 128 MenuInflater inflater = getMenuInflater(); 129 inflater.inflate(R.menu.main, menu); 130 return super.onCreateOptionsMenu(menu); 131 } 132 133 /* Called whenever we call invalidateOptionsMenu() */ 134 @Override 135 public boolean onPrepareOptionsMenu(Menu menu) { 136 // If the nav drawer is open, hide action items related to the content view 137 boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); 138 menu.findItem(R.id.action_websearch).setVisible(!drawerOpen); 139 return super.onPrepareOptionsMenu(menu); 140 } 141 142 @Override 143 public boolean onOptionsItemSelected(MenuItem item) { 144 // The action bar home/up action should open or close the drawer. 145 // ActionBarDrawerToggle will take care of this. 146 if (mDrawerToggle.onOptionsItemSelected(item)) { 147 return true; 148 } 149 // Handle action buttons 150 switch(item.getItemId()) { 151 case R.id.action_websearch: 152 // create intent to perform web search for this planet 153 Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); 154 intent.putExtra(SearchManager.QUERY, getActionBar().getTitle()); 155 // catch event that there's no activity to handle intent 156 if (intent.resolveActivity(getPackageManager()) != null) { 157 startActivity(intent); 158 } else { 159 Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show(); 160 } 161 return true; 162 default: 163 return super.onOptionsItemSelected(item); 164 } 165 } 166 167 /* The click listner for ListView in the navigation drawer */ 168 private class DrawerItemClickListener implements ListView.OnItemClickListener { 169 @Override 170 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 171 selectItem(position); 172 } 173 } 174 175 private void selectItem(int position) { 176 // update the main content by replacing fragments 177 Fragment fragment = new PlanetFragment(); 178 Bundle args = new Bundle(); 179 args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position); 180 fragment.setArguments(args); 181 182 FragmentManager fragmentManager = getFragmentManager(); 183 fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit(); 184 185 // update selected item and title, then close the drawer 186 mDrawerList.setItemChecked(position, true); 187 setTitle(mPlanetTitles[position]); 188 mDrawerLayout.closeDrawer(mDrawerList); 189 } 190 191 @Override 192 public void setTitle(CharSequence title) { 193 mTitle = title; 194 getActionBar().setTitle(mTitle); 195 } 196 197 /** 198 * When using the ActionBarDrawerToggle, you must call it during 199 * onPostCreate() and onConfigurationChanged()... 200 */ 201 202 @Override 203 protected void onPostCreate(Bundle savedInstanceState) { 204 super.onPostCreate(savedInstanceState); 205 // Sync the toggle state after onRestoreInstanceState has occurred. 206 mDrawerToggle.syncState(); 207 } 208 209 @Override 210 public void onConfigurationChanged(Configuration newConfig) { 211 super.onConfigurationChanged(newConfig); 212 // Pass any configuration change to the drawer toggls 213 mDrawerToggle.onConfigurationChanged(newConfig); 214 } 215 216 /** 217 * Fragment that appears in the "content_frame", shows a planet 218 */ 219 public static class PlanetFragment extends Fragment { 220 public static final String ARG_PLANET_NUMBER = "planet_number"; 221 222 public PlanetFragment() { 223 // Empty constructor required for fragment subclasses 224 } 225 226 @Override 227 public View onCreateView(LayoutInflater inflater, ViewGroup container, 228 Bundle savedInstanceState) { 229 View rootView = inflater.inflate(R.layout.fragment_planet, container, false); 230 int i = getArguments().getInt(ARG_PLANET_NUMBER); 231 String planet = getResources().getStringArray(R.array.planets_array)[i]; 232 233 int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()), 234 "drawable", getActivity().getPackageName()); 235 ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId); 236 getActivity().setTitle(planet); 237 return rootView; 238 } 239 } 240 }