/* * Copyright (C) 2015 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.support.design.widget; import com.example.android.support.design.R; import android.os.Bundle; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.widget.Toolbar; import android.util.TypedValue; import android.view.MenuItem; import android.view.View; /** * This demonstrates basic usage of NavigationView */ public class NavigationViewUsage extends NavigationViewUsageBase { private DrawerLayout mDrawerLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // Set the color of status bar TypedValue value = new TypedValue(); getTheme().resolveAttribute(R.attr.colorPrimaryDark, value, true); mDrawerLayout.setStatusBarBackgroundColor(value.data); // Retrieve the Toolbar from our content view, and set it as the action bar Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // Toggle icon toolbar.setNavigationIcon(R.drawable.ic_action_navigation_menu); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mDrawerLayout.openDrawer(GravityCompat.START); } }); } @Override protected int getLayout() { return R.layout.design_navigation; } @Override public NavigationView.OnNavigationItemSelectedListener getNavigationItemSelectedListener() { return new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(MenuItem item) { if (handleNavigationItemSelected(item)) { mDrawerLayout.closeDrawers(); return true; } return false; } }; } @Override public void onBackPressed() { if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) { mDrawerLayout.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } }