1431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov/*
2431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * Copyright (C) 2016 The Android Open Source Project
3431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov *
4431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * you may not use this file except in compliance with the License.
6431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * You may obtain a copy of the License at
7431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov *
8431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov *
10431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * See the License for the specific language governing permissions and
14431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * limitations under the License.
15431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov */
16431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
17ac5fe7c617c66850fff75a9fce9979c6e5674b0fAurimas Liutikaspackage androidx.appcompat.app;
18431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
19431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikovimport android.view.View;
20431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikovimport android.widget.AdapterView;
21431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikovimport android.widget.ArrayAdapter;
22431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikovimport android.widget.ListView;
23431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikovimport android.widget.TextView;
24431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
253de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.test.R;
263de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.testutils.BaseTestActivity;
273de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.testutils.Shakespeare;
283de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.appcompat.widget.Toolbar;
293de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.core.view.GravityCompat;
303de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikasimport androidx.drawerlayout.widget.DrawerLayout;
313de8a4e8305507475d7890205184946a25cf45e7Aurimas Liutikas
32431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov/**
33431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * Test activity for testing various APIs and interactions for DrawerLayout with start and end
34431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov * drawers.
35431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov */
36431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikovpublic class DrawerLayoutDoubleActivity extends BaseTestActivity {
37431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    private DrawerLayout mDrawerLayout;
38431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    private ListView mStartDrawer;
39431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    private View mEndDrawer;
40431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    private TextView mContent;
41431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
42431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    private Toolbar mToolbar;
43431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
44431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    @Override
45431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    protected int getContentViewLayoutResId() {
46431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        return R.layout.drawer_double_layout;
47431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    }
48431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
49431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    @Override
50431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    protected void onContentViewSet() {
51431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        super.onContentViewSet();
52431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
53fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mDrawerLayout = findViewById(R.id.drawer_layout);
54fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mStartDrawer = findViewById(R.id.start_drawer);
55431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mEndDrawer = findViewById(R.id.end_drawer);
56fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mContent = findViewById(R.id.content_text);
57431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
58431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
59431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
60431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // The drawer title must be set in order to announce state changes when
61431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // accessibility is turned on. This is typically a simple description,
62431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // e.g. "Navigation".
63431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mDrawerLayout.setDrawerTitle(GravityCompat.START, getString(R.string.drawer_title));
64431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
65431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mStartDrawer.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
66431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov                Shakespeare.TITLES));
67431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mStartDrawer.setOnItemClickListener(new DrawerItemClickListener());
68431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
69431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // Find the toolbar in our layout and set it as the support action bar on the activity.
70431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // This is required to have the drawer slide "over" the toolbar.
71fa2e2acf79d791a90410025daad438968550d18cAlan Viverette        mToolbar = findViewById(R.id.toolbar);
72431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mToolbar.setTitle(R.string.drawer_title);
73431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        setSupportActionBar(mToolbar);
74431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
75431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
76431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        getSupportActionBar().setDisplayShowHomeEnabled(false);
77431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
78431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // Configure the background color fill of the system status bar (on supported platform
79431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // versions) and the toolbar itself. We're using the same color, and android:statusBar
80431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // from the theme makes the status bar slightly darker.
81431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        final int metalBlueColor = getResources().getColor(R.color.drawer_sample_metal_blue);
82431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mDrawerLayout.setStatusBarBackgroundColor(metalBlueColor);
83431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        mToolbar.setBackgroundColor(metalBlueColor);
84431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    }
85431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
86431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    @Override
87431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    public void onBackPressed() {
88431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // Is the start drawer open?
89431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        if (mDrawerLayout.isDrawerOpen(mStartDrawer)) {
90431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            // Close the drawer and return.
91431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            mDrawerLayout.closeDrawer(mStartDrawer);
92431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            return;
93431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        }
94431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
95431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        // Is the end drawer open?
96431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        if (mDrawerLayout.isDrawerOpen(mEndDrawer)) {
97431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            // Close the drawer and return.
98431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            mDrawerLayout.closeDrawer(mEndDrawer);
99431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            return;
100431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        }
101431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
102431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        super.onBackPressed();
103431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    }
104431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov
105431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    /**
106431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov     * This list item click listener implements very simple view switching by changing
107431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov     * the primary content text. The drawer is closed when a selection is made.
108431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov     */
109431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    private class DrawerItemClickListener implements ListView.OnItemClickListener {
110431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        @Override
111431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
112431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            mContent.setText(Shakespeare.DIALOGUE[position]);
113431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            mToolbar.setTitle(Shakespeare.TITLES[position]);
114431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov            mDrawerLayout.closeDrawer(mStartDrawer);
115431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov        }
116431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov    }
117431713be0958fd0f4d878f5f25755575191fb8baKirill Grouchnikov}
118