I'm trying to create a grid layout containing buttons but by default there is a space between these buttons and I don't need that. The .xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center">
<HorizontalScrollView android:id="@+id/HorizontalScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</GridLayout>
</HorizontalScrollView>
</ScrollView>
The code where I create the buttons and add it to the grid layout:
for (int rowCounter = 0; rowCounter < DIMENSION; rowCounter++)
for (int columnCounter = 0; columnCounter < DIMENSION; columnCounter++) {
Button b = new Button(this);
b.setText(" ");
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.setMargins(0, 0, 0, 0);
b.setLayoutParams(params);
gridLayout.addView(b);
}
Here is an image of how it looks:
Android default button have some padding.
If you don't want that space, you need create a custom backgroud for your buttons.
This is an example:
button_dark_gradient.xml
Set the backgroud of your buttons to this drawable.