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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#00000000"
android:centerColor="#FFFFFF"
android:endColor="#00000000"
android:angle="270" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
<shape>
<gradient
android:startColor="#00CFCCCE"
android:centerColor="#FFFFFF"
android:endColor="#00BAB8B9"
android:angle="270" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#00CFCCCE"
android:centerColor="#FFFFFF"
android:endColor="#00BAB8B9"
android:angle="270" />
</shape>
</item>
</selector>
Set the backgroud of your buttons to this drawable.
b.setBackground(getResources().getDrawable(R.drawable.button_dark_gradient));
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