BottomNavigationView not inflating - android

I'm trying to inflate a BottomNavigationView in activity_main.xml but to no avail.
Here's my bottom_navigation.xml in the menu resource directory:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="nav_library"
android:icon="#drawable/svg_library"
android:title="Library" />
<item
android:id="nav_search"
android:icon="#drawable/svg_search"
android:title="Search" />
<item
android:id="nav_liked"
android:icon="#drawable/svg_heart"
android:title="Liked" />
</menu>
And here's my activity_main.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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
android:background="?android:attr/windowBackground" />
</RelativeLayout>
It seems that android:layout_height="wrap_content" does not detect the icons nor the title of the menu and as such, the height and width of the navigation bar are both 0.
Here's an image with the wrap_content attribute. (Not enough rep for an embed)
Please advise.
P.S This is my first question on Stack Overflow, so my question formatting might not be the best.

A bit late to the party. But I eventually figured it out that day. Was missing the prefix #+id/:
<item
android:id="#+id/nav_library"
android:icon="#drawable/svg_library"
android:title="Library" />

Related

(android) Why doesn't app:actionLayout="" work?

Hi
I'm using Navgation Drawer.
I want to change from text to text and icon at overflow menu items.
So i used 'android:icon' and 'android:title' in menu.xml. But Anything is not displayed.
To be exact, I could only see text.
and
I used android:actionLayout="#layout/overflow_menu_item". In overflow_menu_title, Text and Icon are defined.
But It dosen't work, too
I don't no why it dosen't work.
How to set Text and icon in overflow menu item?
I searched a lot but it didn't help.
plz help me...
Here is Code
overflow_menu.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MovieList">
<item
android:id="#+id/menu_movie"
android:orderInCategory="150"
android:icon="#drawable/ic_search_black_24dp"
android:title="setting1"
android:actionLayout="#layout/overflow_menu_title"
app:showAsAction="never" />
<item
android:id="#+id/menu_gallery"
android:orderInCategory="150"
android:title="setting2"
android:icon="#drawable/ic_search_black_24dp"
app:showAsAction="never" />
<item
android:id="#+id/settings3"
android:orderInCategory="150"
android:icon="#drawable/ic_search_black_24dp"
android:title="setting2"
app:showAsAction="never" />
</menu>
overflow_menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:padding="2dp"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/ic_15"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MOVIE"
android:textSize="24dp"/>
</LinearLayout>
You can't create a custom layout for menu item using android:actionLayout for items that are not on the action bar.
In other meaning, you can't set android:actionLayout for non-action items that are in the overflow menu items.

Image as background in menu item

As said in the question.
What I currently have:
Desired output:
On each column there would be a background image. I'm going to remove the text and just fill items with images. Is this possible? If yes, do I need different resolutions for background images?
Activity_main_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_ITEM1"
android:title="#string/ITEM1"
/>
<item
android:id="#+id/nav_ITEM2"
android:title="#string/ITEM2" />
<item
android:id="#+id/nav_ITEM3"
android:title="#string/ITEM3" />
</group>
</menu>
Depending on what exactly you are looking to do, you could try add an icon:
<item
android:id="#+id/nav_ITEM1"
android:icon="#drawable/item1_image"
android:title="#string/ITEM1" />
EDIT / UPDATE: to set the 'background image' of a menu entry, try:
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemBackground="#drawable/YOUR_DRAWABLE_HERE". <<<<<<<<<
app:headerLayout="#layout/nav_header_home"
app:menu="#menu/activity_home_drawer" />
Unfortunately, this sets the same image for each entry - but, it is a good starting point. Will update if I see a way of customising each entry.
LAST UPDATE, This allows you to define a unique background for each entry. Its not perfect, but maybe you can tweak it to work:
include 'app' in your xml for your menu:
xmlns:app="http://schemas.android.com/apk/res-auto"
Then, make a simple layout file that has an image:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/announce"/>
</FrameLayout>
In menu, update the item to point to the layout file you made above
<item
android:id="#+id/nav_gallery"
android:title=""
app:actionLayout="#layout/test" /> <<<<< HERE
Source: found some methods and ideas here: https://stablekernel.com/article/using-custom-views-as-menu-items/

Bottom Navigation Icons are Cropped Android

