In my app there is Bottom Navigation Bar i want to add Navigation Drawer because there is many buttons that dont fit at Bottom Navigation Bar.
i tried many ways but unfortunately none of them worked
here is my Bottom Navigation Bar xml code
<?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_height="match_parent"
android:background="#color/colorAccent2"
tools:context=".UI.HomeActivity"
tools:layout_editor_absoluteY="81dp">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="0dp"
app:layout_constraintBottom_toTopOf="#+id/navigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread_inside">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/navigationView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#fcff38"
app:itemIconTint="#000000"
app:itemTextColor="#303132"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/frame1"
app:menu="#menu/menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Any answer could help,
Thanks.
You need to add 'NavigationView' class in the layout, also wrap the root view group inside DrawerLayout.
<androidx.drawerlayout.widget.DrawerLayout>
<androidx.constraintlayout.widget.ConstraintLayout>
<FrameLayout
...
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView/>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView />
</androidx.drawerlayout.widget.DrawerLayout>
This post will help you: NavigationView
Related
All my Android fragment layouts seem to be offset by the navbar height. This also means they get covered at the bottom by the tab bar. What am I doing wrong?
Examples:
I'm not sure what code you want to see; most of it came straight out of Android Studio. And apparently I can't post it all here at once - "It looks like your post is mostly code." :(
activity_main.xml
<?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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:labelVisibilityMode="labeled"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment_activity_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
Sample fragment: fragment_home.xml
<?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_height="match_parent"
tools:context=".ui.carnival.CarnivalFragment">
<TextView
android:id="#+id/text_home"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
exchange android:layout_height="match_parent" set for <fragment to android:layout_height="0dp", allow constraints to to their job, attach fragment above bottom nav. currently looks like app:layout_constraintBottom_toTopOf="#id/nav_view" isn't making your Fragment appear above bottom nav, instead its fulfilling whole Activity, even space under bottom nav
top spacing is probably introduced by android:paddingTop="?attr/actionBarSize" line set for root element, just remove this line
btw. there is a dev option "show layout bounds", it will show you where your views/fragments are placed
I have activity with FloatingActionButton and BottomNavigationView.
I want FloatingActionButton have position above BottomNavigationView. but no matter what I do, it is always with BottomNavigationView like on img
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".MainActivity2">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="#drawable/ic_baseline_addchart_24" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/yu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:menu="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Place FAB button inside the constraint layout and then set the property
app:layout_constraintTop_toBottomOf="#id/fab"
to your Navigation View and
app:layout_constraintBottom_toTopOf="#id/bottom_bar"
android:layout_margin="16dp"
In this way the FAB button will always stay on top of navigation view.
here you go just replace it with your floatbutton tag
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_marginBottom="70dp"
android:layout_marginEnd="16dp"
android:src="#drawable/ic_baseline_addchart_24" />
the bottom navigation is almost 56dp height so what we did is we added 70dp to marginBottom to lift it above the navigation and it will stay there always, you can change that value as your likings :)
in my app i created a bootom menubar layout with floating button and i include that in my main activity and also i place a recycler view in my main activity
i just add 100 items to display in my recyclerview to check whether it was working or not for that when i run my application the recycler view of last item hides inside the bottom menu bar how to solve this
here is the sample image of what i said
activity_main.xml
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/toolbar"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
/>
<include
android:id="#+id/bottom_nav"
layout="#layout/bottom_menu_bar"
/>
bottom menu bar. xml
<?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"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="#menu/bottom_nav_menu" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/margin_padding"
android:src="#drawable/ic_add"
android:tint="#color/colorWhite"
app:backgroundTint="#color/colorPrimary"
app:layout_anchor="#id/bottom_nav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:elevation="#dimen/floating_b_ele"
android:clickable="true"
android:focusable="true"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
add a layout_marginBottom to the RecyclerView which is equal to the height of the bottom navigation
I would like to hide a layout behind another component in my activity_main.xml. I am using a bottom sheet and would only like to view the top portion. The bottom should be hidden behind my toolbar. Is there a function or property that will push the layout behind the component?
main_activity.xml
<?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_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="#+id/top_app_bar"
android:name="com.ds.base.fragments.TopAppBarFragment"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_constraintBottom_toTopOf="#+id/include"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="spread" />
<include
android:id="#+id/include"
android:layout_height="0dp"
android:layout_width="match_parent"
layout="#layout/content_main"
app:layout_constraintBottom_toTopOf="#+id/main_bottom_app_bar"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/top_app_bar"/>
<fragment
android:id="#+id/main_bottom_app_bar"
android:name="com.ds.base.fragments.BottomAppBarFragment"
android:layout_width="match_parent"
android:layout_height="90dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/include">
</fragment>
<include
android:id="#+id/time_remaining_layout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
layout="#layout/time_remaining_bottom_sheet"
app:layout_constraintTop_toTopOf="#id/main_bottom_app_bar"/>
</androidx.constraintlayout.widget.ConstraintLayout>
use in xml
android:visibility="gone"
or
android:visibility="invisible"
also use in java(activity) code
view.setVisibility(View.INVISIBLE);
or
view.setVisibility(View.GONE);
To bring any View to top, You have to call
yourView.bringToFront()
I am trying to specify my own Android Navigation Drawer.
There is dynamic data shown so I can't just set it up in a menu.xml.
Right now I am trying to customize the look of a TextView into my ListView to look pretty much like a Menu Item but these classes are all abstract. Furthermore I was not able to find something usefull at the material.io.
What I am looking for:
What I got right now:
Here is the corresponding XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".SlideshowActivity"
android:id="#+id/drawer_slide_show">
<android.support.constraint.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"
android:padding="0dp"
tools:context=".SlideshowActivity">
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButtonRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_arrow_right"
android:alpha="0.5" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingActionButtonLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginLeft="8dp"
android:alpha="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_arrow_left" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager_slideshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent">
</android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigationView_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white">
<ListView
android:id="#+id/listView_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="5px"
android:listSelector="#drawable/ic_media_pause_dark" >
</ListView>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
You need to Customize the navigation drawer by using the listview with custom Adapter.Here is example of Custom navigation drawer.