KarmaCoding
KarmaCoding

Reputation: 97

Android RecyclerView Gravity issue

I am using DrawerLayout to show Navigation Drawer, in that A NavigationView, the xml code is :

MyDrawerLayout.xml

<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">

    <include layout="@layout/layout_navigation_list" />

</android.support.design.widget.NavigationView>

layout_navigation_list.xml

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/white"
 android:orientation="vertical">

   <include
    android:id="@+id/headerLayout"
    layout="@layout/nav_header_dash_nav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_gravity="top"
    android:gravity="top" />

   <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerNav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/headerLayout"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:background="@color/white"
    android:foregroundGravity="bottom" />

</RelativeLayout>

ScreenImage

In the RecyclerView i have only 3 items to be shown, and they are aligning on the top of the RecyclerView layout which is i don't want. Hence my question here is , how to send those items to the bottom of the RecyclerView (just like shown in the image).

Upvotes: 2

Views: 258

Answers (3)

DHAVAL A.
DHAVAL A.

Reputation: 2321

You should have to MATCH_PARENT height instead of WRAP_CONTENT in root relative layout in file layout_navigation_list.xml. So it looks like,

<RelativeLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@color/white"
   android:orientation="vertical">
 ...
 ...
 ...
</RelativeLayout>

Notes: Gravity won't work with relative layouts.

Upvotes: 0

Ferran
Ferran

Reputation: 1448

Insert a ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:orientation="vertical">

    <include
        android:id="@+id/headerLayout"
        layout="@layout/nav_header_dash_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_gravity="top"
        android:gravity="top" />

    <android.support.constraint.ConstraintLayout
        android:layout_below="@id/headerLayout"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerNav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            app:layout_constraintBottom_toBottomOf="parent"
         />

    </android.support.constraint.ConstraintLayout>

</RelativeLayout>

Upvotes: 0

milad salimi
milad salimi

Reputation: 1660

Set match_parent instead wrap_content field layout_height in relativeLayout :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="@color/white"
 android:orientation="vertical">

Upvotes: 1

Related Questions