Connect with us

Hi, what are you looking for?

Latest

Android Bottom Navigation Bar Example

The lower navigation bar is a hardware component that allows you to easily navigate and switch between the views at the highest level with a single click or press of a button. In this Android tutorial you will learn how to create the bottom navigation bar in Android. Do you also learn how to display menu items in the lower navigation bar? Let’s get started.

When using the lower navigation bar in your application

  • Itinerary requiring direct access
  • Three to five high-level destinations

Let’s take the example of a popular application that implements the lower navigation bar, such as Google plus android, which is used for various application screens.

Requirements

After this point we will use the BottomNavigationView hardware element to perform this task. I will also create a sample application for Android Studio on Java to quickly download your project from the bottom navigation bar.  In order to follow this manual, you must…

  • Android Studio 3.0 or higher
  • Plugin Kotlin 1.1.51 or higher

Navigation panel – Example Appendix

Input level of the lower navigation scale

  • Create an Android Studio project
  • Add a bottom view of the navigation
  •  Creates menu items for the lower navigation bar
  • Initialisation of the components
  • Click on Organize events
  • Fragmentation
  • Pulling the fragments

1. Creating an Android Studioproject

Let’s open Android Studio and create a new project (enter the desired name, I’ll use the lower navigation view here) with an empty activity template. Make sure you create the project using the Androidx support library.

2. Addition of lower navigation control

To start using BottomNavigationView in your project, you will need to add the io material dependencies to the app/build.gradle file.

Implementation of com.google.android.material:material:1.1.0-alpha10.

Implementation of com.google.android.material:material:1.1.0-alpha10.

Implementation of com.google.android.material:material:1.1.0-alpha10.

Now open the file res/layout/activlty_main.xmlto add the BottomNavigationView widget. You need to add a FrameLayout to this layout file. It will serve as a container for various fragments.

<?xml Version=1.0 Encryption=utf-8? >

<androidx.constraintlayout.widget.ConstraintLayout 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

android: layout_width=match_parent

android: layout_height=match_parent

android: [email protected]+id/container

<com.google.android.material.bottomnavigation.BottomNavigationView.com

android: [email protected]+id/bottom_navigation

android: layout_width=match_parent

android: layout_height=wrap_content

android: layout_gravity=bottom

android: [email protected]/colorPrimary

app:[email protected]/white

app:[email protected]/white

app:[email protected]/low_navigation_menu

</androidx.constraintlayout.widget.ConstraintLayout>

<?xml version=1.0 encoding=utf-8? > <androidx.constraintLayout.widget.ConstraintLayout 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_width=match_parent android:layout_height=match_parent tools:context=.MainActivity > <FrameLayout android:layout_width=match_parent android:layout_height=match_parent android:[email protected]+id/container > <!– Main Contents –> <com.google.android.material.bottomnavigation.BottomNavigationView android:[email protected]+id/bottom_navigation android:layout_width=match_parent android:layout_height=wrap_content android:layout_gravity=bottom android :[email protected]/colorPrimary app:[email protected]/white app:[email protected]/white app:[email protected]/bottom_navigation_menu /> </FrameLayout> </androidx.LimitationLayout.widget.LimitationLayout>.

<?xml version=1.0 encoding=utf-8?><androidx.constraintLayout.widget.ConstraintLayout xmlns:android=http://schemas.android.com/apk/res/androidxmlns:app=http://schemas.android.com/apk/res-autoxmlns:tools=http://schemas.android.com/toolsandroid:layout_width=match_parentandroid:layout_width=match_parentandroid:layout_height=match_parenttools:context=.MainActivity><FrameLayoutandroid:layout_width=match_parentandroid:layout_height=match_parentandroid:[email protected]+id/container>.

<! — Main content —

<com.google.android.material.bottomnavigation.BottomNavigationView
android:[email protected]+id/bottom_navigation
android:layout_width=match_parent
android:layout_height=wrap_content
android :layout_gravity=bottom
android:[email protected]/colorPrimary
app:[email protected]/white
app:[email protected]/white
app:[email protected]/bottom_navigation_menu
/>

</FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

3. Creates menu items for the lower navigation bar

To activate the menu items in the lower navigation bar, create a menu file inres/menu/ bottom_navigation_menu.

