Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to place view below RecyclerView

I'm having trouble placing an Adview below a RecyclerView. Currently the RecyclerView is taking up the rest of the layout and the Adview isn't being shown. None of the solutions I have found online have solved my issue. Usually layout_weight or layout_below solves the problem but it isn't in this case.

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#7c7c7c"
        android:focusable="true"
        android:focusableInTouchMode="true">


        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relContainer"
            android:background="@drawable/bordershadow2"
            android:paddingBottom="17dp"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="10dp" />


        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/recycler_view"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>

        <RelativeLayout
            android:id="@+id/relContainer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bordershadow"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

           <TableLayout
                android:id="@+id/purchaseTableLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingBottom="7dp"
                android:paddingTop="7dp"
                android:layout_alignParentTop="true"
                android:stretchColumns="0,1,2">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:weightSum="3">

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:paddingLeft="2dp"
                        android:singleLine="true"
                        android:textColor="#000"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:paddingLeft="2dp"
                        android:singleLine="true"
                        android:textColor="#000"
                        android:textSize="24sp"
                        android:textStyle="bold" />

                    <Spinner
                        android:id="@+id/typeSpinner"
                        style="style/Theme.Material"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:gravity="center_vertical"
                        android:entries="@array/type_array"
                        android:textSize="@dimen/title_size" />
                </TableRow>
            </TableLayout>

            <EditText
                android:id="@+id/searchEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/purchaseTableLayout"
                android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'() "
                android:hint="Search"
                android:imeOptions="actionDone"
                android:singleLine="true" />

            <TableLayout
                android:id="@+id/tableLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/searchEditText"
                android:paddingBottom="7dp"
                android:paddingTop="7dp"
                android:stretchColumns="0,1,2">

                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:weightSum="3.0">

                    <Spinner
                        android:id="@+id/sortRaceSpinner"
                        style="style/Theme.Material"
                        android:paddingTop="5dp"
                        android:paddingBottom="10dp"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:entries="@array/sort_race_array"
                        android:textSize="@dimen/title_size" />

                    <Spinner
                        android:id="@+id/sortAffinitySpinner"
                        style="style/Theme.Material"
                        android:paddingTop="5dp"
                        android:paddingBottom="10dp"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:entries="@array/sort_affinity_array"
                        android:textSize="@dimen/title_size" />

                    <Spinner
                        android:id="@+id/sortSpinner"
                        style="style/Theme.Material"
                        android:paddingTop="5dp"
                        android:paddingBottom="10dp"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1.0"
                        android:entries="@array/sort_array"
                        android:textSize="@dimen/title_size" />
                </TableRow>
            </TableLayout>
        </RelativeLayout>
    </RelativeLayout>
</RelativeLayout>
like image 613
M0rty Avatar asked Mar 01 '16 01:03

M0rty


4 Answers

I had a view that needed to be under a recycler view with dynamic size. meaning that the view supposes to move down as the RecyclerView grew.

my solution was to add paddingBottom to the RecyclerView and then negative marginTop in the same value to the view, it worked perfectly.

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:paddingBottom="48dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

        <TextView
            android:layout_marginTop="-48dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
</LinearLayout>
like image 75
Yosef Avatar answered Oct 07 '22 06:10

Yosef


I rewrote your layout file, The recycleview is wrapped inside a linearlayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
   android:id="@+id/wrapper"
   android:layout_width="match_parent"
   android:layout_height="60dp"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:layout_marginBottom="12dp"
   android:orientation="vertical">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:clipToPadding="false"
       android:paddingBottom="20dp"
       android:paddingLeft="@dimen/activity_horizontal_margin"
       android:paddingRight="@dimen/activity_horizontal_margin"
       android:paddingTop="10dp" />

   </LinearLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="60dp"
    android:layout_below="@+id/wrapper"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>


<RelativeLayout
    android:id="@+id/relContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/adView"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TableLayout
        android:id="@+id/purchaseTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:layout_alignParentTop="true"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Spinner
                android:id="@+id/typeSpinner"
                style="style/Theme.Material"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical" />
        </TableRow>
    </TableLayout>

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/purchaseTableLayout"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'() "
        android:hint="Search"
        android:imeOptions="actionDone"
        android:singleLine="true" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchEditText"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3.0">

            <Spinner
                android:id="@+id/sortRaceSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortAffinitySpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

The result is as show

enter image description here

If your recycleview content will extend beyond the display height then you should consider dividing your layout into partitions with android:layout_weight.

Update

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/relContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TableLayout
        android:id="@+id/purchaseTableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:layout_alignParentTop="true"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3">

            <TextView
                android:id="@+id/textView1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/textView2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:paddingLeft="2dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textSize="24sp"
                android:textStyle="bold" />

            <Spinner
                android:id="@+id/typeSpinner"
                style="style/Theme.Material"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical" />
        </TableRow>
    </TableLayout>

    <EditText
        android:id="@+id/searchEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/purchaseTableLayout"
        android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'() "
        android:hint="Search"
        android:imeOptions="actionDone"
        android:singleLine="true" />

    <TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/searchEditText"
        android:paddingBottom="7dp"
        android:paddingTop="7dp"
        android:stretchColumns="0,1,2">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:weightSum="3.0">

            <Spinner
                android:id="@+id/sortRaceSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortAffinitySpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0"/>

            <Spinner
                android:id="@+id/sortSpinner"
                style="style/Theme.Material"
                android:paddingTop="5dp"
                android:paddingBottom="10dp"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1.0" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

<LinearLayout
    android:id="@+id/wrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relContainer"
    android:background="@color/colorPrimary"
    android:orientation="vertical">

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

</LinearLayout>

<LinearLayout
    android:id="@+id/wrappers"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_alignParentBottom="true"
    android:orientation="vertical">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

</LinearLayout>

enter image description here

like image 28
Inducesmile Avatar answered Oct 07 '22 06:10

Inducesmile


Try adding the android:clipToPadding="false" attribute to your RecyclerView.

The official documentation says, about clipToPadding:

Sets whether this ViewGroup will clip its children to its padding and resize (but not clip) any EdgeEffect to the padded region, if padding is present.

By default, children are clipped to the padding of their parent ViewGroup. This clipping behavior is only enabled if padding is non-zero.


Your XML layout would end up being:

layout.xml

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/relContainer"
    android:background="@drawable/bordershadow2"
    android:clipToPadding="false"
    android:paddingBottom="20dp"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="10dp" />
like image 43
Evin1_ Avatar answered Oct 07 '22 06:10

Evin1_


You can do it other way, put the Adview on the recycler at the bottom and then in the adapter add an empty ViewHolder which will be always your last element, you can use for it an empty layout with the height of Adview.

like image 41
Eduard Albu Avatar answered Oct 07 '22 06:10

Eduard Albu