1e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov/*
2e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project
3e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
4e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * you may not use this file except in compliance with the License.
6e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * You may obtain a copy of the License at
7e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
8e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
10e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * See the License for the specific language governing permissions and
14e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * limitations under the License.
15e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov */
16e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
17e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovpackage com.example.android.supportv7.widget;
18e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
19e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.content.res.Configuration;
2049b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikovimport android.content.res.Resources;
2149b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikovimport android.content.res.TypedArray;
22e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.os.Bundle;
23e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.view.MenuItem;
24e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.view.View;
2549b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikovimport android.view.ViewTreeObserver;
26e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.widget.AdapterView;
27e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.widget.ArrayAdapter;
2826b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikovimport android.widget.FrameLayout;
29e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.widget.ListView;
30e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport android.widget.TextView;
31def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas
32def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.appcompat.app.ActionBarDrawerToggle;
33def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.appcompat.app.AppCompatActivity;
34def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.appcompat.widget.Toolbar;
35def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.content.ContextCompat;
36def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.core.view.GravityCompat;
37def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.drawerlayout.widget.DrawerLayout;
38def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas
39e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport com.example.android.supportv7.R;
40e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovimport com.example.android.supportv7.Shakespeare;
41e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
42e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov/**
43e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * This example illustrates a common usage of the DrawerLayout widget combined with Toolbar
44e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * in the Android support library that respect the
45e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * <a href="https://www.google.com/design/spec/patterns/navigation-drawer.html">Material design
46e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * guidelines</a> for the drawer component.
47e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
48e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
49e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * <p>A DrawerLayout should be positioned at the top of your view hierarchy, placing it
50e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * below the action bar but above your content views. The primary content should match_parent
51e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * in both dimensions. Each drawer should define a reasonable width and match_parent for height.
52e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * Drawer views should be positioned after the content view in your layout to preserve proper
53e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * ordering.</p>
54e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
55e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * <p>When a navigation (left) drawer is present, the host activity should detect presses of
56e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * the action bar's Up affordance as a signal to open and close the navigation drawer.
57e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * Items within the drawer should fall into one of two categories.</p>
58e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
59e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * <ul>
60e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     <li><strong>View switches</strong>. A view switch follows the same basic policies as
61e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     list or tab navigation in that a view switch does not create navigation history.
62e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     This pattern should only be used at the root activity of a task, leaving some form
63e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     of Up navigation active for activities further down the navigation hierarchy.</li>
64e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     <li><strong>Selective Up</strong>. The drawer allows the user to choose an alternate
65e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     parent for Up navigation. This allows a user to jump across an app's navigation
66e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     hierarchy at will. The application should treat this as it treats Up navigation from
67e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     a different task, replacing the current task stack using TaskStackBuilder or similar.
68e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     This is the only form of navigation drawer that should be used outside of the root
69e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *     activity of a task.</li>
70e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * </ul>
71e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
72e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * <p>Right side drawers should be used for actions, not navigation. This follows the pattern
73e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * established by the Action Bar that navigation should be to the left and actions to the right.
74e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * An action should be an operation performed on the current contents of the window,
75e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * for example enabling or disabling a data overlay on top of the current content.</p>
76e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov *
77e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * <p>When the drawer is open, it is above the application toolbar. On Lollipop versions of the
78e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * platform and above the drawer spans the full height of the screen, including behind the system
79e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov * status bar.</p>
80e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov */
81e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikovpublic class DrawerLayoutActivity extends AppCompatActivity {
82e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    private DrawerLayout mDrawerLayout;
8326b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov    private ListView mStartDrawer;
8426b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov    private FrameLayout mEndDrawer;
85e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    private TextView mContent;
86e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
87e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    private ActionBarDrawerToggle mDrawerToggle;
88e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    private Toolbar mToolbar;
89e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
90e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    @Override
91e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    protected void onCreate(Bundle savedInstanceState) {
92e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        super.onCreate(savedInstanceState);
93e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
94e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        setContentView(R.layout.drawer_layout);
95e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
96fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mDrawerLayout = findViewById(R.id.drawer_layout);
97fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mStartDrawer = findViewById(R.id.start_drawer);
98fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mEndDrawer = findViewById(R.id.end_drawer);
99fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mContent = findViewById(R.id.content_text);
100e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
101e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
10226b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow_end, GravityCompat.END);
103e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
104e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // The drawer title must be set in order to announce state changes when
105e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // accessibility is turned on. This is typically a simple description,
106e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // e.g. "Navigation".
107e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.drawer_title));
108e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
10926b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        mStartDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
110e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov                Shakespeare.TITLES));
11126b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        mStartDrawer.setOnItemClickListener(new DrawerItemClickListener());
112e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
113e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // Find the toolbar in our layout and set it as the support action bar on the activity.
114e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // This is required to have the drawer slide "over" the toolbar.
115fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mToolbar = findViewById(R.id.toolbar);
116e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mToolbar.setTitle(R.string.drawer_title);
117e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        setSupportActionBar(mToolbar);
118e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
119e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
120e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        getSupportActionBar().setDisplayShowHomeEnabled(false);
121e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
122e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // ActionBarDrawerToggle provides convenient helpers for tying together the
123e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // prescribed interactions between a top-level sliding drawer and the action bar.
124e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // Note that, as the Javadocs of ActionBarDrawerToggle constructors say, we are
125e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // *not* using a constructor that gets a Toolbar since we're setting our toolbar
126e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // dynamically at runtime. Furthermore, as the drawer is sliding over the toolbar,
127e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // we are suppressing the morphing animation from hamburger to back arrow by
128e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // calling super.onDrawerSlide with slideOffset=0.0f. In case your app only has
129e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // top-level pages and doesn't need back arrow visuals at all, you can set up
130e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // your activity theme to have attribute named "drawerArrowStyle" that points
131e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // to an extension of Widget.AppCompat.DrawerArrowToggle that has its "spinBars"
132e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // attribute set to false.
133e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
134e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov                R.string.drawer_open, R.string.drawer_close) {
135e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            @Override
136e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            public void onDrawerOpened(View drawerView) {
137e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov                super.onDrawerOpened(drawerView);
138e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov                super.onDrawerSlide(drawerView, 0.0f);
139e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            }
140e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
141e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            @Override
142e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            public void onDrawerSlide(View drawerView, float slideOffset) {
143e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov                super.onDrawerSlide(drawerView, 0.0f);
144e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            }
145e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        };
146e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
1470984f98baad154a687823303666d437161ef3bffKirill Grouchnikov        mDrawerLayout.addDrawerListener(mDrawerToggle);
148e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
149e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // Configure the background color fill of the system status bar (on supported platform
150e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // versions) and the toolbar itself. We're using the same color, and android:statusBar
151e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // from the theme makes the status bar slightly darker.
152e8e0eae2600001c8b1e9d5a9f0331cf045193746Aurimas Liutikas        final int metalBlueColor = ContextCompat.getColor(this, R.color.drawer_sample_metal_blue);
153e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mDrawerLayout.setStatusBarBackgroundColor(metalBlueColor);
154e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mToolbar.setBackgroundColor(metalBlueColor);
15549b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov
15649b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov        // Register a pre-draw listener to get the initial width of the DrawerLayout so
15749b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov        // that we can determine the width of the drawer based on the Material spec at
15849b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov        // https://www.google.com/design/spec/patterns/navigation-drawer.html#navigation-drawer-specs
15949b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov        mDrawerLayout.getViewTreeObserver().addOnPreDrawListener(
16049b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                new ViewTreeObserver.OnPreDrawListener() {
16149b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                    @Override
16249b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                    public boolean onPreDraw() {
16349b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        // What is the width of the entire DrawerLayout?
16449b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final int drawerLayoutWidth = mDrawerLayout.getWidth();
16549b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov
16649b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        // What is the action bar size?
16749b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final Resources.Theme theme = mDrawerLayout.getContext().getTheme();
16849b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final TypedArray a = theme.obtainStyledAttributes(
169ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikas                                new int[] { androidx.appcompat.R.attr.actionBarSize });
17049b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final int actionBarSize = a.getDimensionPixelSize(0, 0);
17149b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        if (a != null) {
17249b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                            a.recycle();
17349b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        }
17449b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov
17549b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        // Compute the width of the drawer and set it on the layout params.
17649b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final int idealDrawerWidth = 5 * actionBarSize;
17749b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final int maxDrawerWidth = Math.max(0, drawerLayoutWidth - actionBarSize);
17849b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        final int drawerWidth = Math.min(idealDrawerWidth, maxDrawerWidth);
17949b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov
18026b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                        final DrawerLayout.LayoutParams startDrawerLp =
18126b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                                (DrawerLayout.LayoutParams) mStartDrawer.getLayoutParams();
18226b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                        startDrawerLp.width = drawerWidth;
18326b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                        mStartDrawer.setLayoutParams(startDrawerLp);
18426b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov
18526b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                        final DrawerLayout.LayoutParams endDrawerLp =
18626b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                                (DrawerLayout.LayoutParams) mEndDrawer.getLayoutParams();
18726b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                        endDrawerLp.width = drawerWidth;
18826b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov                        mEndDrawer.setLayoutParams(endDrawerLp);
18949b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov
19049b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        // Remove ourselves as the pre-draw listener since this is a one-time
19149b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        // configuration.
19249b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        mDrawerLayout.getViewTreeObserver().removeOnPreDrawListener(this);
19349b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                        return true;
19449b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov                    }
19549b20509b42c7e9abf941fdce55599c57ac689cfKirill Grouchnikov        });
196e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    }
197e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
198e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    @Override
199e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    protected void onPostCreate(Bundle savedInstanceState) {
200e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        super.onPostCreate(savedInstanceState);
201e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
202e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        // Sync the toggle state after onRestoreInstanceState has occurred.
203e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mDrawerToggle.syncState();
204e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    }
205e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
206e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    @Override
207e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    public boolean onOptionsItemSelected(MenuItem item) {
208e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        /*
209e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov         * The action bar home/up action should open or close the drawer.
210e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov         * The drawer toggle will take care of this.
211e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov         */
212e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        if (mDrawerToggle.onOptionsItemSelected(item)) {
213e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            return true;
214e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        }
215e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        return super.onOptionsItemSelected(item);
216e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    }
217e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
218e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    @Override
219e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    public void onBackPressed() {
22026b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        boolean hadOpenDrawer = false;
22126b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        // Is the start drawer open?
22226b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        if (mDrawerLayout.isDrawerOpen(mStartDrawer)) {
22326b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            // Close it
22426b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            mDrawerLayout.closeDrawer(mStartDrawer);
22526b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            hadOpenDrawer = true;
22626b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        }
22726b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        // Is the end drawer open?
22826b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        if (mDrawerLayout.isDrawerOpen(mEndDrawer)) {
22926b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            // Close it
23026b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            mDrawerLayout.closeDrawer(mEndDrawer);
23126b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            hadOpenDrawer = true;
23226b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        }
23326b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov
23426b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov        if (hadOpenDrawer) {
23526b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            // If we had one or both drawers open, now that we've closed it / them, return.
236e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            return;
237e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        }
238e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
239e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        super.onBackPressed();
240e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    }
241e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
242e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    @Override
243e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    public void onConfigurationChanged(Configuration newConfig) {
244e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        super.onConfigurationChanged(newConfig);
245e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        mDrawerToggle.onConfigurationChanged(newConfig);
246e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    }
247e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov
248e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    /**
249e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov     * This list item click listener implements very simple view switching by changing
250e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov     * the primary content text. The drawer is closed when a selection is made.
251e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov     */
252e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    private class DrawerItemClickListener implements ListView.OnItemClickListener {
253e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        @Override
254e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
255e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            mContent.setText(Shakespeare.DIALOGUE[position]);
256e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov            mToolbar.setTitle(Shakespeare.TITLES[position]);
25726b4a0416012ed538a0f4e0dc69ba6bd19d247beKirill Grouchnikov            mDrawerLayout.closeDrawer(mStartDrawer);
258e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov        }
259e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov    }
260e963911344d98d1a79e3e8dcea16ea04371f2af6Kirill Grouchnikov}
261