13

I need help to add a space inside a GridView at the bottom of it. This Space should be below the last Element of the GridView, inside of it. This space shouldn't work like a margin to the next element, it should be only visible, when the user scrolles to the bottom of the GridView. The reason for this is an ad banner which partly covers the bottom of the GridView. Besides this obstruction, the user should still be able to see the whole content of the GridView, that's why the space at the bottom of the GridView is needed.

left: ad (blue) covers part of GridView elements (orange); right: ad covers the space in the bottom of the GridView left: ad (blue) covers part of GridView elements (orange); right: ad covers the space in the bottom of the GridView

same approach but with space at the bottom

Example, how it will look like, just imagine that the space is at the bottom, not at the top.

So far I tried to work with the Padding and Marging Variables for Bottom, but they are not the right variables for the problem. I also searched through stackoverflow, I found some similar questions like: Add extra space to bottom of a GridView or ListView or: Adding a footer View to a multi-column GridView in Android?. But the solutions doesn't seem to fit my case and furthermore I am searching for a solution inside the layout file and not inside the source code (if there is any of course).

Your help is very much appreciated.

Community
  • 1
  • 1
Elementary
  • 2,153
  • 2
  • 24
  • 35

5 Answers5

30

To achieve that you want you need to add a bottom padding to your GridView but also disable clipToPadding behavior. Try the XML code below:

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

You can also do it from code if you want. The benefit is that you can compute the offset dynamically in code:

gridView.setPadding(int left, int top, int right, int bottom);
gridView.setClipToPadding(false);

Note: Without disabling the clipToPadding behavior you will end up with persistent empty area on the bottom of your GridView, so disabling it is very important.


Bonus: Here is also a nice link about using clipToPadding parameter in ListView or GridView: https://plus.google.com/+AndroidDevelopers/posts/LpAA7q4jw9M

Maciej Ciemięga
  • 10,125
  • 1
  • 41
  • 48
  • Thanks I didn't tried the clipToPadding before, I will give it a try. – Elementary Sep 11 '14 at 13:47
  • Sadly, this doesn't works for me. There is still no extra space inside the GridView at the botton. The covering ad still obstructs content of the GridView after scrolling to the bottom of it. – Elementary Sep 11 '14 at 13:53
  • 1
    This have to work... Please see the link posted in my edited answer. You must have done something wrong. I'm using it myself in lot of my apps. Maybe you need to just set bigger padding or you are overriding this somewhere (in code for example). – Maciej Ciemięga Sep 11 '14 at 13:57
  • Thanks, I'm reading the blog post now, and will have a look if some code overwrites the setted behaviour. – Elementary Sep 11 '14 at 13:59
  • According to the blog post the approach seems to be the right one, so I'm now searching through my code why it doesn't work. :) – Elementary Sep 11 '14 at 14:05
  • 1
    I found my mistake, thanks for you solution, it absolutely works for me. :) – Elementary Sep 11 '14 at 14:48
  • No problem:) BTW. Can you tell what was the mistake? It can be helpful to other people that maybe will have similar problem in future. – Maciej Ciemięga Sep 11 '14 at 15:50
  • Nothing important, I implemented the changes on the wrong layout. I did my tests with the tablet layout but I did the implementation in the phone layout. ;) – Elementary Sep 12 '14 at 06:22
  • I posted the essence of your answer together with credits to you on two older similar questions, because your answer fits best in my opinion to this problem. – Elementary Sep 12 '14 at 07:50
  • Brilliant. Thanks. – dazed Nov 03 '16 at 20:39
  • Thanks bro, you saved my time for searching. – Tony Wu May 01 '18 at 09:50
0

Make custom gridview and in getview() method, use view which make space like you want.

Pankaj
  • 833
  • 12
  • 35
  • I'm searching for an easier solution with the existing GridView, if possible. – Elementary Sep 11 '14 at 13:42
  • Without custom gridview, it's not possible. You should at least do some effort :) – Pankaj Sep 11 '14 at 13:43
  • Of course I did put and will put effort in it. And of course in the end you can always make a custom control. But why should I do that, as long as there is a possibility for an easier solution with the existing control? – Elementary Sep 11 '14 at 13:46
  • :) Every pre-defined control have some specific behaviour only, even you can't expect everything from any predefined controls, that's why list or grid layout's provided a custom adapter. Use them instead of looking for easy way. Those are easy too. Hope you understand. – Pankaj Sep 11 '14 at 13:50
  • Like I said, you can make everything custom, but many times this effort is more pain then gain, because often there exists an easier workaround you can use. The question is just some minutes old, why not giving it some more time to find out if other users might know an answer. ;) – Elementary Sep 11 '14 at 13:57
  • I agree with you if don't have any deadline :) – Pankaj Sep 11 '14 at 14:08
  • 1
    Hello Pankaj, the solution by Maciej works for me as it should. For this no changes in custom control or changes in code are needed, just two parameters in layout. :) – Elementary Sep 11 '14 at 14:51
0

This is what I had so far:

In your Activity:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid);

        final TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setVisibility(View.GONE);

        final GridView grid = (GridView) findViewById(R.id.gridViewCustom);


        grid.setOnScrollListener(new OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem,
                    int visibleItemCount, int totalItemCount) {
                if((firstVisibleItem + visibleItemCount) == totalItemCount){//it means you are at the end of the gridview
                    txt.setVisibility(View.VISIBLE);
                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 50);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
                    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
                    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);

                    RelativeLayout.LayoutParams paramsGrid = (RelativeLayout.LayoutParams) grid.getLayoutParams();
                    paramsGrid.addRule(RelativeLayout.ABOVE, txt.getId());

                    txt.setLayoutParams(params);
                    grid.setLayoutParams(paramsGrid);

                }
            }
        });
    }

.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"
    tools:context=".MainActivity" >

   <GridView
       android:id="@+id/gridViewCustom"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_above="@+id/textView1"
       android:layout_alignParentRight="true"
       android:layout_margin="4dp"
       android:columnWidth="80dp"
       android:gravity="center"
       android:numColumns="auto_fit"
       android:stretchMode="columnWidth" >
   </GridView>

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="50dp"
       android:layout_alignParentBottom="true"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:text=""
       android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>
Metehan
  • 729
  • 5
  • 22
  • I see that this is a possible solution, but first I want to see if I can get the solution of @Maciej to work. – Elementary Sep 11 '14 at 14:35
  • I took the solution from Maciej, because with that you just need to add two parameters in the layout of the Grid, that's it. No workaround and no extra source code needed. – Elementary Sep 11 '14 at 14:52
0

To get the following result GridView with a space above the button

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_submit"
        style="@style/Base.Widget.AppCompat.Button.Colored"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/gv_someGrid"
        android:layout_margin="10dp"
        android:layout_alignParentBottom="true"
        android:text="submit" />

    <GridView
        android:id="@+id/gv_someGrid"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/btn_submit" />
</RelativeLayout>

The trick is to declare Gridview at bottom of that button then add constraint layout_above="@+id/your_id"

Elathan
  • 31
  • 1
  • 6
-3
<GridView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:marginBottom="50dp"
    android:clipToPadding="false"/>