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 com.example.android.support.design.widget;
18 
19 import android.content.res.ColorStateList;
20 import android.os.Bundle;
21 import android.view.MenuItem;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.TextView;
25 
26 import androidx.annotation.NonNull;
27 import androidx.appcompat.app.AppCompatActivity;
28 
29 import com.example.android.support.design.R;
30 import com.google.android.material.bottomnavigation.BottomNavigationView;
31 
32 /**
33  * This demonstrates idiomatic usage of the bottom navigation widget.
34  */
35 public class BottomNavigationViewUsage extends AppCompatActivity {
36     private ColorStateList mOriginalTint;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.design_bottom_navigation_view);
42         Button buttonDisable = findViewById(R.id.button_disable);
43         final BottomNavigationView bottom =
44                 findViewById(R.id.bottom_navigation);
45         mOriginalTint = bottom.getItemIconTintList();
46         buttonDisable.setOnClickListener(new View.OnClickListener() {
47             @Override
48             public void onClick(View view) {
49                 bottom.getMenu().getItem(0).setEnabled(!bottom.getMenu().getItem(0).isEnabled());
50             }
51         });
52         Button buttonAdd = findViewById(R.id.button_add);
53         buttonAdd.setOnClickListener(new View.OnClickListener() {
54             @Override
55             public void onClick(View view) {
56                 if (bottom.getMenu().size() < bottom.getMaxItemCount()) {
57                     MenuItem item = bottom.getMenu().add("Bananas");
58                     item.setIcon(android.R.drawable.ic_lock_power_off);
59                 }
60             }
61         });
62         Button buttonRemove = findViewById(R.id.button_remove);
63         buttonRemove.setOnClickListener(new View.OnClickListener() {
64             @Override
65             public void onClick(View view) {
66                 if (bottom.getMenu().size() > 0) {
67                     bottom.getMenu().removeItem(bottom.getMenu().getItem(0).getItemId());
68                 }
69             }
70         });
71         Button buttonTint = findViewById(R.id.button_tint);
72         buttonTint.setOnClickListener(new View.OnClickListener() {
73             @Override
74             public void onClick(View view) {
75                 if (bottom.getItemIconTintList() == null) {
76                     bottom.setItemIconTintList(mOriginalTint);
77                 } else {
78                     bottom.setItemIconTintList(null);
79                 }
80             }
81         });
82         Button buttonNext = findViewById(R.id.button_select_next);
83         buttonNext.setOnClickListener(new View.OnClickListener() {
84             @Override
85             public void onClick(View view) {
86                 final int menuSize = bottom.getMenu().size();
87                 if (menuSize < 1) {
88                     return;
89                 }
90                 int currentlySelected = 0;
91                 for (int i = 0; i < menuSize; i++) {
92                     if (bottom.getMenu().getItem(i).isChecked()) {
93                         currentlySelected = i;
94                         break;
95                     }
96                 }
97                 int next = (currentlySelected + 1) % menuSize;
98                 bottom.setSelectedItemId(bottom.getMenu().getItem(next).getItemId());
99             }
100         });
101         final TextView selectedItem = findViewById(R.id.selected_item);
102         bottom.setOnNavigationItemSelectedListener(
103                 new BottomNavigationView.OnNavigationItemSelectedListener() {
104                     @Override
105                     public boolean onNavigationItemSelected(@NonNull MenuItem item) {
106                         switch (item.getItemId()) {
107                             case R.id.action_search:
108                                 selectedItem.setText("Entering searching mode");
109                                 break;
110                             case R.id.action_settings:
111                                 selectedItem.setText("Entering settings!?!");
112                                 break;
113                             case R.id.action_music:
114                                 selectedItem.setText("Play some music");
115                                 break;
116                             default:
117                                 selectedItem.setText("Selected " + item.getTitle());
118                         }
119                         return true;
120                     }
121                 });
122         bottom.setOnNavigationItemReselectedListener(
123                 new BottomNavigationView.OnNavigationItemReselectedListener() {
124                     @Override
125                     public void onNavigationItemReselected(@NonNull MenuItem item) {
126                         selectedItem.setText("Reselected " + item.getTitle());
127                     }
128                 });
129     }
130 }
131