Create Bottom Navigation View in Android Studio

1. Add library spacebarlayout:
'eu.long1:spacetablayout:1.0.4'
reference : https://github.com/long1eu/SpaceTabLayout

Video Tutorial : https://youtu.be/FFVsSpbWyi4

2. Add this code in activity_main.xml

 <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:app="http://schemas.android.com/apk/res-auto"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:id="@+id/activity_main"  
   tools:context=".MainActivity">  
   <androidx.viewpager.widget.ViewPager  
     android:id="@+id/viewpager"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_marginBottom="56dp"/>  
   <eu.long1.spacetablayout.SpaceTabLayout  
     android:id="@+id/spacetablayout"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     app:layout_behavior="eu.long1.spacetablayout.SpaceTabLayoutBehavior"  
     app:number_of_tabs="three"  
     app:starting_position="one"  
     app:tab_color="@android:color/holo_green_dark"  
     app:text_color="#FFFFFF"  
     app:icon_one="@drawable/ic_menu_black_24dp"  
     app:icon_two="@drawable/ic_book_black_24dp"  
     app:icon_three="@drawable/ic_person_black_24dp"  
     app:text_one="Menu"  
     app:text_two="Book"  
     app:text_three="User"/>  
 </androidx.coordinatorlayout.widget.CoordinatorLayout>  

3. Add 3 Fragment(blank)

4. MainActivity.java

 package com.project.ichwan.reseller;  
 import androidx.appcompat.app.AppCompatActivity;  
 import androidx.coordinatorlayout.widget.CoordinatorLayout;  
 import androidx.fragment.app.Fragment;  
 import androidx.viewpager.widget.ViewPager;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Space;  
 import android.widget.Toast;  
 import com.google.android.material.snackbar.Snackbar;  
 import java.util.ArrayList;  
 import java.util.List;  
 import eu.long1.spacetablayout.SpaceTabLayout;  
 public class MainActivity extends AppCompatActivity {  
   SpaceTabLayout spaceTabLayout;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     ViewPager viewPager = findViewById(R.id.viewpager);  
     final CoordinatorLayout coordinatorLayout = findViewById(R.id.activity_main);  
     spaceTabLayout = findViewById(R.id.spacetablayout);  
     List<Fragment> fragmentList = new ArrayList<>();  
     fragmentList.add(new OneFragment());  
     fragmentList.add(new TwoFragment());  
     fragmentList.add(new ThreeFragment());  
     spaceTabLayout.initialize(viewPager, getSupportFragmentManager(), fragmentList, savedInstanceState);  
     spaceTabLayout.setTabOneOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Snackbar snackbar = Snackbar.make(coordinatorLayout, "Laptopku Kentang, jadi gk bisa nampilin emulator :(",  
             Snackbar.LENGTH_SHORT);  
       }  
     });  
     spaceTabLayout.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View view) {  
         Toast.makeText(getApplication(),"Welcome"+spaceTabLayout.getCurrentPosition(), Toast.LENGTH_SHORT).show();  
       }  
     });  
   }  
 }  

Post a Comment

0 Comments