Remove extra space around GridView programmatically
android, gridview
Solution
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));
Problem
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) ); ```