Tab Bottom Navigation in android application
Tab
Bottom
Navigation
android
application
- By Code solution
- Jan 20th, 2021
- 0 comments
- 1
Tab Bottom Navigation in an android studio.
Creating New Project
- Create a new project in Android Studio from File ⇒ New Project. When selecting Empty Activity and proceed.
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.+' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
- Open String.xml located under res ⇒ values and add the below values.
<resources> <string name="app_name">Tab Navigationstring> <string name="title_shop">Shopstring> <string name="title_gifts">Giftsstring> <string name="title_cart">Cartstring> <string name="title_profile">Profilestring> resources>
- Open activity_main.xml under res ⇒ layouts.
xml version="1.0" encoding="utf-8"?> <android.support.design.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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" app:itemBackground="@color/bgBottomNavigation" android:foreground="?attr/selectableItemBackground" app:itemIconTint="@android:color/white" app:itemTextColor="@android:color/white" app:menu="@menu/navigation" /> android.support.design.widget.CoordinatorLayout>
- Create fragment_store.xml under res ⇒ layouts.
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1f5f7" tools:context=".fragment.StoreFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="10dp" android:text="Home" android:textColor="#111" android:textSize="16dp" /> LinearLayout> android.support.v4.widget.NestedScrollView>
- Create same other fragment pages under res ⇒ layouts.
- After Open MainActivity.java and modify the code as below.
package com.wapptech.tabnavigation; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CoordinatorLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import com.wapptech.tabnavigation.fragment.CartFragment; import com.wapptech.tabnavigation.fragment.GiftsFragment; import com.wapptech.tabnavigation.fragment.ProfileFragment; import com.wapptech.tabnavigation.fragment.StoreFragment; import com.wapptech.tabnavigation.helper.BottomNavigationBehavior; public class MainActivity extends AppCompatActivity { private ActionBar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = getSupportActionBar(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); // attaching bottom sheet behaviour - hide / show on scroll CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) navigation.getLayoutParams(); layoutParams.setBehavior(new BottomNavigationBehavior()); // load the store fragment by default toolbar.setTitle("Shop"); loadFragment(new StoreFragment()); } private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Fragment fragment; switch (item.getItemId()) { case R.id.navigation_shop: toolbar.setTitle("Home"); fragment = new StoreFragment(); loadFragment(fragment); return true; case R.id.navigation_gifts: toolbar.setTitle("My Gifts"); fragment = new GiftsFragment(); loadFragment(fragment); return true; case R.id.navigation_cart: toolbar.setTitle("Cart"); fragment = new CartFragment(); loadFragment(fragment); return true; case R.id.navigation_profile: toolbar.setTitle("Profile"); fragment = new ProfileFragment(); loadFragment(fragment); return true; } return false; } }; private void loadFragment(Fragment fragment) { // load fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame_container, fragment); transaction.addToBackStack(null); transaction.commit(); } }
- After Create BottomNavigationBehavior.java under -> helper
??
package com.wapptech.tabnavigation.helper; import android.content.Context; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CoordinatorLayout; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; public class BottomNavigationBehavior extends CoordinatorLayout.Behavior { public BottomNavigationBehavior() { super(); } public BottomNavigationBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) { boolean dependsOn = dependency instanceof FrameLayout; return dependsOn; } @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) { return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; } @Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) { if (dy < 0) { showBottomNavigationView(child); } else if (dy > 0) { hideBottomNavigationView(child); } } private void hideBottomNavigationView(BottomNavigationView view) { view.animate().translationY(view.getHeight()); } private void showBottomNavigationView(BottomNavigationView view) { view.animate().translationY(0); } }
- After Finally open AndroidManifest.XML and make MainActivity as launcher activity.
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wapptech.tabnavigation"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>
Github Link: https://github.com/Sudarshan101/TabNavigation
">
dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.+' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
- Open String.xml located under res ⇒ values and add the below values.
<resources> <string name="app_name">Tab Navigationstring> <string name="title_shop">Shopstring> <string name="title_gifts">Giftsstring> <string name="title_cart">Cartstring> <string name="title_profile">Profilestring> resources>
- Open activity_main.xml under res ⇒ layouts.
xml version="1.0" encoding="utf-8"?> <android.support.design.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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" app:itemBackground="@color/bgBottomNavigation" android:foreground="?attr/selectableItemBackground" app:itemIconTint="@android:color/white" app:itemTextColor="@android:color/white" app:menu="@menu/navigation" /> android.support.design.widget.CoordinatorLayout>
- Create fragment_store.xml under res ⇒ layouts.
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1f5f7" tools:context=".fragment.StoreFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="10dp" android:text="Home" android:textColor="#111" android:textSize="16dp" /> LinearLayout> android.support.v4.widget.NestedScrollView>
- Create same other fragment pages under res ⇒ layouts.
- After Open MainActivity.java and modify the code as below.
package com.wapptech.tabnavigation; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CoordinatorLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import com.wapptech.tabnavigation.fragment.CartFragment; import com.wapptech.tabnavigation.fragment.GiftsFragment; import com.wapptech.tabnavigation.fragment.ProfileFragment; import com.wapptech.tabnavigation.fragment.StoreFragment; import com.wapptech.tabnavigation.helper.BottomNavigationBehavior; public class MainActivity extends AppCompatActivity { private ActionBar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = getSupportActionBar(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); // attaching bottom sheet behaviour - hide / show on scroll CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) navigation.getLayoutParams(); layoutParams.setBehavior(new BottomNavigationBehavior()); // load the store fragment by default toolbar.setTitle("Shop"); loadFragment(new StoreFragment()); } private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Fragment fragment; switch (item.getItemId()) { case R.id.navigation_shop: toolbar.setTitle("Home"); fragment = new StoreFragment(); loadFragment(fragment); return true; case R.id.navigation_gifts: toolbar.setTitle("My Gifts"); fragment = new GiftsFragment(); loadFragment(fragment); return true; case R.id.navigation_cart: toolbar.setTitle("Cart"); fragment = new CartFragment(); loadFragment(fragment); return true; case R.id.navigation_profile: toolbar.setTitle("Profile"); fragment = new ProfileFragment(); loadFragment(fragment); return true; } return false; } }; private void loadFragment(Fragment fragment) { // load fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame_container, fragment); transaction.addToBackStack(null); transaction.commit(); } }
- After Create BottomNavigationBehavior.java under -> helper
??
package com.wapptech.tabnavigation.helper; import android.content.Context; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CoordinatorLayout; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; public class BottomNavigationBehavior extends CoordinatorLayout.Behavior { public BottomNavigationBehavior() { super(); } public BottomNavigationBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) { boolean dependsOn = dependency instanceof FrameLayout; return dependsOn; } @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) { return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; } @Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) { if (dy < 0) { showBottomNavigationView(child); } else if (dy > 0) { hideBottomNavigationView(child); } } private void hideBottomNavigationView(BottomNavigationView view) { view.animate().translationY(view.getHeight()); } private void showBottomNavigationView(BottomNavigationView view) { view.animate().translationY(0); } }
- After Finally open AndroidManifest.XML and make MainActivity as launcher activity.
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wapptech.tabnavigation"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>
Github Link: https://github.com/Sudarshan101/TabNavigation
Tab Bottom Navigation in an android studio.
Creating New Project
- Create a new project in Android Studio from File ⇒ New Project. When selecting Empty Activity and proceed.
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support:design:27.+' implementation 'com.android.support.constraint:constraint-layout:1.1.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
- Open String.xml located under res ⇒ values and add the below values.
<resources> <string name="app_name">Tab Navigationstring> <string name="title_shop">Shopstring> <string name="title_gifts">Giftsstring> <string name="title_cart">Cartstring> <string name="title_profile">Profilestring> resources>
- Open activity_main.xml under res ⇒ layouts.
xml version="1.0" encoding="utf-8"?> <android.support.design.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:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <FrameLayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.BottomNavigationView android:id="@+id/navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" app:itemBackground="@color/bgBottomNavigation" android:foreground="?attr/selectableItemBackground" app:itemIconTint="@android:color/white" app:itemTextColor="@android:color/white" app:menu="@menu/navigation" /> android.support.design.widget.CoordinatorLayout>
- Create fragment_store.xml under res ⇒ layouts.
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f1f5f7" tools:context=".fragment.StoreFragment"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingTop="10dp" android:text="Home" android:textColor="#111" android:textSize="16dp" /> LinearLayout> android.support.v4.widget.NestedScrollView>
- Create same other fragment pages under res ⇒ layouts.
- After Open MainActivity.java and modify the code as below.
package com.wapptech.tabnavigation; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CoordinatorLayout; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import com.wapptech.tabnavigation.fragment.CartFragment; import com.wapptech.tabnavigation.fragment.GiftsFragment; import com.wapptech.tabnavigation.fragment.ProfileFragment; import com.wapptech.tabnavigation.fragment.StoreFragment; import com.wapptech.tabnavigation.helper.BottomNavigationBehavior; public class MainActivity extends AppCompatActivity { private ActionBar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); toolbar = getSupportActionBar(); BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); // attaching bottom sheet behaviour - hide / show on scroll CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) navigation.getLayoutParams(); layoutParams.setBehavior(new BottomNavigationBehavior()); // load the store fragment by default toolbar.setTitle("Shop"); loadFragment(new StoreFragment()); } private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Fragment fragment; switch (item.getItemId()) { case R.id.navigation_shop: toolbar.setTitle("Home"); fragment = new StoreFragment(); loadFragment(fragment); return true; case R.id.navigation_gifts: toolbar.setTitle("My Gifts"); fragment = new GiftsFragment(); loadFragment(fragment); return true; case R.id.navigation_cart: toolbar.setTitle("Cart"); fragment = new CartFragment(); loadFragment(fragment); return true; case R.id.navigation_profile: toolbar.setTitle("Profile"); fragment = new ProfileFragment(); loadFragment(fragment); return true; } return false; } }; private void loadFragment(Fragment fragment) { // load fragment FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame_container, fragment); transaction.addToBackStack(null); transaction.commit(); } }
- After Create BottomNavigationBehavior.java under -> helper
??
package com.wapptech.tabnavigation.helper; import android.content.Context; import android.support.design.widget.BottomNavigationView; import android.support.design.widget.CoordinatorLayout; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.view.View; import android.widget.FrameLayout; public class BottomNavigationBehavior extends CoordinatorLayout.Behavior { public BottomNavigationBehavior() { super(); } public BottomNavigationBehavior(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean layoutDependsOn(CoordinatorLayout parent, BottomNavigationView child, View dependency) { boolean dependsOn = dependency instanceof FrameLayout; return dependsOn; } @Override public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) { return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; } @Override public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dx, int dy, int[] consumed) { if (dy < 0) { showBottomNavigationView(child); } else if (dy > 0) { hideBottomNavigationView(child); } } private void hideBottomNavigationView(BottomNavigationView view) { view.animate().translationY(view.getHeight()); } private void showBottomNavigationView(BottomNavigationView view) { view.animate().translationY(0); } }
- After Finally open AndroidManifest.XML and make MainActivity as launcher activity.
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wapptech.tabnavigation"> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>