8

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)
    );
alexgophermix
  • 4,189
  • 5
  • 32
  • 59
Magakahn
  • 498
  • 9
  • 31

7 Answers7

6
  • 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));
    
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Gets error in file on the java code, and no change on the xml. – Magakahn Sep 24 '12 at 19:17
  • You mean it crashed? or was a syntax error? wt was the error? – Ron Sep 25 '12 at 10:42
  • I have thought about that solotion, but I want to check if there were any ways of doing this 100% programmatically, without using xml since importing multiple xml into a class often is unstabel(my own experiences) – Magakahn Sep 25 '12 at 13:25
  • what problems did you face inflating? what do you mean "importing multiple xml into a class"? – Ron Sep 25 '12 at 14:09
  • I am saying that this question is about doing this programattically, and if that fails I use you solotion. – Magakahn Sep 25 '12 at 14:22
  • But I dont think there is any difference now between inflating and creating in java, as the view you will inflate is one single view(not a deep heirarchy of views, such that it makes inflating costly).. So you can go ahead with this option without any downsides. – Ron Sep 25 '12 at 14:48
  • I give this question an upvote, but I do not accept it due to that it does not answear my question completely. In other words good answear but not to solve it 100% programatically. Because of your upvotes you also get about 50 of the bounty too. It looks like I have to use this solution anyway. – Magakahn Sep 27 '12 at 13:02
2

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.

Magakahn
  • 498
  • 9
  • 31
2

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);
Community
  • 1
  • 1
Renard
  • 6,909
  • 2
  • 27
  • 23
1

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);
Chinmoy Debnath
  • 2,814
  • 16
  • 20
  • No good. I have experimented with setPadding quite a lot, and I have to set different padding for different screens to fit. And android devices has quite a lot of different screensizes. – Magakahn Sep 26 '12 at 13:40
0

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

Like setSelector(0)

marwinXXII
  • 1,456
  • 14
  • 21
0

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)
Community
  • 1
  • 1
Paul D'Ambra
  • 7,629
  • 3
  • 51
  • 96
0

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.

Eduardo
  • 214
  • 3
  • 9
  • Can you please upload your your stretchMode, smoothScrollbar and listSelector? – Magakahn Sep 27 '12 at 12:59
  • private int listSelector = android.R.color.transparent; private int numColumns = 2; private boolean smoothScrollbar = true; private int stretchMode = GridView.STRETCH_COLUMN_WIDTH; – Eduardo Oct 01 '12 at 11:49