1 /* 2 * Copyright (C) 2015 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.support.design.widget; 18 19 import com.example.android.support.design.R; 20 21 import android.os.Bundle; 22 import android.support.design.widget.NavigationView; 23 import android.support.v4.view.GravityCompat; 24 import android.support.v4.widget.DrawerLayout; 25 import android.support.v7.app.AppCompatActivity; 26 import android.support.v7.widget.Toolbar; 27 import android.util.TypedValue; 28 import android.view.MenuItem; 29 import android.view.View; 30 import android.widget.TextView; 31 import android.widget.Toast; 32 33 /** 34 * This demonstrates basic usage of NavigationView 35 */ 36 public class NavigationViewUsage extends AppCompatActivity { 37 38 private DrawerLayout mDrawerLayout; 39 40 private TextView mTextMessage; 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.design_navigation); 46 47 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 48 mTextMessage = (TextView) findViewById(R.id.message); 49 50 // Set the color of status bar 51 TypedValue value = new TypedValue(); 52 getTheme().resolveAttribute(R.attr.colorPrimaryDark, value, true); 53 mDrawerLayout.setStatusBarBackgroundColor(value.data); 54 55 // Retrieve the Toolbar from our content view, and set it as the action bar 56 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 57 setSupportActionBar(toolbar); 58 59 // Toggle icon 60 toolbar.setNavigationIcon(R.drawable.ic_action_navigation_menu); 61 toolbar.setNavigationOnClickListener(new View.OnClickListener() { 62 @Override 63 public void onClick(View view) { 64 mDrawerLayout.openDrawer(GravityCompat.START); 65 } 66 }); 67 68 // Menu 69 NavigationView navigation = (NavigationView) findViewById(R.id.navigation); 70 navigation.setNavigationItemSelectedListener(mNavigationItemSelectedListener); 71 navigation.inflateHeaderView(R.layout.design_navigation_header); 72 } 73 74 private NavigationView.OnNavigationItemSelectedListener mNavigationItemSelectedListener 75 = new NavigationView.OnNavigationItemSelectedListener() { 76 @Override 77 public boolean onNavigationItemSelected(MenuItem item) { 78 if (handleNavigationItemSelected(item)) { 79 mDrawerLayout.closeDrawers(); 80 return true; 81 } 82 return false; 83 } 84 }; 85 handleNavigationItemSelected(MenuItem item)86 private boolean handleNavigationItemSelected(MenuItem item) { 87 switch (item.getItemId()) { 88 case R.id.navigation_item_1: 89 mTextMessage.setText("1"); 90 return true; 91 case R.id.navigation_item_2: 92 mTextMessage.setText("2"); 93 return true; 94 case R.id.navigation_item_3: 95 mTextMessage.setText("3"); 96 return true; 97 case R.id.navigation_sub_item_1: 98 showToast(R.string.navigation_sub_item_1); 99 return true; 100 case R.id.navigation_sub_item_2: 101 showToast(R.string.navigation_sub_item_2); 102 return true; 103 case R.id.navigation_with_icon: 104 showToast(R.string.navigation_item_with_icon); 105 return true; 106 case R.id.navigation_without_icon: 107 showToast(R.string.navigation_item_without_icon); 108 return true; 109 default: 110 return false; 111 } 112 } 113 showToast(int res)114 private void showToast(int res) { 115 Toast.makeText(this, getString(R.string.navigation_message, getString(res)), 116 Toast.LENGTH_SHORT).show(); 117 } 118 119 @Override onBackPressed()120 public void onBackPressed() { 121 if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { 122 mDrawerLayout.closeDrawer(GravityCompat.START); 123 } else { 124 super.onBackPressed(); 125 } 126 } 127 128 } 129