Recent Post

6/recent/ticker-posts

Bottom Navigation Android Example using Fragments||Programmer hubs just for programmer|| Programmer Rohit Sharma

 Today I'm Discuss about the bottom navigation view in android with java.

Bottom Navigation Android Example using Fragments


Bottom Navigation Android Example using Fragments

so let's start the Xml Design Phase :-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragementConatiner">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:id="@+id/bottomnavigation"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
app:menu="@menu/bottom"
android:background="@color/colorAccent"
/>
</FrameLayout>
</RelativeLayout>

bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_home"
android:title="Home"
android:icon="@drawable/ic_home"/>
<item android:id="@+id/menu_dashboard"
android:title="Dashboard"
android:icon="@drawable/ic_baseline_dashboard_24"/>
<item android:id="@+id/notification"
android:title="Notification"
android:icon="@drawable/ic_notification"
/>
</menu>


load method:-
private boolean loadfragment(Fragment fragment)
{
if(fragment!=null)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragementConatiner,fragment).commit();
return true;
}
return false;
}
}

main_activity.java
package com.example.demodb;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.os.Bundle;
import android.view.MenuItem;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navigationView = (BottomNavigationView) findViewById(R.id.bottomnavigation);
navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment frg = null;
switch (item.getItemId()) {
case R.id.menu_home:
frg = new HomeFragment();
break;
case R.id.menu_dashboard:
frg = new DashboardFragment();
break;
case R.id.notification:
frg=new NotificationFragment();
}
return loadfragment(frg);
}
});
loadfragment(new HomeFragment());
}

private boolean loadfragment(Fragment fragment)
{
if(fragment!=null)
{
getSupportFragmentManager().beginTransaction().replace(R.id.fragementConatiner,fragment).commit();
return true;
}
return false;
}
}
fragement xml code
HomeFragment.xml
<?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=".HomeFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Home Fragment"
android:layout_marginTop="70dp"
android:textSize="25dp"
android:textColorHint="#000"
android:textColor="#000"
android:textStyle="bold"
android:textAlignment="center"
/>

</FrameLayout>
NotificationFragment.xml
<?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=".NotificationFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Notification Fragment"
android:layout_marginTop="70dp"
android:textSize="25dp"
android:textColorHint="#000"
android:textColor="#000"
android:textStyle="bold"
android:textAlignment="center"
/>

</FrameLayout>

Post a Comment

0 Comments