So basically,i'm using the bottom sheet view from the support library. It opens up after a FAB is pressed.
This is the bottom sheet view file:
<LinearLayout
android:id="#+id/bottomSheetView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/bottom_sheet_behavior"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="150dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="New"
android:id="#+id/newLabel"
android:layout_gravity="center_horizontal" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#+id/newLabel"
android:background="#c0c0c0"/>
<Button
android:id="#+id/newFileButton"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="File"
android:layout_gravity="center_horizontal"
/>
<Button
android:id="#+id/newFolderButton"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="Folder"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
and this is the main activity layout:
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/coordinator"
>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- The first child in the layout is for the main Activity UI-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
tools:context=".MainActivity"
android:background="#ffffffff"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/cwdLabel" />
<!--view that creates a dividing line between listview and textview -->
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_below="#+id/cwdLabel"
android:background="#c0c0c0"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fileList"
android:layout_below="#+id/cwdLabel"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:borderWidth="0dp"
app:backgroundTint="#color/colorAccent"
app:elevation="6dp"
android:clickable="true"
app:fabSize="normal"
app:pressedTranslationZ="12dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_add_white_18dp"
/>
</RelativeLayout>
<!-- Side navigation drawer UI -->
<ListView
android:id="#+id/navList"
android:layout_width="275dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"
/>
</android.support.v4.widget.DrawerLayout>
<include layout="#layout/bottom_sheet_new"/>
</android.support.design.widget.CoordinatorLayout>
The problem is, that the bottom sheet doesn't open when you press the FAB.
Instead, it opens after you slide the drawer out.Also, the bottom sheet is transparent, which is not what i have set it to be.
I searched around for answers, and after reading a few other questions, i think it has to do with the elevation of the drawer layout, but i don't really know how to solve that problem
I had the same issue, I updated the com.android.support:appcompat to the last version and now it works!
Nowadays I have
'com.android.support:appcompat-v7:23.4.0'
'com.android.support:design:23.2.0'
Try this,
Put this after initilizing views
ViewCompat.postOnAnimation(coordinator, new Runnable() {
#Override
public void run() {
ViewCompat.postInvalidateOnAnimation(coordinator);
}
});
Hope it helps
Related
Please help me to make a navigation drawer with two menus and two different button to inflate the each Menu Items and click listener
Below is the Mock up and I want to make drawer like these
Navigation Drawer First Button View
Navigation Drawer Second Button View
Please check this, it will help to add a custom view. You can use tablayout and view pager
I have read your question very carefully and try to make this layout. Finally i make it .
try this code to achieve your layout
activity_home_navigation_activtiy
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_home_navigation_activtiy"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--<include layout="#layout/nav_header_home_navigation" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".28"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingBottom="15dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/navigation_user_img"
android:layout_width="65dp"
android:layout_height="65dp"
android:src="#drawable/ic_launcher_foreground" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/navigation_user_name_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Picks App"
android:textColor="#504C4C"
android:textSize="17dp"
android:textStyle="italic" />
<TextView
android:id="#+id/navigation_user_email_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Last Name"
android:textColor="#000"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/navigation_nested"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".72"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:gravity="center"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<Button
android:id="#+id/usr_bt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="User"/>
<Button
android:id="#+id/admin_bt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="10dp"
android:text="Admin"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/user_recyler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:paddingLeft="15dp"
android:scrollbars="none" />
<android.support.v7.widget.RecyclerView
android:id="#+id/admin_recyler"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:paddingLeft="15dp"
android:scrollbars="none" />
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In this layout i try to make custom drawable . you can also customize according to your need
see screen shot
Thank You
How do you add margin between a bottom sheet and a Floating Action Button in android. I made it working by using another FAB and making it invisible but the FAB is only just above the Bottom Sheet. See this picture : 1
And the code :
<android.support.design.widget.CoordinatorLayout
android:id="#+id/coordinator"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:theme="#style/Theme.AppCompat.NoActionBar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="#+id/search_relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/Theme.AppCompat.NoActionBar">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#android:color/white"
android:elevation="1dp"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="#string/bottom_sheet_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circle_picture"
android:src="#mipmap/login_background"
android:layout_margin="10dp"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/name_bottom_sheet"
android:layout_toEndOf="#id/circle_picture"
android:text="Title"
android:textColor="#color/buttonLoginColor"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/address_bottom_sheet"
android:layout_below="#id/name_bottom_sheet"
android:layout_toEndOf="#id/circle_picture"
android:layout_marginStart="10dp"
android:textColor="#color/btn_create"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_margin="15dp"
app:layout_anchor="#id/bottom_sheet"
app:layout_anchorGravity="top|end"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginEnd="15dp"
app:backgroundTint="#color/buttonLoginColor"
app:borderWidth="0dp"
app:elevation="6dp"
app:layout_anchor="#id/fab2"
app:layout_anchorGravity="top"/>
</android.support.design.widget.CoordinatorLayout>
I'm using a NestedScrollView to display the bottom sheet.
I had the same issue. After hours of researching I figured out it can't be made through layout_anchor because of Material design guidelines. You need to use trick like one below. I'm using a transparent view that makes margin between fab and bottom sheet content view.
This is how I solved the issue:
FAB itself:
<android.support.design.widget.FloatingActionButton
...
app:layout_anchor="#+id/bottomPanel"
app:layout_anchorGravity="right"/>
BottomSheet:
<FrameLayout
android:id="#+id/bottomPanel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgound="#android:color/transparent"
app:layout_behavior="#string/bottom_sheet_behavior">
<ContentView
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="44dp"
android:backgound="#color/colorPrimary" />
</FrameLayout>
Just make the root view transparent and your content view put inside that and add margin of 44dp (fab size = 56dp. fab padding = 16dp. 56/2 + 16 = 44)
Yes, you can try with RelativeLayout , I'm still not able to understand why you have gave bottom sheet fixed height with 300dp, anyway this will do it without another fab button..
<RelativeLayout
android:id="#+id/coordinator"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:theme="#style/Theme.AppCompat.NoActionBar"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:id="#+id/search_relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="#style/Theme.AppCompat.NoActionBar">
<com.google.android.gms.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentBottom="true"
android:background="#android:color/white"
android:elevation="1dp"
app:behavior_hideable="true"
app:behavior_peekHeight="0dp"
app:layout_behavior="#string/bottom_sheet_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/circle_picture"
android:src="#mipmap/login_background"
android:layout_margin="10dp"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="#+id/name_bottom_sheet"
android:layout_toEndOf="#id/circle_picture"
android:text="Title"
android:textColor="#color/buttonLoginColor"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/address_bottom_sheet"
android:layout_below="#id/name_bottom_sheet"
android:layout_toEndOf="#id/circle_picture"
android:layout_marginStart="10dp"
android:textColor="#color/btn_create"
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/floatingButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_sheet"
android:layout_margin="15dp"
app:backgroundTint="#color/buttonLoginColor"
app:borderWidth="0dp"
android:layout_alignParentRight="true"
app:elevation="6dp"
app:layout_anchorGravity="top"/>
</RelativeLayout>
You can also set your fab button margins as per your requirement.
I have an AppCompatActivity with a listview with the layout below
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="#+id/main_content"
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/user_title"
android:background="#drawable/user_title_color"
android:textColor="#android:color/white"
android:textAppearance="#android:style/TextAppearance.Large"/>
<ListView android:id="#+id/joggings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false"/>
<LinearLayout
android:id="#+id/empty_list"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center">
<TextView
android:text="No joggings yet"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/empty_add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_jogging"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentRight="true"
android:layout_gravity="end|bottom"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="#drawable/ic_plus"
app:borderWidth="0dp" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimaryDark"
android:foregroundTint="#color/colorAccent"
app:itemTextColor="#color/bottom_navigation_color"
app:itemIconTint="#color/bottom_navigation_color"
app:menu="#menu/bottom_navigation_main" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
and in my code I have
mListView.setEmptyView(findViewById(R.id.empty_list));
in order to display a message and an "add" button when the list is empty. My problem is that when the list is empty the BottomNavigationView is hidden and the user cannot navigate to another activity.
One possible solution I can think of is to add another BottomNavigationView with the same items in the empty layout but that seems ugly.
Any other solution?
Set empty_list height to wrap_content
Your ListView and it's empty view should have the same layout attributes if you want them to occupy the same space. In your case, they should both be
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
Also, you should put the FloatingActionButton outside of the LinearLayout, or else it will affect the size of your ListView/empty view. It is also meant to be a direct child of CoordinatorLayout, otherwise its default behaviors do not work.
The following layout worked fine. The basic idea is that coordinateLayout contains listview and FloatingActionButton and empty view. CoordinateLayout is contained in an outer vertical LinearLayout which also contains the header textview and the BottomNavigationView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<TextView
android:id="#+id/user_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/user_title_color"
android:textAppearance="#android:style/TextAppearance.Large"
android:textColor="#android:color/white" />
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/joggings_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:drawSelectorOnTop="false" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/add_jogging"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_navigation"
android:layout_alignParentRight="true"
android:layout_gravity="end|bottom"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:clickable="true"
android:src="#drawable/ic_plus"
app:borderWidth="0dp"
app:layout_anchor="#id/joggings_list"
app:layout_anchorGravity="bottom|right|end" />
<LinearLayout
android:id="#+id/empty_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No joggings yet" />
<Button
android:id="#+id/empty_add_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimaryDark"
android:foregroundTint="#color/colorAccent"
app:itemIconTint="#color/bottom_navigation_color"
app:itemTextColor="#color/bottom_navigation_color"
app:menu="#menu/bottom_navigation_main" />
</LinearLayout>
I could able to create an app with toolbar and sliding navigation drawer with recycleView.
Now I am trying to add Floating action button but I am unable to figure out right place to add the button.
I am trying to add fab bottom right.
Below is my activity_main.xml file
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
And Fragment layout file fragment_navigtion_drawer.xml is
<RelativeLayout 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"
android:background="#color/drawerColor">
<LinearLayout
android:id="#+id/containerDrawerImage"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="#drawable/drawertitlebackground">
<ImageView
android:id="#+id/drawerAppImage"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginTop="5dp"
android:src="#mipmap/ic_launcher"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:text="Make search easy"
android:id="#+id/drawerSubTitle"
android:textStyle="bold|italic"
android:layout_centerVertical="true"
android:layout_gravity="bottom|center"
android:textColor="#color/textColorPrimary" />
<ImageButton
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="10dp"
android:layout_gravity="bottom|right"
android:src="#android:drawable/ic_menu_preferences"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawerList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_below="#+id/containerDrawerImage" />
</RelativeLayout>
Below is the the code for FAB
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="#id/container_body"
android:layout_gravity="right"
android:layout_marginRight="10dp"
app:rippleColor="#F06292"/>
I am trying to put this in Linearlayout under FrameLayout in activity_main.xml file. I could see the button appears bottom right.
But when I run the application, there shows a rectangle strip with same height which hides the content behind it. The rectangle strip is covering from left to right including the fab button but fab button is coming on its top.
Content behind is the fragment with Listview. So entries at bottom are getting hided because the fab button area is covered as rectangle.
I need just button appear and rest of area should be covered with fragment data i.e. listview in my case.
Its clue less for me where the issue is.
Solved:
I am able to resolve the issue by rewriting activity_main.xml in below way. Now FAB button appears properly. Below is updated one.
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="#id/container_body"
android:layout_gravity="bottom|right"
android:layout_marginRight="10dp"
android:layout_marginBottom="6dp"
app:fabSize="normal"
android:src="#drawable/ic_add_white_24dp"
app:backgroundTint="#color/colorPrimaryDark"
app:rippleColor="#F06292"/>
</FrameLayout>
</LinearLayout>
<fragment
android:id="#+id/fragment_navigation_drawer"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
I am trying to create a Navigation Drawer (it's outdated I know) with a custom layout. I am using a fragment within the NavDrawer to accomplish this.
The problem I am having is that when I run my app, my navigation drawer is automatically opening and I have no way to close it / no idea how. I suspect it has to do with the .commit() method at the end of my fragment transaction.
So my question is how do I get the nav drawer, housing a fragment, to operate like a normal nav drawer that only contains a ListView where it can be dragged in and out? Or if a fragment causes too many difficulties in a nav drawer is there another way for a super custom layout inside of a nav drawer? Thanks in advance!
Here is what it looks like. Code is below picture...
in MapsActivity.java:
//Instantiates Fragment inside of our Nav Drawer
NavigationDrawerFragment navDrawerFragment = new NavigationDrawerFragment();
getFragmentManager().beginTransaction()
.add(R.id.navDrawer_container,
navDrawerFragment).addToBackStack(null).commit();
activity_maps.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorMapToolbar"
android:minHeight="?attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp">
<!-- contentInsert is the initial padding that is added by the system at the start -->
<RelativeLayout
android:id="#+id/topLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<ImageView
android:id="#+id/back_action"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:gravity="center_vertical"
android:padding="6dp"
android:src="#drawable/icon_goleft" />
<ImageView
android:id="#+id/locker"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:gravity="center_vertical"
android:padding="6dp"
android:src="#drawable/lock_open" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
<View
android:id="#+id/dropshadow"
android:layout_width="match_parent"
android:layout_height="10dp"
android:layout_marginTop="?attr/actionBarSize"
android:background="#drawable/dropshadow_dark" />
<ImageButton
android:id="#+id/floatingActionImageButton"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="15dp"
android:background="#drawable/circle"
android:focusable="true"
android:src="#drawable/icon_location" />
<LinearLayout
android:id="#+id/markerLayout"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#color/colorMapToolbar"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/markerCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/cancel"
android:textColor="#android:color/white"
android:textSize="#dimen/abc_text_size_large_material" />
<TextView
android:id="#+id/markerMark"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/mark"
android:textColor="#color/colorAccent"
android:textSize="#dimen/abc_text_size_large_material"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<!-- FRAGMENT CONTAINER -->
<FrameLayout
android:id="#+id/navDrawer_container"
android:layout_width="240dp"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
fragment_navigation_drawer.xml: (accompanied by a .java for Fragment but it is not relevant here)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:background="#android:color/white">
<RelativeLayout
android:id="#+id/topRowLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:padding="8dp">
<TextView
android:id="#+id/userTitleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:padding="8dp"
android:text="#string/userTitle"
android:textStyle="bold" />
<TextView
android:id="#+id/alertTitleBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:padding="8dp"
android:text="#string/alertTitle"
android:textStyle="bold" />
<TextView android:id="#+id/lastFlamText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center_horizontal"
android:text="#string/lastFlameTitle"
android:layout_below="#+id/userTitleBtn" />
<TextView
android:id="#+id/scoreTextNavDrawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal|bottom"
android:padding="16dp"
android:textSize="100sp"
android:textStyle="bold"
android:layout_below="#+id/lastFlamText" />
</RelativeLayout>
<ListView
android:id="#+id/navList"
android:layout_width="240dp"
android:layout_height="0dp"
android:layout_gravity="left|start"
android:layout_weight="1" />
</LinearLayout>
The android:layout_gravity="left|start" attribute needs to go on the navDrawer_container FrameLayout in the activity_maps layout. This attribute lets the DrawerLayout know that this View is the drawer. The fact that it's missing would explain why your drawer is "open" on startup, and immovable.
<FrameLayout
android:id="#+id/navDrawer_container"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left|start" />
You don't necessarily need that attribute now on the ListView in the fragment_navigation_drawer layout.
Add this Code in your activity or Fragment
#Override
public void onStart() {
super.onStart();
if (drawer_layout.isDrawerOpen(Gravity.LEFT)) {
drawer_layout.closeDrawer(Gravity.LEFT);
}
}