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