Skip to main content

Bottom Navigation (Android Material Design)

Android Material Design

Material design is a design language developed by Google in 2014. The idea was to have a unified front for all design aspects.

Bottom Navigation

Bottom navigation bars are situated as name suggests at the bottom of your view. It enables you to select or to say explore top-level views on single tap. The requirement should be clear in your head, if you have two main views to show, its better you don't use Bottom Navigation View, instead go for Tabs. There can be minimum three to maximum five top-levels destinations. In case, you add sixth element it will throw an exception at run-time.



Bottom Navigation view

A simple example to create Bottom Navigation View.
  1. Create a Project in Android Studio.
  2. Select type of Activity you want to create. Here we will select "Bottom Navigation Activity".   
    Select your Activity
  3. Customizing the view accordingly now, will require we add are own icons and text. To change the designated icons from the bottom navigation view,you need to change the "navigation.xml" file from /menu folder. My changes looks like this.                                                                                          
 <?xml version="1.0" encoding="utf-8"?>  
 <menu xmlns:android="http://schemas.android.com/apk/res/android">  
   <item  
     android:id="@+id/navigation_home"  
     android:icon="@mipmap/ic_home"  
     android:title="@string/title_home" />  
   <item  
     android:id="@+id/navigation_offer"  
     android:icon="@mipmap/ic_offers"  
     android:title="@string/offers" />  
   <item  
     android:id="@+id/navigation_ticket"  
     android:icon="@mipmap/ic_ticket"  
     android:title="@string/tickets" />  
 </menu>  

   4. OnNavigationItemSelected(MenuItem item) is overridden for OnNavigationItemSelectedListener , for each cases you can define your action to be implemented.


 public class MainActivity extends AppCompatActivity {  
   private TextView mTextMessage; // text to show on screen  
   /**  
    * This listener is used to detect item selected when any tab is pressed.  
    */  
   private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener  
       = new BottomNavigationView.OnNavigationItemSelectedListener() {  
     @Override  
     public boolean onNavigationItemSelected(@NonNull MenuItem item) {  
       switch (item.getItemId()) {  
         case R.id.navigation_home:  
           mTextMessage.setText(R.string.title_home); //change text on screen  
           return true;  
         case R.id.navigation_offer:  
           mTextMessage.setText(R.string.offers);//change text on screen  
           return true;  
         case R.id.navigation_ticket:  
           mTextMessage.setText(R.string.tickets);//change text on screen  
           return true;  
       }  
       return false;  
     }  
   };  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     mTextMessage = (TextView) findViewById(R.id.message);  
     BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);  
     navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);  
   }  
 }  


Full source code

Comments

Popular posts from this blog

Bottom Sheet (Modal)

Bottom Sheet (Modal) Modal bottom sheet are more like a dialog in appearance.One must have seen a deeplink dialog which pops when you want to share stuff, something like that, except here he manipulate and populate the contents on that dialog.  The fact that they appear on top of our other UI components, in order to resume interaction to page we need to dismiss or close the bottom sheet. When the modal bottom sheet slides up, rest of the page appears dim giving focus to the bottom sheet. Modal Bottom Sheet requires BottomSheetDialogFragment, which is a thin layer on top of regular support library fragment that renders your fragment as Bottom Sheet. The development of this bottom sheet is different than persistent bottom sheet.  public class ModalSheetDialog extends BottomSheetDialogFragment { String mString; static ModalSheetDialog newInstance(String string) { ModalSheetDialog f = new ModalSheetDialog(); Bundle args =...

Material Design - Bottom Sheet

Bottom Sheet (Persistent) Bottom sheets slides up from the bottom of the screen to reveal content. However, depending on the display of sheets they are categorized into two. Persistent Android Bottom Sheet Modal Android Bottom Sheet Persistent Android Bottom Sheet Persistent bottom sheet are mainly used to display in-app details. Suppose there is an additional content which is useful to the user, you need to show in it. Scenario, in which you are persistent to show content related to your app you will use this type of design.  Persistent Bottom Sheet Simple example of Persistent Bottom Sheet.  For this you require to create a project in Android Studio. Add the following line under dependencies in build.gradle file of /app folder. implementation 'com.android.support:design:27.0.2' The above line will enable you to have all the classes that are required for Bottom Sheet implementation Now, in your main layout file define the layout cont...