Search code examples
androidandroid-scrollviewnavigation-drawer

ScrollView inside a NavigationDrawer menu


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


Solution

  • 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