1 /* 2 * Copyright (C) 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 17 package android.support.v7.app; 18 19 import android.content.res.Configuration; 20 import android.os.Bundle; 21 import android.support.v4.view.GravityCompat; 22 import android.support.v4.widget.DrawerLayout; 23 import android.support.v7.appcompat.test.R; 24 import android.support.v7.testutils.BaseTestActivity; 25 import android.support.v7.testutils.Shakespeare; 26 import android.support.v7.widget.Toolbar; 27 import android.view.MenuItem; 28 import android.view.View; 29 import android.widget.AdapterView; 30 import android.widget.ArrayAdapter; 31 import android.widget.ListView; 32 import android.widget.TextView; 33 34 /** 35 * Test activity for testing various APIs and interactions for DrawerLayout with start and end 36 * drawers. 37 */ 38 public class DrawerLayoutDoubleActivity extends BaseTestActivity { 39 private DrawerLayout mDrawerLayout; 40 private ListView mStartDrawer; 41 private View mEndDrawer; 42 private TextView mContent; 43 44 private Toolbar mToolbar; 45 46 @Override getContentViewLayoutResId()47 protected int getContentViewLayoutResId() { 48 return R.layout.drawer_double_layout; 49 } 50 51 @Override onContentViewSet()52 protected void onContentViewSet() { 53 super.onContentViewSet(); 54 55 mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 56 mStartDrawer = (ListView) findViewById(R.id.start_drawer); 57 mEndDrawer = findViewById(R.id.end_drawer); 58 mContent = (TextView) findViewById(R.id.content_text); 59 60 mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 61 62 // The drawer title must be set in order to announce state changes when 63 // accessibility is turned on. This is typically a simple description, 64 // e.g. "Navigation". 65 mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.drawer_title)); 66 67 mStartDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 68 Shakespeare.TITLES)); 69 mStartDrawer.setOnItemClickListener(new DrawerItemClickListener()); 70 71 // Find the toolbar in our layout and set it as the support action bar on the activity. 72 // This is required to have the drawer slide "over" the toolbar. 73 mToolbar = (Toolbar) findViewById(R.id.toolbar); 74 mToolbar.setTitle(R.string.drawer_title); 75 setSupportActionBar(mToolbar); 76 77 getSupportActionBar().setDisplayHomeAsUpEnabled(true); 78 getSupportActionBar().setDisplayShowHomeEnabled(false); 79 80 // Configure the background color fill of the system status bar (on supported platform 81 // versions) and the toolbar itself. We're using the same color, and android:statusBar 82 // from the theme makes the status bar slightly darker. 83 final int metalBlueColor = getResources().getColor(R.color.drawer_sample_metal_blue); 84 mDrawerLayout.setStatusBarBackgroundColor(metalBlueColor); 85 mToolbar.setBackgroundColor(metalBlueColor); 86 } 87 88 @Override onBackPressed()89 public void onBackPressed() { 90 // Is the start drawer open? 91 if (mDrawerLayout.isDrawerOpen(mStartDrawer)) { 92 // Close the drawer and return. 93 mDrawerLayout.closeDrawer(mStartDrawer); 94 return; 95 } 96 97 // Is the end drawer open? 98 if (mDrawerLayout.isDrawerOpen(mEndDrawer)) { 99 // Close the drawer and return. 100 mDrawerLayout.closeDrawer(mEndDrawer); 101 return; 102 } 103 104 super.onBackPressed(); 105 } 106 107 /** 108 * This list item click listener implements very simple view switching by changing 109 * the primary content text. The drawer is closed when a selection is made. 110 */ 111 private class DrawerItemClickListener implements ListView.OnItemClickListener { 112 @Override onItemClick(AdapterView<?> parent, View view, int position, long id)113 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 114 mContent.setText(Shakespeare.DIALOGUE[position]); 115 mToolbar.setTitle(Shakespeare.TITLES[position]); 116 mDrawerLayout.closeDrawer(mStartDrawer); 117 } 118 } 119 } 120