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)
);
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));
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With