<?xml Version=1.0 Encryption=utf-8? >

<menu xmlns:android=http://schemas.android.com/apk/res/android>

android:[email protected]+id/navigation_home

android:[email protected]/ic_home

android:[email protected]+id/navigation_sms

android:[email protected]/ic_sms

android: [email protected]+id/navigation_notifications

android: [email protected]/ic_notifications

android: title=Notification />

<?xml version=1.0 encoding=utf-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android> <item android:[email protected]+id/navigation_home android:[email protected]/ic_home android:title=Home /> <item android:[email protected]+id/navigation_sms android :[email protected]/ic_sms android:title=SMS /> <item android:[email protected]+id/navigation_notifications android:[email protected]/ic_notifications android:title=Notification /> </menu>

<?xml version=1.0 encoding=utf-8? >
<menu xmlns:android=http://schemas.android.com/apk/res/android>
<item
android:[email protected]+id/navigation_home
android:[email protected]/ic_home
android:title=Home />

<Input
android: [email protected]+id/navigation_sms
android:[email protected]/ic_sms
android:title=SMS />

<pot
android:[email protected]+id/notification navigation
android:[email protected]/ic_notifications
android:title=notification />
</menu>.

In this menu file we use the menu that serves as a container for the menu items. Created the Menu Item, which is a single menu item. As you can see, each of them has a head, a symbol and an identification.

4. Initialization of components

Next, we initialize the BottomNavigationView instance. Initialization takes place as part of the onCreate method activity.

Soil NavigationViewing the Soil Navigation ;

protected hole onCreate(Bundle storedInstanceState) {

create super.onCreate(savedInstanceState) ;

setContentView(R.layout.activity_main) ;

bottomNavigation = findViewById(R.id.bottom_navigation) ;

BottomNavigationView bottomNavigation; @Scribe secure empty space toCreate(Bundle savedInstanceState); setContentView(R.layout.activity_main); bottomNavigation = findViewById(R.id.bottom_navigation); }

Soil NavigationViewing the Soil Navigation ;

@
Overwrite secure blank onCreate(Bundle StoredInstanceState) {
super.onCreate(storedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = findViewById(R.id.bottom_navigation) ;

}

5. Click on event setting

BottomNavigationView.OnNavigationItemSelectedListener NavigationItemSelectedListener =

new BottomNavigationView.OnNavigationItemSelectedListener() {

@All publicly available Boolean elements in NavigationSelected(@Non-zero menu item entry) {

Exchange (item.getItemId()) {

the R.id.navigation_home case:

openFragment(HomeFragment.newInstance(, )) ;

Case of R.id.navigation_sms :

openFragment(SmsFragment.newInstance(, )) ;

Case of R.id navigation messages :

openFragment (NotificationFragment.newInstance(, )) ;

BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @All public boolevards onNavigationItemSelected(@NonNull MenuItem item))). {Switch (item.getItemId()) Case R.id.navigation_home: openFragment(HomeFragment.newInstance(, )); Return true; Case R.id.navigation_sms: openFragment(SmsFragment.newInstance(, )); Return true; Case R.id.navigation_notifications: openFragment(NotificationFragment.newInstance(, ); Return true; } Return false; } ;

BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@All publicly available Boolean onNavigationItemSelected(@NonNull MenuItem item)). {
switch (item.getItemId()). {
registry R.id.navigation_home:
openFragment(HomeFragment.newInstance(, ));
return true;
case R.id.navigation_sms:
openFragment(SmsFragment).newInstance(, ));
return true;
case R.id.navigation_notifications:
openFragment(NotificationFragment.newInstance(, ));
return true;
}
return false;
}
} ;}.

6. Fragmentation formation

Home pageFragment.java

Package com.bottomnavigationview.fragments ;

Import the android.os package;

androidx.fracgment.app Importfragment;

import android.view.layoutInflater ;

import android.view

Import android.view.ViewGroup ;

Import com.bottomnavigationview.R

* Simple subclass {@link Fragment}.

