How to Implement Bubble Navigation Bar in Android Studio | Android Studio Tutorials

How to Implement Bubble Navigation Bar in Android Studio | Android Studio Tutorials

 



In this tutorial we will learn how to implement this beautiful bottom bar in Android Studio.
We'll use gauravk95/bubble-navigation library for our implementation.

  • So the first step is to implement dependencies in build.gradle.
dependencies {
    implementation 'com.gauravk.bubblenavigation:bubblenavigation:1.0.7'
}

  • Next Step is to make our fragments, I have 3 options : Home, Feed, Category. So, I have created 3 fragments for them in my MainActivity.java and added it to my fragment manager.
  • We'll play with show, hide in this whole activity.
package com.example.tutorialspecial;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity {
    Context context;
    FragmentManager fragmentManager;
    Fragment1 home = new Fragment1();
    Fragment2 feed = new Fragment2();
    Fragment3 category = new Fragment3();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        bubbleNavigation = findViewById(R.id.bottom_navigation_view_linear);
        fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .add(R.id.frame_ly,home)
                .add(R.id.frame_ly,feed)
                .add(R.id.frame_ly,category)
                .show(home)
                .hide(feed)
                .hide(category)
                .commit();
    }

}

  • With the above step we'll be able to add our all fragment to our fragment manager. So now we have to add the bottom bar layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:id="@+id/frame_ly"
        android:layout_height="match_parent"/>

    <com.gauravk.bubblenavigation.BubbleNavigationLinearView
        android:id="@+id/bottom_navigation_view_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/design_default_color_primary"
        android:layout_alignParentBottom="true"
        android:elevation="8dp"
        android:orientation="horizontal"
        android:padding="10dp">

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/l_item_feed"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:bt_colorActive="@color/black"
            android:layout_weight="1"
            app:bt_colorInactive="#CCC7C7"
            app:bt_shapeColor="@color/white"
            android:layout_marginLeft="20dp"
            app:bt_icon="@drawable/feed_ic"
            app:bt_title="Feed"
            app:bt_padding="5dp"/>

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/l_item_home"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:bt_active="true"
            app:bt_colorActive="@color/black"
            app:bt_colorInactive="#CCC7C7"
            android:layout_marginRight="30dp"
            android:layout_weight="1"
            app:bt_shapeColor="@color/white"
            app:bt_icon="@drawable/home_ic"
            app:bt_title="Home"
            android:layout_marginLeft="30dp"
            app:bt_padding="5dp"/>

        <com.gauravk.bubblenavigation.BubbleToggleView
            android:id="@+id/l_item_cat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:bt_colorActive="@color/black"
            app:bt_colorInactive="#CCC7C7"
            android:layout_weight="1"
            app:bt_shapeColor="@color/white"
            app:bt_icon="@drawable/category_ic"
            app:bt_title="Category"
            app:bt_padding="5dp"
            android:layout_marginRight="20dp"/>


    </com.gauravk.bubblenavigation.BubbleNavigationLinearView>

</RelativeLayout>

  • Here I have 3 child in bottom bar as per our requirement. You guys can add according to your requirements. 
  • We can set color, item_icon, item_background according to our needs.
  • Now, we have to implement our selection change listener.
package com.example.tutorialspecial;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.gauravk.bubblenavigation.BubbleNavigationLinearView;
import com.gauravk.bubblenavigation.listener.BubbleNavigationChangeListener;

public class MainActivity extends AppCompatActivity {
    Context context;
    FragmentManager fragmentManager;
    Fragment1 home = new Fragment1();
    Fragment2 feed = new Fragment2();
    Fragment3 category = new Fragment3();
    BubbleNavigationLinearView bubbleNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        bubbleNavigation = findViewById(R.id.bottom_navigation_view_linear);
        fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .add(R.id.frame_ly,home)
                .add(R.id.frame_ly,feed)
                .add(R.id.frame_ly,category)
                .show(home)
                .hide(feed)
                .hide(category)
                .commit();

        // Bottom bar

        bubbleNavigation.setNavigationChangeListener(new BubbleNavigationChangeListener() {
            @Override
            public void onNavigationChanged(View view, int position) {
                
            }
        });
    }

}

  • We'll get current selected fragment by the integer position so we'll use a switch case to do the desired selection.
package com.example.tutorialspecial;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.gauravk.bubblenavigation.BubbleNavigationLinearView;
import com.gauravk.bubblenavigation.listener.BubbleNavigationChangeListener;

public class MainActivity extends AppCompatActivity {
    Context context;
    FragmentManager fragmentManager;
    Fragment1 home = new Fragment1();
    Fragment2 feed = new Fragment2();
    Fragment3 category = new Fragment3();
    BubbleNavigationLinearView bubbleNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        bubbleNavigation = findViewById(R.id.bottom_navigation_view_linear);
        fragmentManager = getSupportFragmentManager();
        fragmentManager
                .beginTransaction()
                .add(R.id.frame_ly,home)
                .add(R.id.frame_ly,feed)
                .add(R.id.frame_ly,category)
                .show(home)
                .hide(feed)
                .hide(category)
                .commit();

        // Bottom bar

        bubbleNavigation.setNavigationChangeListener(new BubbleNavigationChangeListener() {
            @Override
            public void onNavigationChanged(View view, int position) {
                switch (position){
                    case 0 : fragmentManager
                            .beginTransaction()
                            .show(feed)
                            .hide(home)
                            .hide(category)
                            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                            .commit();
                    break;
                    case 1 : fragmentManager
                            .beginTransaction()
                            .show(home)
                            .hide(feed)
                            .hide(category)
                            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                            .commit();
                    break;
                    case 2 : fragmentManager
                            .beginTransaction()
                            .show(category)
                            .hide(home)
                            .hide(feed)
                            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                            .commit();
                    break;
                }
            }
        });
    }

}

  • When we set "Home" to show then the fragment manager will make Home fragment visible and hide will make other 2 fragment invisible.
  • Feel free to ask doubts in comment section

Post a Comment

0 Comments