2

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?

Aswin P Ashok
  • 702
  • 8
  • 27

5 Answers5

3

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>

Sharath kumar
  • 4,064
  • 1
  • 14
  • 20
  • Is there any way I can keep the button size 30dp fixed? – Aswin P Ashok Sep 19 '17 at 08:08
  • I did this like you posted, but button was kinda big, so i fixed button size as 30dp and it works. But isn't it a dirty fix? Am I overriding the weight aspect by setting fixed size? – Aswin P Ashok Sep 19 '17 at 08:19
  • I concept of weight is to give specific height or width for view to occupy in every different screens.May be you should try to reduce button weight and check. – Sharath kumar Sep 19 '17 at 08:42
  • 2
    Try like this..remove weightsum from Linearlayout,add layout_weight to ViewSwitcher as 1,then add height of button to 30dp.If you find my answer satisfactory please accept it. – Sharath kumar Sep 19 '17 at 08:44
1

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>
PRIYA PARASHAR
  • 777
  • 4
  • 15
0

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>
vishal jangid
  • 2,967
  • 16
  • 22
0

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>

R.R.M
  • 780
  • 4
  • 10
  • I tried something like this before.Now the button is not visible. So I added Linear layout with weight to make it somewhat visible – Aswin P Ashok Sep 19 '17 at 07:23
0

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>
Zeero0
  • 2,602
  • 1
  • 21
  • 36