* Use the factory method {@link HomeFragment#newInstance} to

* create an example of this fragment.

the HomeFragment public class expands Fragment {

// TODO : Rename the parameter arguments, select the names that

// Fragmentation initialization parameters, e.g. ARG_ITEM_NUMBER

Private static end line ARG_PARAM1 = Parameter 1 ;

Private static end line ARG_PARAM2 = Parameter2 ;

// TODO : Renaming and changing parameter types

// Requires a public builder in case of vacancy

* Use this factory method to create a new instance

* this fragment using the specified parameters.

* @parameter Parameter1 Parameter 1.

* @parameter Parameter2 Parameter 2.

* @ Return a new copy of HomeFragment

// TODO : Rename and change the species and number of parameters

public static HomeFragment newInstance (String param1, String param2) {

HomeFragment = new HomeFragment() ;

Bundleargs = new bundle() ;

args.put string(ARG_PARAM1, param1) ;

args.put string(ARG_PARAM2, param2) ;

fisgment.setargumenten(args) ;

public void onCreate(Bundle storedInstanceState) {

create super.onCreate(savedInstanceState) ;

if (getArguments() != null) {

mParam1 = getArguments().getString(ARG_PARAM1) ;

mParam2 = getArguments().getString(ARG_PARAM2) ;

Public view onCreateView(LayoutInflater inflater, ViewGroup container,

The package was kept. State party) {

// blow up the layout of this fragment.

return inflater.inflate(R.layout.fragment_home, container, wrong) ;

Package com.bottomnavigationview.fragments; Import android.os.bundle; Import androidx.fragment.app.fragment; Import android.view.LayoutInflater; Import android.view.view; Import android.viewGroup; Import com.bottomnavigation View.R; /** * simple subclass {@link Fragment}. * Use the factory method {@link HomeFragment#newInstance} to * create an instance of this fragment */ the public class of HomeFragment expands the fragment { // TODO : Rename parameter arguments, select names that match // fragment initialization parameters, e.g. ARG_ITEM_NUMBER closed static end line ARG_PARAM1 = param1; closed static end line ARG_PARAM2 = param2; // TODO : Rename and modify the following types of parameters: private String mParam1; private String mParam2; public HomeFragment() { // Requires an empty public constructor } /** * Use this factory method to create a new instance * of this fragment using the supplied parameters. * @param parameter1 Parameter 1 * @param parameter2 Parameter 2 * @return New instance of the HomeFragment. */ // TODO : Rename and change the types and number of parameters of the public static HomeFragment newInstance(String param1, String param2) { Fragment HomeFragment fragment = new HomeFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArgumenten(args); return fragment; }. Cancel the public blank onCreate(bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); }. } @CreationView public view user interface (LayoutInflater inflater, container ViewGroup, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } }.

Package com.bottomnavigationview.fragments ;

Import android.os.bundle; import
androidx.app.fragment; import
android.view.LayoutInflater; import
android.view.view; import
android.view.ViewGroup ;

Import com.bottomnavigationview.R

/**
* Simple subclass {@Link Fragment}.
* use factory method {@link HomeFragment#newInstance} at
* create an instance of this fragment
*/
public class HomeFragment expands the fragment {
// TODO : Rename the parameter arguments, select the names that match the initialization parameters of fragments
//, e.g. ARG_ITEM_NUMBER
Private static end line ARG_PARAM1 = param1 ;
Private static end line ARG_PARAM2 = param2 ;

// TODO : Parameter types
private channel mParam1 ;
private channel mParam2 ;

public HomeFragment() {
// Empty public builder required
}

/**
* Use this factory method to create a new
* copy of this fragment with the specified parameters.
*
* @param param1 parameter 1.
* @param param2 parameter 2.
* @return New instance of the HomeFragment.
*/
// TODO : Rename and change the types and number of parameters
static public HomeFragment newInstance (String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArgumenten(args);
returnable fragment;
}

@Override of
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != zero) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Configure public view
to create the view (LayoutInflater inflater, Container ViewGroup,
Bundle savedInstanceState) {
// Layout setup for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
}

fragment_house.xml

<?xml Version=1.0 Encryption=utf-8? >

<FrameLayout 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

tools:context=.fragments.HomeFragmentation

android:[email protected]+id/picture display

android: layout_broad=200dp

android: layout_height=200dp

android: layout_gravity=center

android:[email protected]/ic_home

<?xml version=1.0 encoding=utf-8? > <FrameLayout 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 tools:context=.snippets.HomeFragment > <imageView android:[email protected]+id/imageView android:layout_width=200dp android:layout_height=200dp android:layout_gravity= android:[email protected]/ic_home /> </FrameLayout>.

<?xml version=1.0 encoding=utf-8?>
<FrameLayout xmlns:android=http://schemas.android.com/apk/res/android
xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_older
android:layout_height=match_older
tools:context=.fragments.homeFragment
>.

<View
android:[email protected]+id/imageView
android:layout_width=200dp
android:layout_height=200dp
android:layout_gravity=center
android:[email protected]/ic_home
/>
</FrameLayout>.

MessageFragment.java

Package com.bottomnavigationview.fragments ;

Import the android.os package;

androidx.fracgment.app Importfragment;

import android.view.layoutInflater ;

import android.view

Import android.view.ViewGroup ;

Import com.bottomnavigationview.R

* Simple subclass {@link Fragment}.

* Use the factory method {@link NotificationFragment#newInstance} to

* create an example of this fragment.

the NotificationFragment public class extends the fragment {

// TODO : Rename the parameter arguments, select the names that

// Fragmentation initialization parameters, e.g. ARG_ITEM_NUMBER

Private static end line ARG_PARAM1 = Parameter 1 ;

Private static end line ARG_PARAM2 = Parameter2 ;

// TODO : Renaming and changing parameter types

public notificationFragmentation() {

// Requires a public builder in case of vacancy

* Use this factory method to create a new instance

* this fragment using the specified parameters.

* @parameter Parameter1 Parameter 1.

* @parameter Parameter2 Parameter 2.

* @Back New copy of the notification fragment.

// TODO : Rename and change the species and number of parameters

notification of public statisticsFragmentation newInstance(String param1, String param2) {

Notification fragment = new notification fragment() ;

Bundleargs = new bundle() ;

args.put string(ARG_PARAM1, param1) ;

args.put string(ARG_PARAM2, param2) ;

fisgment.setargumenten(args) ;

public void onCreate(Bundle storedInstanceState) {

create super.onCreate(savedInstanceState) ;

if (getArguments() != null) {

mParam1 = getArguments().getString(ARG_PARAM1) ;

mParam2 = getArguments().getString(ARG_PARAM2) ;

Public view onCreateView(LayoutInflater inflater, ViewGroup container,

The package was kept. State party) {

// blow up the layout of this fragment.

return inflater.inflate(R.layout.fragment_notification, container, wrong) ;

Package com.bottomnavigationview.fragments; Import android.os.bundle; Import androidx.fragment.app.fragment; Import android.view.LayoutInflater; Import android.view.view; Import android.viewGroup; Import com.bottomnavigation View.R; /** * Simple subclass {@link Fragment}. * Use the factory default method {@link NotificationFragment#newInstance} to create * an instance of this fragment */ the NotificationFragment public class expands the fragment { // TODO : Rename parameter arguments, select names that match // fragment initialization parameters, e.g. ARG_ITEM_NUMBER closed static end line ARG_PARAM1 = param1; closed static end line ARG_PARAM2 = param2; // TODO : Rename and modify the following types of parameters: private String mParam1; private String mParam2; public NotificationFragment() { // Requires an empty public constructor } /** * Use this factory method to create a new instance * of this fragment using the supplied parameters. * @param param1 parameter 1 * @param param2 parameter 2 * @return New instance of the NotificationFragment. */ // TODO : Rename and change types and number of parameters public static NotificationFragment newInstance(String param1, String param2) { Fragment NotificationFragment = new NotificationFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Cancel public blank onCreate(bundle storedInstanceState) { super.onCreate(storedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); }. } @User interface of the creationCreationView public view(LayoutInflater inflater, ViewGroup container, package savedStatus) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_notification, container, false); } }.

Package com.bottomnavigationview.fragments ;

Import android.os.bundle; import
androidx.app.fragment; import
android.view.LayoutInflater; import
android.view.view; import
android.view.ViewGroup ;

Import com.bottomnavigationview.R

/**
* Simple subclass {@Link Fragment}.
* Use the factory default method {@link NotificationFragment#newInstance} for
* Create an instance of this fragment
*/
public classification NotificationFragment expands the fragment {
// TODO : Rename the parameter arguments, select the names that match the initialization parameters of fragments
//, e.g. ARG_ITEM_NUMBER
Private static end line ARG_PARAM1 = param1 ;
Private static end line ARG_PARAM2 = param2 ;

// TODO : Parameter types
private channel mParam1 ;
private channel mParam2 ;

public notificationFragmentation() {
// Empty public builder required
}

/**
* Use this factory method to create a new
* copy of this fragment with the specified parameters.
*
* @param param1 parameter 1.
* @param param2 parameter 2.
* @return New instance of the NotificationFragment.
*/
// TODO : Rename and modify the types and number of parameters
public static NotificationFragment newInstance (String param1, String param2) {
NotificationFragment = new NotificationFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArgumenten(args);
returnable fragment;
}

@Override of
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != zero) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Configure public view
to create the view (LayoutInflater inflater, Container ViewGroup,
Bundle savedInstanceState) {
// Layout setup for this fragment
return inflater.inflate(R.layout.fragment_notification, container, false);
}
}

fragment_notification.xml

<?xml Version=1.0 Encryption=utf-8? >

<FrameLayout 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

tools:context=.fragments.NotificationFragment

android:[email protected]+id/picture display

android: layout_broad=200dp

android: layout_height=200dp

android: layout_gravity=center

android:[email protected]/ic_notifications

<?xml version=1.0 encoding=utf-8? > <FrameLayout 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 tools:context=.fragments.notificationFragment > < android:[email protected]+id/imageView android:layout_width=200dp android:layout_height=200dp android:layout_gravity=android:[email protected]/ic_notifications /> </FrameLayout>.

<?xml version=1.0 encoding=utf-8?>
<FrameLayout xmlns:android=http://schemas.android.com/apk/res/android
xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_older
android:layout_height=match_older
tools:context=.fragments.NotificationFragment
>.

<View
android:[email protected]+id/imageView
android:layout_width=200dp
android:layout_height=200dp
android:layout_gravity=center
android:[email protected]/ic_notifications
/>
</FrameLayout>.

SmsFragment.java

Package com.bottomnavigationview.fragments ;

Import the android.os package;

androidx.fracgment.app Importfragment;

import android.view.layoutInflater ;

import android.view

Import android.view.ViewGroup ;

Import com.bottomnavigationview.R

* Simple subclass {@link Fragment}.

* Use the factory method {@link SmsFragment#newInstance} to

* create an example of this fragment.

the public class SmsFragment expands Fragment {

// TODO : Rename the parameter arguments, select the names that

// Fragmentation initialization parameters, e.g. ARG_ITEM_NUMBER

Private static end line ARG_PARAM1 = Parameter 1 ;

Private static end line ARG_PARAM2 = Parameter2 ;

// TODO : Renaming and changing parameter types

// Requires a public builder in case of vacancy

* Use this factory method to create a new instance

* this fragment using the specified parameters.

* @parameter Parameter1 Parameter 1.

* @parameter Parameter2 Parameter 2.

* @ Send back a new copy of the SMS fragment.

// TODO : Rename and change the species and number of parameters

public static SmsFragment newInstance (String param1, String param2) {

SmsFragment = new SmsFragment() ;

Bundleargs = new bundle() ;

args.put string(ARG_PARAM1, param1) ;

args.put string(ARG_PARAM2, param2) ;

fisgment.setargumenten(args) ;

public void onCreate(Bundle storedInstanceState) {

create super.onCreate(savedInstanceState) ;

if (getArguments() != null) {

mParam1 = getArguments().getString(ARG_PARAM1) ;

mParam2 = getArguments().getString(ARG_PARAM2) ;

Public view onCreateView(LayoutInflater inflater, ViewGroup container,

The package was kept. State party) {

// blow up the layout of this fragment.

return inflater.inflate(R.layout.fragment_sms, container, wrong) ;

Package com.bottomnavigationview.fragments; Import android.os.bundle; Import androidx.fragment.app.fragment; Import android.view.LayoutInflater; Import android.view.view; Import android.viewGroup; Import com.bottomnavigation View.R; /** * Simple subclass {@link Fragment}. * Use the factory method {@link SmsFragment#newInstance} to create * an instance of this fragment */ The public SmsFragment class expands the fragment { // TODO : Rename parameter arguments, select names that match // fragment initialization parameters, e.g. ARG_ITEM_NUMBER closed static end line ARG_PARAM1 = param1; closed static end line ARG_PARAM2 = param2; // TODO : Rename and modify the following types of parameters: private String mParam1; private String mParam2; public SmsFragment() { // Requires an empty public constructor } /** * Use this factory method to create a new instance * of this fragment using the supplied parameters. * @param param1 parameter 1 * @param param2 parameter 2 * @return New instance of the SmsFragment. */ // TODO : Rename and change the types and number of parameters of the public static SmsFragment newInstance(String param1, String param2) { SmsFragment fragment = new SmsFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Cancel public blank onCreate(bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); }. } @CreationView public view user interface (LayoutInflater inflater, container ViewGroup, Bundle SavedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_sms, container, false); } }

Package com.bottomnavigationview.fragments ;

Import android.os.bundle; import
androidx.app.fragment; import
android.view.LayoutInflater; import
android.view.view; import
android.view.ViewGroup ;

Import com.bottomnavigationview.R

/**
* Simple subclass {@Link Fragment}.
* use factory method {@link SmsFragment#newInstance} at
* create an instance of this fragment
*/
public class SmsFragment expands fragment {
// TODO : Rename the parameter arguments, select the names that match the initialization parameters of fragments
//, e.g. ARG_ITEM_NUMBER
Private static end line ARG_PARAM1 = param1 ;
Private static end line ARG_PARAM2 = param2 ;

// TODO : Parameter types
private channel mParam1 ;
private channel mParam2 ;

public SmsFragment() {
// Empty public builder required
}

/**
* Use this factory method to create a new
* copy of this fragment with the specified parameters.
*
* @param param1 parameter 1.
* @param param2 parameter 2.
* @return New instance of the SMS fragment.
*/
// TODO : Rename and modify the types and number of parameters
public static SmsFragment newInstance (String param1, String param2) {
SmsFragment = new SmsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArgumenten(args);
returnable fragment;
}

@Override of
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != zero) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Create view (LayoutInflater inflater, ViewGroup-Container,Bundle savedInstanceState) {// Layout inflation for this fragment return inflater.inflate(R.layout.fragment_sms, Container, false);}}}.

fragment_sms.xml

<?xml Version=1.0 Encryption=utf-8? >

<FrameLayout 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

Tools:context=.fragments.SmsFragment

android:[email protected]+id/picture display

android: layout_broad=200dp

android: layout_height=200dp

android: layout_gravity=center

android:[email protected]/ic_sms

<?xml version=1.0 encoding=utf-8? > <FrameLayout 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 tools:context=.fragments.SmsFragment > <imageView android:[email protected]+id/imageView android:layout_width=200dp android:layout_height=200dp android:layout_gravity=android:[email protected]/ic_sms /> </FrameLayout>.

<?xml version=1.0 encoding=utf-8?>
<FrameLayout xmlns:android=http://schemas.android.com/apk/res/android
xmlns:tools=http://schemas.android.com/tools
android:layout_width=match_older
android:layout_height=match_older
tools:context=.fragments.SmsFragment
>

<imageView
android:[email protected]+id/imageView
android:layout_width=200dp
android:layout_height=200dp
android:layout_gravity= Center
android:[email protected]/ic_sms
/>

</FrameLayout>

7. Beginning of fragments

public null and void openFragment {

FragmentTransaction transaction = getSupportFragmentManager() ;

transaction.replace(R.id.container, fragment);

transaction.addToBackStack(null) ;

openFragment(fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.container, fragment); transaction.addToBackStack(null); transaction.commit(); }.

public void openFragment(fragment) {
FragmentTransaction transaction = getSupportFragmentManager();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}.

Source code for the entire principal activity in

Package com.bottomnavigation view ;

Import the android.os package;

android.view.menuImport item ;

androidx.annotation.non-zero import;

Import androidx.appcompat.appCompatActivity ;

androidx.fracgment.app Importfragment;

Import androidx.fracgment.app.FragmentTransaction ;

com.bottomnavigationview.homeFragment ;

com.bottomnavigationview.NotificationFragment ;

com.bottomnavigationview.fragments.SmsFragment ;

com.google.android.material.bottomnavigation.BottomNavigationView ;

the MainActivity public class expands AppCompatActivity {

Soil NavigationViewing the Soil Navigation ;

protected hole onCreate(Bundle storedInstanceState) {

create super.onCreate(savedInstanceState) ;

setContentView(R.layout.activity_main) ;

bottomNavigation = findViewById(R.id.bottom_navigation) ;

bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener) ;

openFragment(HomeFragment.newInstance(, )) ;

public null and void openFragment {

FragmentTransaction transaction = getSupportFragmentManager() ;

transaction.replace(R.id.container, fragment);

transaction.addToBackStack(null) ;

BottomNavigationView.OnNavigationItemSelectedListener NavigationItemSelectedListener =

new BottomNavigationView.OnNavigationItemSelectedListener() {

@All publicly available Boolean elements in NavigationSelected(@Non-zero menu item entry) {

Exchange (item.getItemId()) {

the R.id.navigation_home case:

openFragment(HomeFragment.newInstance(, )) ;

Case of R.id.navigation_sms :

openFragment(SmsFragment.newInstance(, )) ;

R.id.navigation_messages :

openFragment (NotificationFragment.newInstance(, )) ;

com.bottomnavigation view package; import android.os.bundle; import android.view.MenuItem; import androidx.annotation.NonNull; import androidx.appCompatActivity; import androidx.fragment.app Fragment; import androidx.fragment.appTransaction; import com.bottomnavigationview.fragments.HomeFragment; import com.bottomnavigationview.fragments.NotificationFragment; import com.bottomnavigationview.fragments.SmsFragment; import com.google.android.material.bottomnavigation.BottomNavigationView; the MainActivity public class extends AppCompatActivity { BottomNavigationView bottomNavigation; @Overwrite protected blank onCreate(bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomNavigation = findViewById(R.id.bottom_navigation); bottomNavigation.setOnNavigationItemSelectedListener(navigationItemSelectedListener); openFragment(HomeFragment.newInstance(, )); }. public void openFragment(Fragment) { FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.container, Fragment); transaction.addToBackStack(null); transaction.commit(); } BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() { @All Public starts atNavigationItemSelected(@NonNull MenuItem item)). { switch (item.getItemId()) Case R.id.navigation_home: openFragment(HomeFragment.newInstance(, )); Return true; Case R.id.navigation_sms: openFragment(SmsFragment.newInstance(, )); Return true; Case R.id.navigation_notifications: openFragment(NotificationFragment.newInstance(, ); Return true; } Return false; } }.

Package com.bottomnavigation view ;

android.os.import bundle; android.view.import MenuItem; androidx.annotation.import NonNull; androidx.appcompat.app.AppCompatActivity; androidx.fragment.app.import FragmentTransaction; com Import.bottomnavigationview.snippets.HomeFragment; Import com.bottomnavigationview.fragments.NotificationFragment; Import com.bottomnavigationview.fragments.SmsFragment; Import com.google.android.material.bottomnavigation.BottomNavigationView ;

the MainActivity public class expands AppCompatActivity {

Soil NavigationViewing the Soil Navigation ;

Override
protected void onCreate(storedInstanceState bundle) {
super.onCreate(storedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigation = findViewById(R).id.bottom_navigation);
bottomNavigation.setOnNavigationItemSelectedListener;
openFragment(HomeFragment.newInstance(, ));
}.

public void openFragment(fragment) {
FragmentTransaction transaction = getSupportFragmentManager();
transaction.replace(R.id.container, fragment);
transaction.addToBackStack(null);
transaction.commit();
}.

BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@All publicly available Boolean onNavigationItemSelected(@NonNull MenuItem item)). {
switch (item.getItemId()). {
registry R.id.navigation_home:
openFragment(HomeFragment.newInstance(, ));
return true;
case R.id.navigation_sms:
openFragment(SmsFragment).newInstance(, ));
return true;
case R.id.navigation_notifications:
openFragment(NotificationFragment.newInstance(, ));
return true;
}
return false;
}
};
}.

Conclusion

In this article we learned how to create a bottom navigation bar in Android with BottomNavigationView. We also create an example application that clarifies the implementation of soil navigation.

Search solution code

I recommend that you read the official design manual for the lower navigation bar.

If you have any questions, please feel free to ask them in the comments section below. Happy coding

 

 

 bottom navigation bar android kotlin,custom bottom navigation bar android

You May Also Like