Aswin P Ashok
Aswin P Ashok

Reputation: 711

RecyclerView in dialog takes up (almost) whole area

I have a dialog with layout

subject_view.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:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:paddingTop="8dp"
    android:paddingBottom="8dp">

    <TextView
        android:layout_marginLeft="15dp"
        android:layout_marginTop="15dp"
        android:text="Subject"
        android:textSize="18sp"
        android:textColor="@color/black"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/subject_name"/>

    <LinearLayout
        android:weightSum="2"
        android:gravity="end"
        android:layout_below="@id/subject_name"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ViewSwitcher
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            android:layout_below="@id/subject_name"
            android:id="@+id/switcher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:paddingLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/please_wait"/>
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="10dp"
                android:id="@+id/studentRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>
        </ViewSwitcher>

        <Button
            android:layout_weight="1"
            android:layout_below="@id/switcher"
            android:layout_alignParentRight="true"
            android:id="@+id/ok_Btn"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:background="@drawable/blue_button"
            android:text="ok"
            android:textColor="#fff"/>
    </LinearLayout>

</RelativeLayout>

When the dialog is created, I am fetching data from server to populate RecyclerView. At that time ViewPager's first child (with the progressbar) is visible. When I get data from server I set RecyclerView as visible child.

The problem here is, when RecyclerView have a lot of rows, the button under under ViewSwitcher containing the RecyclerView is only partially visible (Like, one third of the button is not visible).

Screenshot (How it is not supposed to look)

enter image description here

But when the recyclerView doesn't have that much rows its showing as expected,

Screenshot (How it is supposed to look)

enter image description here

How can I solve this?

Upvotes: 2

Views: 2550

Answers (5)

Sharath kumar
Sharath kumar

Reputation: 4132

Whenever you are adding weight for linear layout point to keep in mind are:

Give android:weightSum="1" as one and child layout weight from 0.0 to 1.0 ex:android:layout_weight="0.3"

Use layout weight as 0dp instead pf hardcoded value ex: android:layout_height="0dp" instead of android:layout_height="30dp"

Try this layout instead.Hope it works

<?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:paddingBottom="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">

<TextView
    android:id="@+id/subject_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Subject"
    android:textColor="@color/black"
    android:textSize="18sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/subject_name"
    android:gravity="end"
    android:orientation="vertical"
    android:weightSum="1">

    <ViewSwitcher
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/subject_name"
        android:layout_marginTop="8dp"
        android:layout_weight="0.9">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal">

            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:text="@string/please_wait" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"></android.support.v7.widget.RecyclerView>
    </ViewSwitcher>

    <Button
        android:id="@+id/ok_Btn"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        android:layout_below="@id/switcher"
        android:layout_weight="0.1"
        android:background="@drawable/blue_button"
        android:text="ok"
        android:textColor="#fff" />
</LinearLayout>

Upvotes: 3

Zeero0
Zeero0

Reputation: 2790

Ok Check now!

<?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:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">

<TextView
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Subject"
    android:textSize="18sp"
    android:textColor="@color/black"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/subject_name"/>

<RelativeLayout
    android:layout_below="@id/subject_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ViewSwitcher
        android:layout_marginTop="8dp"
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:paddingLeft="8dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Please wait"/>
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="10dp"
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>
    </ViewSwitcher>

    <Button
        android:layout_weight="1"
        android:layout_alignParentRight="true"
        android:id="@+id/ok_Btn"
        android:layout_below="@id/switcher"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="#369"
        android:text="OK"
        android:textColor="#fff"/>

</RelativeLayout>

</RelativeLayout>

Upvotes: 0

R.R.M
R.R.M

Reputation: 790

Try this:

<?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:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">

<TextView
    android:layout_marginLeft="15dp"
    android:layout_marginTop="15dp"
    android:text="Subject"
    android:textSize="18sp"
    android:textColor="@color/black"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/subject_name"/>

<RelativeLayout
    android:layout_below="@id/subject_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ViewSwitcher
        android:layout_marginTop="8dp"
        android:id="@+id/switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_gravity="center"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ProgressBar
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:paddingLeft="8dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/please_wait"/>
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_marginTop="10dp"
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>
    </ViewSwitcher>

    <Button
        android:layout_below="@id/switcher"
        android:layout_alignParentRight="true"
        android:id="@+id/ok_Btn"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:background="@drawable/blue_button"
        android:text="ok"
        android:textColor="#fff"/>
</RelativeLayout>

Upvotes: 0

PRIYA PARASHAR
PRIYA PARASHAR

Reputation: 777

You should maintain your weightSum and weight properly in layout.for more knowledge you may check this solution and apply like this

  <LinearLayout
        android:weightSum="2"
        android:gravity="end"
        android:layout_below="@id/subject_name"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ViewSwitcher
            android:layout_weight="1.5"
            android:layout_marginTop="8dp"
            android:layout_below="@id/subject_name"
            android:id="@+id/switcher"
            android:layout_width="match_parent"
            android:layout_height="0dp">

            <LinearLayout
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:paddingLeft="8dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/please_wait"/>
            </LinearLayout>

            <android.support.v7.widget.RecyclerView
                android:layout_marginTop="10dp"
                android:id="@+id/studentRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </android.support.v7.widget.RecyclerView>
        </ViewSwitcher>

        <Button
            android:layout_weight="0.5"
            android:layout_below="@id/switcher"
            android:layout_alignParentRight="true"
            android:id="@+id/ok_Btn"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:background="@drawable/blue_button"
            android:text="ok"
            android:textColor="#fff"/>
    </LinearLayout>

Upvotes: 1

vishal jangid
vishal jangid

Reputation: 3025

use android:layout_marginBottom="30dp" in recycle view

  <android.support.v7.widget.RecyclerView
            android:layout_marginTop="10dp"
            android:layout_marginBottom="30dp"
            android:id="@+id/studentRecycler"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </android.support.v7.widget.RecyclerView>

Upvotes: 0

Related Questions