Using Tabbed Navigation Bar in Android — A simple tutorial for iOS developers

The Android does provided a bottom navigation bar natively, however, I found it is a bit difficult to use. After google around, I found a better solution: BottomBar (https://github.com/roughike/BottomBar).

The final look would be like:

Step 0x00: Add dependencies
 Add the following dependency to your build.gradle file.

compile 'com.android.support:design:26+'
compile 'com.roughike:bottom-bar:2.3.1'

Step 0x01: Add bottom navigation bar layout file

Create a new folder named xml -> right click -> create xml resources -> clean the project:

The file content:

<?xml version="1.0" encoding="utf-8"?>
<tabs>
<tab
id="@+id/tab_search"
icon="@drawable/ic_favorite_border_black_24dp"
title="Favorites" />
<tab
id="@+id/tab_history"
icon="@drawable/ic_favorite_border_black_24dp"
title="Nearby" />
<tab
id="@+id/tab_about"
icon="@drawable/ic_favorite_border_black_24dp"
title="Friends" />
</tabs>

Step 0x02: Create Fragment layouts file and corresponding Java class:

For each of the fragment layout, it would be better to create a seperate layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="Search"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

Here, you need to create serveral tabs xml layout resources, such as:

fragment_1.xml
 fragment_2.xml
 fragment_3.xml

package test.apeapp.ben.mynbn;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by ben on 27/9/17.
*/

public class FragmentHome extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_1, container, false);
}
}

Step 0x03: Add Fragment in the activity_main.xml

In the main layout, load the fragment view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<!-- This could be your fragment container, or something -->
<FrameLayout
android:id="@+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomBar" />

<com.roughike.bottombar.BottomBar
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/bottombar_tabs" />

</RelativeLayout>

In the mainActivity, load the tab:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {

Fragment fragment = null;
switch (tabId){
case R.id.tab_search:
fragment = new FragmentSearch();
break;
case R.id.tab_history:
fragment = new FragmentHistory();
break;
case R.id.tab_about:
fragment = new FragmentAbout();
break;
default:
fragment = new FragmentSearch();
break;
}

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment, fragment);
fragmentTransaction.commit();
}
});

bottomBar.setOnTabReselectListener(new OnTabReselectListener() {
@Override
public void onTabReSelected(@IdRes int tabId) {
Toast.makeText(MainActivity.this, "Re selected", Toast.LENGTH_SHORT).show();
//Toast.makeText(getApplicationContext(), TabMessage.get(tabId, true), Toast.LENGTH_LONG).show();
}
});
}

Reference
 http://www.arkilis.me/?p=823

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.