Reputation: 1713
I am trying to add an item in a bottom menu navigation bar according to a var got from an API.
I am able to delete an item from this navigation bar, like this :
if (restaurant.acceptsBookings == false) {
bottom_navigation_view.menu.removeItem(R.id.bottom_menu_book)
}
The problem is, when I am launching my app, we can see the icon during like, half of a second, then it disappears.
This is not that bad, but I was hoping there is a better and neater way to do this ; for example by adding the elements in an empty navigation bar, instead of removing them.
Here is my navigation bar xml code :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/bottom_menu_home"
android:enabled="true"
android:title="@string/bottom_menu_home"
android:icon="@drawable/ic_home"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_menu"
android:enabled="true"
android:icon="@drawable/ic_menu"
android:title="@string/bottom_menu_menu"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_profile"
android:enabled="true"
android:title="@string/bottom_menu_profile"
android:icon="@drawable/ic_user"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_book"
android:enabled="true"
android:icon="@android:drawable/ic_menu_my_calendar"
android:title="@string/bottom_menu_bookings"
app:showAsAction="ifRoom" />
<item
android:id="@+id/bottom_menu_fidelity"
android:enabled="true"
android:icon="@drawable/giftgrey2"
android:title="@string/bottom_menu_fidelity"
app:showAsAction="ifRoom" />
</menu>
Do someone have a solution for this ?
Thanks in advance.
Upvotes: 2
Views: 2408
Reputation: 1713
First, thanks everyone for helping me.
I tried both methods and they work perfectly : Hide the navigation bar with
bottomNavigationMenu?.visibility = View.GONE
just after bottomNavigationMenu declaration, and then set
bottomNavigationMenu?.visibility = View.VISIBLE
just after API response.
The method which create dynamically an item works too, here is how
bottomNavigationMenu?.menu?.add(Menu.NONE, 1, Menu.NONE, "TEST")?.setIcon(R.drawable.ic_home)
Here is what the man tell about the add fun (from https://developer.android.com/reference/android/view/Menu.html ; ctrl + f "add" 43) :
groupId int: The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId int: Unique item ID. Use NONE if you do not need a unique ID.
order int: The order for the item. Use NONE if you do not care about the order. See getOrder().
title CharSequence: The text to display for the item.
Thanks everyone for helping !
Upvotes: 1
Reputation: 2529
Instead of using a static menu.xml file, what you can do is dynamically add menus that suits your condition.
Perhaps, this link may help: Inflate Bottom Navigation View menu programmatically
Upvotes: 0
Reputation: 2782
This sounds like a race condition.
What you can do is block the drawing of the navigation menu or the entire screen until after you get a response from the API. This just means your app will take half a second longer to launch.
Upvotes: 0