I want to implement ScrollView in NavigationDrawer template of Android Studio. What do I need to do? How can make the full Navigation menu scrollable? I have a lot of options are here, but I can't scroll & view all of them
Demo Image
I am using for this including layout in navigationView which I can easily manage.
Like this
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="true">
<include
layout="#layout/nav_header_main_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
nav_header_main_navigation
<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:background="#color/app_gray"
android:gravity="bottom">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/rlTop"
android:layout_width="match_parent"
android:layout_height="88dp"
android:layout_alignParentTop="true"
android:background="#color/appYellow">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="24dp"
android:layout_marginEnd="30dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="30dp"
android:layout_marginStart="24dp"
android:ellipsize="end"
android:includeFontPadding="false"
android:maxEms="14"
android:maxLines="2"
android:textColor="#color/app_gray"
android:textSize="15sp" />
<RelativeLayout
android:id="#+id/rlBack"
android:layout_width="30dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:clickable="true">
<ImageView
android:id="#+id/ivBack"
android:layout_width="10dp"
android:layout_height="20dp"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="centerCrop"
android:src="#drawable/back_icon_black" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlBottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/rlTop"
android:background="#color/app_gray">
<RelativeLayout
android:id="#+id/rlProfile"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="10dp">
<ImageView
android:id="#+id/ivProfile"
android:layout_width="19dp"
android:layout_height="19dp"
android:layout_centerVertical="true"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
android:src="#drawable/icon_nav_profile" />
<TextView
android:id="#+id/tvProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#id/ivProfile"
android:layout_toRightOf="#id/ivProfile"
android:includeFontPadding="false"
android:paddingBottom="1dp"
android:paddingTop="1dp"
android:text="#string/profile"
android:textAlignment="viewStart"
android:textColor="#color/white"
android:textDirection="locale"
android:textSize="15sp" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Here is my whole Navigation activity XML
<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_main_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginEnd="-65dp"
android:layout_marginRight="-65dp"
android:fitsSystemWindows="true">
<include
layout="#layout/nav_header_main_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Here is a major factor to avoid ViewPager
app_bar_main_navigation
<android.support.design.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"
android:background="#color/app_gray"
tools:context="com.barq.consumer.ui.home.MainNavigationActivity">
<FrameLayout
android:id="#+id/flMain"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Now in MainNavigationActivity.Java
public class MainNavigationActivity extends BaseActivity {
#BindView(R.id.drawer_layout)
DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_navigation);
ButterKnife.bind(this);
//here is you can load your fragment without view pager
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.flMain, HomeFragment.newInstance(), HomeFragment.TAG)
.commit();
}
And other clicks you can handle manually to load other fragments in frame layout and activities and drawer open and close you also handle manually use drawer object. Hope so you can understand it.
its an example for your easiness to avoid menu and use normal layouts in the scroll view.
Here are more answers regarding the above example ULR
Related
In my main activity there is navigation drawer and fab button and inside content.xml which is the main xml to load content in my Main activity. In other word, I've navigation drawer activity provided by android studio.
Code in that content.xml is:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#f0eb79"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="#drawable/computer"
android:paddingTop="25dp"
android:text="Chemistry"
android:textAllCaps="false"
android:textColor="#2f2e2e"
android:textSize="18sp" />
<Button
android:id="#+id/b2"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#524d47"
android:foreground="?attr/selectableItemBackground"
android:drawableTop="#drawable/computer"
android:paddingTop="25dp"
android:text="Sports"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/b3"
android:layout_width="0dp"
android:foreground="?attr/selectableItemBackground"
android:layout_height="200dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:background="#e7595d"
android:drawableLeft="#drawable/computer"
android:paddingLeft="45dp"
android:text="Inventions"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</ScrollView>
And it is not completely scrolling as shown in the figure below. I also tested same xml code into another empty activity where there is NOT fab and nagivation drawer and there it is showing scrolling behaviour perfectly.
Similarly, this is my activitymain.xml's code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<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:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
It worked for me after adding these attribute in scrollview:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.nepali_test.MainActivity"
tools:showIn="#layout/app_bar_main"
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
I'm creating an android app and I've just added a Navigation drawer into one of my activities. I've successfully added it in, however, it now overlaps the content of the page. The jobs text and the rest should be below the ActionBar.
I've tried adding a margin to the top of the jobs text which does push it down as I wish. Is there a better way to move it down?
<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout 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/jobListPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
tools:context="com.kitkat.crossroads.Jobs.JobsActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jobs"
android:fontFamily="#font/bebasneueregular"
android:textSize="30dp"
android:textAlignment="center"
tools:layout_editor_absoluteX="163dp"
tools:layout_editor_absoluteY="16dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textName"
android:layout_width="125dp"
android:layout_height="50dp"
android:layout_marginLeft="60dp"
android:fontFamily="#font/bebasneueregular"
android:gravity="center_vertical"
android:text="Job Name"
android:textSize="15dp"
android:textColor="#FF000000"
android:visibility="visible" />
<TextView
android:id="#+id/textFrom"
android:layout_width="100dp"
android:layout_height="50dp"
android:fontFamily="#font/bebasneueregular"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/textName"
android:gravity="center_vertical"
android:text="From"
android:textSize="15dp"
android:textColor="#FF000000"
android:visibility="visible" />
<TextView
android:id="#+id/textTo"
android:layout_width="100dp"
android:layout_height="50dp"
android:fontFamily="#font/bebasneueregular"
android:textSize="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:gravity="center_vertical"
android:text="To"
android:textColor="#FF000000"
android:visibility="visible" />
</LinearLayout>
<ExpandableListView
android:id="#+id/jobListView12345"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
<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"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Becoz you assign the gravity="top" in Linearlayout so its happens
so u remove this gravity see my below code:
<LinearLayout 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/jobListPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
//remove gravity="top" from hear
tools:context="com.kitkat.crossroads.Jobs.JobsActivity">
if your problem not solve then u can take android:layout_marginTop="20dp" in inner Linearlayout
see below code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:orientation="vertical">
Don't take a include in LinearLayout. Becoz its change your actionbar so u can only remove gravity and assign margin top
I believe you should put
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
inside
<LinearLayout
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/jobListPage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
tools:context="com.kitkat.crossroads.Jobs.JobsActivity">
The reason why I was having the overlap was because I put the page content in the activity_main.xml layout and not the content_main.xml. Putting the content in that layout sorted the issue out and put the content below the action bar.
I need some help with a layout. I have a map, a drawer, a button for the drawer and a cardview to hold the autocomplete search box. Everything looks fine except i cannot interact with the map. Pinch zoom, swipe to move and My Location button are not working. I think it's because the layout is wrong and i'm not actually interacting with the map but with something else. Posting my layout below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
map:uiZoomControls="true"
map:uiZoomGestures="true"
map:mapType="normal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MapsActivity">
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/LeftDrawer"
android:layout_width="200dp"
android:layout_marginTop="60dp"
android:layout_height="500dp"
android:layout_gravity="start"
android:background="#drawable/layout_bg" >
<TextView
android:id="#+id/TitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#android:color/darker_gray"
android:text="Test Title" />
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_gravity="left|start"
android:background="#drawable/layout_bg"/>
<TextView
android:id="#+id/TitleTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="240dp"
android:textColor="#android:color/darker_gray"
android:text="Test Title 2" />
<Button
android:id="#+id/test_button1"
android:layout_width="30dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="20dp"
android:layout_height="30dp"
android:layout_gravity="left|start"
android:background="#drawable/settings"/>
<Button
android:id="#+id/test_button2"
android:layout_width="30dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="60dp"
android:layout_height="30dp"
android:layout_gravity="left|start"
android:background="#drawable/settings"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="top"
android:layout_marginTop="17dp"
android:layout_marginLeft="50dp"
android:layout_width="260dp"
android:layout_height="30dp"
card_view:cardCornerRadius="4dp">
<fragment
android:id="#+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
</android.support.v7.widget.CardView>
<Button
android:id="#+id/drawer_button"
android:layout_width="30dp"
android:layout_marginTop="17dp"
android:layout_marginLeft="5dp"
android:layout_height="30dp"
android:layout_gravity="left|start"
android:onClick="OpenCloseDrawer"
android:background="#drawable/settings"/>
</fragment>
</LinearLayout>
Edit: Posting a screenshot of what the app looks like. Top left button is the open/close drawer button. As seen in the screen shot, that button (#+id/drawer_button) becomes inaccessable. The drawer closes when touching outside, but i have code in that button that then never fires as the button is not accessable. So i would like to always have access to the button, as well as probably some way of detecting the drawer closing, so i can use the same code in that event as i do in my button in case the user just swipes/touches outside to close the drawer.
Here are my layouts as the are now.
activity_maps.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:id="#+id/map" layout="#layout/fragment" />
<RelativeLayout
android:id="#+id/whatYouWantInLeftDrawer"
android:layout_width="200dp"
android:layout_marginTop="60dp"
android:layout_height="500dp"
android:layout_gravity="start"
android:background="#drawable/layout_bg" >
<TextView
android:id="#+id/TitleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:textColor="#android:color/darker_gray"
android:text="Title1" />
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp"
android:layout_gravity="left|start"
android:background="#drawable/layout_bg"/>
<TextView
android:id="#+id/TitleTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="240dp"
android:textColor="#android:color/darker_gray"
android:text="Ttitle2" />
<Button
android:id="#+id/test_button1"
android:layout_width="30dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="20dp"
android:layout_height="30dp"
android:layout_gravity="left|start"
android:background="#drawable/settings"/>
<Button
android:id="#+id/test_button2"
android:layout_width="30dp"
android:layout_marginTop="270dp"
android:layout_marginLeft="60dp"
android:layout_height="30dp"
android:layout_gravity="left|start"
android:background="#drawable/settings"/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
map:uiZoomControls="true"
map:uiZoomGestures="true"
map:mapType="normal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MapsActivity">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/drawer_button"
android:layout_width="30dp"
android:layout_marginTop="17dp"
android:layout_marginLeft="5dp"
android:layout_height="30dp"
android:layout_gravity="left|start"
android:onClick="OpenCloseDrawer"
android:background="#drawable/settings"/>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="top"
android:layout_marginTop="17dp"
android:layout_marginLeft="50dp"
android:layout_width="240dp"
android:layout_height="30dp"
card_view:cardCornerRadius="4dp">
<fragment
android:id="#+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
</android.support.v7.widget.CardView>
</fragment>
</FrameLayout>
</LinearLayout>
what you need to understand is that your will have only two child like this
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
< first View, here you will have your google map/>
< second View, here you will have your ListView/>
</android.support.v4.widget.DrawerLayout>
now the problem is that you don't just have a plain google map to show as First View, what you can do is create a separate fragment which will replace your first view,
There are many ways to do this depending on what you want to achieve with it, it will work fine as long as you keep this pattern for DrawerLayout
well to make it simple create fragment which will have google map and your cardview
fragment.xml
<LinearLayout>
<FrameLayout>
<GoogleMapFragment
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<CardView
android:layout_width="280dp"
android:layout_height="?attr/actionBarSize"/>
</FrameLayout>
</LinearLayout>
now make some changes to your Navigation drawer layout like this
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<first View, include fragment & class representing that stuff/>
< second View, here you will have your ListView/>
</android.support.v4.widget.DrawerLayout>
this is just an example that you make implement your own layout:
Update
<FrameLayout>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<first View, include fragment & class representing that stuff/>
< second View, here you will have your ListView
margin="leave enough margin to top as you will have buttons, otherwise button will overlap listview"
/>
</android.support.v4.widget.DrawerLayout>
<LinearLayout>
<put your button Views here, so these will appear all the time/>
</LinearLayout>
</FrameLayout>
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);
}
}