I'm assembling an app with a Botton navigation bar in Android with Android Studio. With two icons it looked fine, but when I added four, the icons look cropped. I can't find any other error like this. Any suggestions are welcomed.
Cropped icons bottom nav 4 options
Here is the code for the Bottom Navigation (bottom_navigation.xml)
<?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_lines"
android:enabled="true"
android:icon="#drawable/ic_lines_off"
android:title="Lines"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_contacts"
android:enabled="true"
android:icon="#drawable/ic_contacts_off"
android:title="Contacts"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_conversations"
android:enabled="true"
android:icon="#drawable/ic_conversations_off"
android:title="Conversations"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_settings"
android:enabled="true"
android:icon="#drawable/ic_settings_off"
android:title="Settings"
android:background="#android:color/transparent"
app:showAsAction="ifRoom" />
</menu>
In my activity.xml I call the bottom menu like this:
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottomNavigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:itemIconTint="#drawable/bnv_tab_item_foreground"
app:itemTextColor="#drawable/bnv_tab_item_foreground"
app:menu="#menu/bottom_navigation" />
Question Update:
I tried changing
app:showAsAction="ifRoom"
for
app:showAsAction="always"
And it doesn't solve the problem. There is no change to the issue. And it does not show the tag in every icon as it should. Only on the selected one.
One way of solving it is adding a Theme to the bottom navigation call in activity, like this:
android:theme="?attr/toolbarNavigationButtonStyle"
I have no clue why it works, but it solves the issue apparently in lots of devices exept in Samsung Galaxy Note 8
Here is "bnv_tab_item_foreground"
<?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/colorAccent" />
<item android:color="#android:color/darker_gray" />
</selector>
Try changing a value for the property below:-
app:showAsAction="always"
and if still, your problem persists, please provide the file
"bnv_tab_item_foreground"
Use Support Library 28. Then, just add app:labelVisibilityMode="labeled" to your BottomNavigationView XML declaration.
Hope this helps
I believe this is being caused by your layout not fitting into the System window, and you can easily fix this by going to the Layout you have the Bottom Navigation and at the root layout add android:fitsSystemWindows="true" just like I did below
<?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"
android:fitsSystemWindows="true">
<!--things come here -->
</RelativeLayout>

Android Studio Bottom Navigation View disappearing after adding menu items

I have made the menu and the Bottom Navigation View and its menu but when I add app:menu"#menu/bottom_navigation_menu" the whole Bar disappears and the app crashes when I try to run it.
Activity Main seems to be implemented correctly:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="com.troy.connor.lightswitch.MainActivity">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
app:menu="#menu/bottom_navigation_menu"/>
</LinearLayout>
As well as the menu items:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/buttonSlider"
android:icon="#drawable/ic_slider"
android:title="Sliders"
/>
<item
android:id="#+id/buttonPalette"
android:icon="#drawable/ic_palette"
android:title="Palette"
/>
<item
android:id="#+id/buttonShuffle"
android:icon="#drawable/ic_shuffle"
android:title="Shuffle"
/>
<item
android:id="#+id/buttonSchedule"
android:icon="#drawable/ic_alarm_off"
android:title="Schedule"
/>
<item
android:id="#+id/buttonConnect"
android:icon="#drawable/ic_bt_off"
android:title="Connect"
/>
<item
android:id="#+id/buttonStats"
android:icon="#drawable/ic_timeline"
android:title="Statistics"
/>
</menu>
If you check the stack trace for the crash, you'll find this:
03-29 00:08:30.568 9850 9850 E AndroidRuntime:
Caused by: java.lang.IllegalArgumentException:
Maximum number of items supported by BottomNavigationView is 5.
Limit can be checked with BottomNavigationView#getMaxItemCount()
Indeed, if you check the documentation for BottomNavigationView, you will find:
It should be used when application has three to five top-level destinations.
The solution is simple enough: delete one of your menu items. If that is not an option for you, you will have to create your own implementation of bottom navigation rather than using BottomNavigationView.

How to add custom style to menu items in Android Studio?

I made a navigation drawer following a youtube video.
Here is my activity_main.xml
<android.support.v4.widget.DrawerLayout 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"
android:orientation="vertical">
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:background="#color/menuColor"
app:headerLayout="#layout/header"
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
app:paddingStart="20px"
app:menu="#menu/drawermenu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And here is custom drawermenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/title"
android:title="Title"
android:layout_gravity="center"/>
<item
android:id="#+id/first"
android:title="First item" />
<item
android:id="#+id/second"
android:title="Second Item"
android:layout_gravity="center"/>
How can I add custom fonts and font sizes for the items like - Making the "Title" with a size of 26sp and other items with a size of 18sp. How can I also make them centered and also add padding to it?
I tried doing that from the design interface but it doesn't have any of those options. Using android:layout_gravity="center" that did not work for me either.
Try to having the
android:actionMenuTextAppearance
item under your action bar style, move it under your app theme.
Add this to strings.xml from values folder. Here for size their are different terms like and and to change font face you can use face inside font tag.
<resources>
<string name="history"><big><font face="serif" color="#303030">History</font></big></string>
<string name="favourites"><big><font color="#303030">Favourites</font></big></string>
</resource>
And change below one where you add menuitems.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/history"
android:title="#string/history" />
<item android:id="#+id/favourites"
android:title="#string/favourites"/>
</menu>

Categories

Resources