1

I have a grid of four buttons in my layout. I have chained them horizontally, so that the space to the left, right, and between the buttons is evenly weighted.

screenshot of my app's buttons

Can I make the two rows of buttons have the same amount of space vertically between them so it doesn't look uneven? I would rather not just center them vertically like I did to achieve the horizontal spacing, which done thanks to @AdamK of this post: Evenly spacing views using ConstraintLayout

activity_main.xml:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.jason.notifier.MainActivity"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="388dp"
        android:layout_height="164dp"
        app:srcCompat="@drawable/banner"
        android:layout_marginStart="8dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent" />

    <Button
        android:id="@+id/testbutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:onClick="sampleNotification"
        android:text="Test"
        app:layout_constraintRight_toLeftOf="@+id/settingsbutton"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/historybutton" />

    <Button
        android:id="@+id/settingsbutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:text="Settings"
        android:onClick="settingsbutton"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/testbutton"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/pingbutton" />

    <Button
        android:id="@+id/pingbutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:onClick="pingbutton"
        android:text="ping"
        android:layout_marginTop="-31dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/historybutton" />

    <Button
        android:id="@+id/historybutton"
        android:layout_width="171dp"
        android:layout_height="136dp"
        android:onClick="historybutton"
        android:text="History"
        android:layout_marginTop="-31dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintRight_toLeftOf="@+id/pingbutton"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="358dp"
        android:layout_height="42dp"
        android:text="TextView"
        android:gravity="center"
        tools:layout_editor_absoluteX="2dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/imageView3" />

</android.support.constraint.ConstraintLayout>
JasonRad
  • 13
  • 3

1 Answers1

0

Place a View widget in the center of your layout with a dimension ratio of 1:1 and constrained on the left by historybutton and on the right by pingbutton. This will give you a square where each side is the length of the horizontal gap.

<View
    android:id="@+id/spacerView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="0dp"
    app:layout_constraintDimensionRatio="H,1:1"
    app:layout_constraintEnd_toStartOf="@+id/pingbutton"
    app:layout_constraintStart_toEndOf="@+id/historybutton"
    app:layout_constraintTop_toBottomOf="@+id/historybutton" />

Now constrain the bottom two buttons to the bottom of your square as follows:

<Button
    android:id="@+id/testbutton"
    android:layout_width="171dp"
    android:layout_height="136dp"
    android:text="Test"
    app:layout_constraintTop_toBottomOf="@+id/spacerView"
    app:layout_constraintEnd_toEndOf="@+id/historybutton" />

<Button
    android:id="@+id/settingsbutton"
    android:layout_width="171dp"
    android:layout_height="136dp"
    android:text="Settings"
    app:layout_constraintTop_toBottomOf="@+id/spacerView"
    app:layout_constraintStart_toStartOf="@+id/pingbutton" />

And that should do it. I made the square view visible so we could see it, but you should make it invisible. Here is an image of the effect.

enter image description here

Cheticamp
  • 61,413
  • 10
  • 78
  • 131