4

I have some buttons I've created using gridview and a custom adapter, but the buttons are surrounded by a padding which I believe to be part of the gridview. I found this thread and tried the solutions but was unable to fix the problem. Here is a picture of the app:

enter image description here

XML code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1"
    android:orientation="vertical"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:background="@drawable/background">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.4" />
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.6">
        <GridView
            android:id="@+id/mainMenuGridview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:numColumns="2"
            android:listSelector="@null"
            android:verticalSpacing="0dp"
            android:horizontalSpacing="0dp" />
    </LinearLayout>
</LinearLayout>

Adapter code:

class CustomAdapter : BaseAdapter
{
    private readonly Context context;
    private int numRows = 2;

    public CustomAdapter(Context c)
    {
        context = c;
    }

    public override int Count
    {
        get { return buttonTexts.Length; }
    }

    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }

    public override long GetItemId(int position)
    {
        return 0;
    }

    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        Button button;

        if (convertView == null)
        {
            // if it's not recycled, initialize some attributes
            button = new Button(context);
        }
        else
        {
            button = (Button)convertView;
        }

        button.LayoutParameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent);

        button.SetHeight(Convert.ToInt32((Main_Menu.SCREEN_HEIGHT - Main_Menu.ACTIONBAR_HEIGHT) * 0.6) / numRows);
        button.Text = buttonTexts[position];
        return button;
    }

    private readonly string[] buttonTexts =
    {
        "Button 1",
        "Button 2",
        "Button 3",
        "Button 4"
    };
}

Any help would be greatly appreciated!

Ryalis
  • 147
  • 1
  • 15
  • Can you post your adapter code and your full xml, please? – Marc Jun 15 '17 at 21:48
  • @Marc Added as requested. – Ryalis Jun 15 '17 at 21:53
  • Curious as to why you're resetting the layout parameters and height. Try putting those in the if block. Leave the button.Text = ... where it is. Also try deleting the button.SetHeight line. – Marc Jun 15 '17 at 22:00
  • 1
    I don't believe that's a padding. This seems to be the "space" all Android buttons have by default which is part of the background. To confirm this change the button background to any color: `button.SetBackgroundColor(Android.Graphics.Color.Gray);` – pinedax Jun 15 '17 at 22:11
  • @Marc the layout parameters are for the buttons, not the gridview. The SetHeight is required or the button would look like [this](http://imgur.com/iFzO7HZ) – Ryalis Jun 15 '17 at 22:29
  • 1
    @apineda Thank you! That was it. Do you mind putting this as an answer so that I can mark the question as answered? – Ryalis Jun 15 '17 at 22:38

2 Answers2

2

I don't believe that's a padding. This seems to be the "space" all Android buttons have by default which is part of the background.

To confirm this change the button background to any color: button.SetBackgroundColor(Android.Graphics.Color.Gray);

pinedax
  • 9,246
  • 2
  • 23
  • 30
0

in Xamarin giving padding to Layouts is like this:

<StackLayout Padding="0,20,0,0"></StackLayout>

but the XML is not for Xamarin

Meikiem
  • 1,876
  • 2
  • 11
  • 19