Magakahn
Magakahn

Reputation: 508

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)
    );

Upvotes: 8

Views: 5880

Answers (7)

Eduardo
Eduardo

Reputation: 214

Using the following settings works for me:

public ContactContainer(Context context, AttributeSet attrs) {
    super(context, attrs);
    gallery = new GridView(context, attrs);
    gallery.setNumColumns(numColumns);
    gallery.setStretchMode(stretchMode);
    gallery.setSmoothScrollbarEnabled(smoothScrollbar);
    gallery.setSelector(listSelector);
    gallery.setGravity(Gravity.CENTER_HORIZONTAL);
    this.addView(gallery);
}

I use it in combination with this piece of xml code:

<de.ramicro.SClip.components.ContactContainer
    android:id="@+id/gridview_gallery_contacts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/contact_gallery_bottom_bar"
    android:layout_below="@id/contact_gallery_top_bar"
    android:padding="0dip"/>

I think that what you need is maybe the combination of a transparent Selector and gravity set to CENTER_HORIZONTAL.

Upvotes: 0

Paul D&#39;Ambra
Paul D&#39;Ambra

Reputation: 7824

Have you seen this question Why are there extra pixels around my Android GridView?? In that case the problem was that the image being used as a selector on the gridview had a padding.

Applying the suggested solution from that question programmatically you would set the selector

gridView.setSelector(android.R.color.transparent)

Upvotes: 0

Chinmoy Debnath
Chinmoy Debnath

Reputation: 2824

I think you can use setPadding to remove that problem while creating dynamic gridview.

int grids_height = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, (int) (Row_num * 80),
            getResources().getDisplayMetrics());
    Log.v("", "" + grids_height);
    gridview = new GridView(this);
    gridview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            grids_height));
    gridview.setPadding(0, 3, 0, 0);
    gridview.setBackgroundColor(Color.TRANSPARENT);
    gridview.setNumColumns(7);
    gridview.setVerticalSpacing(1);
    gridview.setHorizontalSpacing(1);
    gridview.setGravity(Gravity.CENTER_HORIZONTAL);
    gridview.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);

Upvotes: 1

Ron
Ron

Reputation: 24235

  • 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));
    

Upvotes: 6

marwinXXII
marwinXXII

Reputation: 1446

setSelector(int) (from AbsListView) isn't it what you are looking for?

Like setSelector(0)

Upvotes: 0

Renard
Renard

Reputation: 6929

To get the same effect like using android:listSelector="@null", but in code you need to use setSelector() as marvinXXII mentioned. But you need to pass in a valid ResourceId or use the setSelector(Drawable sel) variant. Unfortunatly its not possible to pass null into that method as that will lead to a NullPointerException. The workaround which is described here is to use this:

gridView.setSelector(android.R.color.transparent);

Upvotes: 2

Magakahn
Magakahn

Reputation: 508

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.

Upvotes: 2

Related Questions