Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove extra space around GridView programmatically

I am trying to make a GridView programmatically in my java class and it all works fine. The problem is the auto-generated 5 pixel padding around the GridView. In the xml I manage to remove it using:

android:listSelector="@null"

But I do not manage to do anything similar in java. I have tried some workarounds like making the GridView 10 pixels larger then the actual screen with no luck.

Does anyone have any code for this?

Edit:

The answer by me does not solve the problem. There is still a bounty going. Here is my GridView code:

    GridView gridView = new GridView(this);
    gridView.setNumColumns(someInt);
    gridView.setAdapter (new MyCustomAdapter(this));
    gridView.setLayoutParams(new GridView.LayoutParams(
            customValue,
            LayoutParams.FILL_PARENT,
            Gravity.CENTER_HORIZONTAL)
    );
like image 202
Magakahn Avatar asked Aug 27 '12 13:08

Magakahn


2 Answers

  • One way to ensure the padding appears same on screens with different density is by converting it to DIP units.

    int padding = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, -5, 
                                        getResources().getDisplayMetrics());
    
  • Other thing you can try is to define a null drawable in xml..

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <drawable name="null_drawable">@null</drawable>
        ...
    </resources>
    

    Then call setSelector(R.drawable.null_drawable);

Update:

  • Define your GridView in its own xml and inflate it.

    layout/mygrid.xml

    <?xml version="1.0" encoding="utf-8"?>
    <GridView 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:listSelector="@null"/>
    

    In java,

    GridView gridView = (GridView)inflater.inflate(R.layout.mygrid, null);
    gridView.setLayoutParams(new GridView.LayoutParams(customValue, 
                            LayoutParams.FILL_PARENT));
    gridView.setNumColumns(someInt);
    gridView.setAdapter (new MyCustomAdapter(this));
    
like image 72
Ronnie Avatar answered Nov 09 '22 21:11

Ronnie


Solved it sort of by:

    gridView.setPadding(-5, 0, -5, 0);

But you need different padding for different screens. But it is not a complete solution. Functional modifications of this code will be accepted.

like image 40
Magakahn Avatar answered Nov 09 '22 22:11

Magakahn