Add extra space to bottom of a GridView or ListView
Asked Answered
D

5

6

Is it possible to add extra space (kinda like an empty row) to the bottom of a GridView?

I would like it so when you scroll down to the bottom of a GridView, there will be an extra 50dp of empty space. I tried setting paddingBottom to 50dp, but it didn't seem to change anything.

Denitadenitrate answered 12/4, 2013 at 3:46 Comment(1)
please put your xml or screen shot.Pau
O
14

If I understand you right, it should look like this:

For that an easy solution is mentioned here by Maciej: How to add extra space inside at the bottom of a GridView

You just need to add the following in your GridView Layout:

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="50dp"
android:clipToPadding="false"/>

The most important part is the clipToPadding attribute which has to be set to false.

You can also see the according blog post from Google, where this solution is mentioned: https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M

Ordinance answered 12/9, 2014 at 7:19 Comment(6)
FYI, duplicate answers are not preferable. Rather you should post a single answer and give that link for the reference wherever required.Gastropod
I haven't duplicate the answer itself from the original one. I adjusted it, I gave credit to the owner and I'm showing this answer here, because it fits better then the other ones.Ordinance
my mean of duplicate answers was you have posted same answers with same contents on Stackoverflow!Gastropod
I understand you, but what exactly should I do then, if there are similar questions out there, but for me the best solution to that was given by Maciej. Just posting the Link only leads to a comment, not to an answer. In my opinion it is always better to post the answer itself, together with the credits, so users have it easier to find the best fitting answer for themself and don't have to jump from one page to another.Ordinance
The best solution as per the SO rules and as per the best practices is post a single answer and give a link of that answer in a comment on particular thread!Gastropod
Thank you for the great answer! the image in the link did a better job of showing the goal than the animation... Thanks for this post, regardless of what SO Karen saysBrother
E
1

You can realize own GridViewAdapter extends ArrayAdapter and:

@Override
public int getCount() {
    return gridArray.size();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //...
    if (getCount()-1 == position) {
        convertView.setPadding(0, 0, 0, 50);
    } else {
        convertView.setPadding(0, 0, 0, 0);;
    }
    return convertView;
}

where 50 is bottom padding in pixels see View.setPadding(int, int, int, int)

and you GridItemView layout mast be android:layout_height="wrap_content"

Entice answered 26/9, 2013 at 7:58 Comment(0)
A
0

Try adding paddingBottom to the container its inside of instead. PaddingBottom on the listview won't work because the listview would add it after its last element, which won't be on screen. It kind of pretends that it has infinite height.

Another solution would be to use a RelativeLayout and shove a view with a fixed height but no graphical elements inbetween the bottom and the GridView, with the GridView set to layout_above that view.

Accord answered 12/4, 2013 at 3:50 Comment(0)
M
0

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:orientation="vertical" >

        <GridView
            android:id="@+id/gridUsers"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clipChildren="true"
            android:columnWidth="100dp"
            android:fastScrollEnabled="true"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:scrollbars="none"
            android:stretchMode="columnWidth" >
        </GridView>

        <TextView
            android:id="@android:id/empty"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="#f1f1f1"
            android:textSize="15sp"
            android:textStyle="bold"
            android:visibility="gone" >
        </TextView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linlaProgressBar"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:orientation="horizontal" >
    </LinearLayout>

</LinearLayout>

I use this in an app where the LinearLayout with the id of linlaProgressBar serves as a container for a ProgressBar when loading more data.

This example shows a GridView but replacing that with a ListView will achieve the same result.

Marriott answered 12/4, 2013 at 3:51 Comment(0)
P
0

With a ListView you would add a footer, unfortunately there's no support for that in GridView. I would suggest that adding an empty row with the height that you want is the cleanest solution.

Palliate answered 12/4, 2013 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.