Android New Bottom Navigation Bar or Bottomnavigationview

Android new Bottom Navigation bar or BottomNavigationView

I think you might looking for this.

Here's a quick snippet to get started:

public class MainActivity extends AppCompatActivity {
private BottomBar mBottomBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Notice how you don't use the setContentView method here! Just
// pass your layout to bottom bar, it will be taken care of.
// Everything will be just like you're used to.
mBottomBar = BottomBar.bind(this, R.layout.activity_main,
savedInstanceState);

mBottomBar.setItems(
new BottomBarTab(R.drawable.ic_recents, "Recents"),
new BottomBarTab(R.drawable.ic_favorites, "Favorites"),
new BottomBarTab(R.drawable.ic_nearby, "Nearby"),
new BottomBarTab(R.drawable.ic_friends, "Friends")
);

mBottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
@Override
public void onItemSelected(final int position) {
// the user selected a new tab
}
});
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mBottomBar.onSaveInstanceState(outState);
}
}

Here is reference link.

https://github.com/roughike/BottomBar

EDIT New Releases.

The Bottom Navigation View has been in the material design guidelines for some time, but it hasn’t been easy for us to implement it into our apps. Some applications have built their own solutions, whilst others have relied on third-party open-source libraries to get the job done. Now the design support library is seeing the addition of this bottom navigation bar, let’s take a dive into how we can use it!

How to use ?

To begin with we need to update our dependency!

compile ‘com.android.support:design:25.0.0’

Design xml.

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Content Container -->

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/bottom_navigation_main" />

</RelativeLayout>

Create menu as per your requirement.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_favorites"
android:enabled="true"
android:icon="@drawable/ic_favorite_white_24dp"
android:title="@string/text_favorites"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_schedules"
android:enabled="true"
android:icon="@drawable/ic_access_time_white_24dp"
android:title="@string/text_schedules"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_music"
android:enabled="true"
android:icon="@drawable/ic_audiotrack_white_24dp"
android:title="@string/text_music"
app:showAsAction="ifRoom" />
</menu>

Handling Enabled / Disabled states. Make selector file.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="@color/colorPrimary" />
<item
android:state_checked="false"
android:color="@color/grey" />
</selector>

Handle click events.

BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);

bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorites:

break;
case R.id.action_schedules:

break;
case R.id.action_music:

break;
}
return false;
}
});

Edit : Using Androidx you just need to add below dependencies.

implementation 'com.google.android.material:material:1.2.0-alpha01'

Layout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_gravity="bottom"
app:menu="@menu/bottom_navigation_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</FrameLayout>

If you want to read more about it's methods and how it works read this.

Surely it will help you.

bottom navigation view do not work with navigation component


Initialize BottomNavigationView and Navigation controller in your Activity and merge them.

Suppose Your Activity Name "SecondActivity".

class SecondActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_second)

val bottomNaviagtionview: BottomNavigationView = findViewById(R.id.bottomNavigationView)
val navController: NavController = findNavController(R.id.fragment)
bottomNaviagtionview.setupWithNavController(navController)
}
}

Loading an image inside a bottom navigation bar item icon using an URL

Menus do not support coloured images

You can not use a coloured image in these places and a bit more also:

  1. In bottom navigation
  2. In navigation drawer
  3. In popup menu
  4. etc...

Basically which ever view uses a menu file for its resource, cannot have a coloured image. That is why you don't see it. To test it yourself, take a colourful image in your drawable and set it as an icon for the bottom navigation. You notice that it comes the same way. This proves it

Edits



  1. This post gives some info on how to do it
  2. You can do it this way:
    mBottomNav.setItemIconTintList(null);

Bottom Navigation Bar Floating Button with action covering the whole screen - how to do this?

Instead of navigating to the fragment, try to start another Activity after clicking "Add" floating action button.
Your new Activity will not have bottom navigation bar as you expected.

live view of bottom navigation in design view is not showing icons and the menu resource

This problem occurred while using the new material io 1.5.0 library, and it's working fine on 1.3.0.

Thanks Niaj Mahmud for your help and support



Related Topics



Leave a reply



Submit