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
17package com.example.android.support.design.widget;
18
19import com.example.android.support.design.R;
20
21import android.os.Bundle;
22import android.support.annotation.LayoutRes;
23import android.support.design.widget.NavigationView;
24import android.support.v7.app.AppCompatActivity;
25import android.view.MenuItem;
26import android.widget.TextView;
27import android.widget.Toast;
28
29public abstract class NavigationViewUsageBase extends AppCompatActivity {
30
31    private TextView mTextMessage;
32
33    @Override
34    protected void onCreate(Bundle savedInstanceState) {
35        super.onCreate(savedInstanceState);
36        setContentView(getLayout());
37
38        mTextMessage = findViewById(R.id.message);
39
40        // Menu
41        NavigationView navigation = findViewById(R.id.navigation);
42        navigation.setNavigationItemSelectedListener(getNavigationItemSelectedListener());
43        navigation.inflateHeaderView(R.layout.design_navigation_header);
44    }
45
46    @LayoutRes
47    protected abstract int getLayout();
48
49    protected abstract NavigationView.OnNavigationItemSelectedListener getNavigationItemSelectedListener();
50
51    protected boolean handleNavigationItemSelected(MenuItem item) {
52        switch (item.getItemId()) {
53            case R.id.navigation_item_1:
54                mTextMessage.setText("1");
55                return true;
56            case R.id.navigation_item_2:
57                mTextMessage.setText("2");
58                return true;
59            case R.id.navigation_item_3:
60                mTextMessage.setText("3");
61                return true;
62            case R.id.navigation_sub_item_1:
63                showToast(R.string.navigation_sub_item_1);
64                return true;
65            case R.id.navigation_sub_item_2:
66                showToast(R.string.navigation_sub_item_2);
67                return true;
68            case R.id.navigation_sub_item_3:
69                showToast(R.string.navigation_sub_item_3);
70                return true;
71            case R.id.navigation_with_icon:
72                showToast(R.string.navigation_item_with_icon);
73                return true;
74            case R.id.navigation_without_icon:
75                showToast(R.string.navigation_item_without_icon);
76                return true;
77            default:
78                return false;
79        }
80    }
81
82    private void showToast(int res) {
83        Toast.makeText(this, getString(R.string.navigation_message, getString(res)),
84                Toast.LENGTH_SHORT).show();
85    }
86
87}
88