sir-haver
sir-haver

Reputation: 3592

Android: create space-around (equal space) between elements and borders

I have two imageView widgets and I would like to place them so that there is equal space between the left border and the left image, the left image and the right image, and the right image and the right border. (I wrote in the title of the question space-around because it's similar to the spacing used in CSS...)

Is it possible to achieve without manual adaptation (such as calculating margins)? And is it possible to achieve solely in the design mode without writing xml code?

Screen shot: enter image description here

My XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:background="@drawable/newbackground"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnRoll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#3498db"
        android:text="@string/btnRoll"
        android:textColor="#ffffff"
        tools:layout_editor_absoluteX="167dp"
        tools:layout_editor_absoluteY="621dp" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="@+id/imageView3"
        app:srcCompat="@drawable/dice1"
        tools:layout_editor_absoluteX="59dp" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/dice2"
        tools:layout_editor_absoluteX="267dp"
        tools:layout_editor_absoluteY="406dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 1159

Answers (2)

Bruno
Bruno

Reputation: 4007

Here is a proposition with a LinearLayout

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />
  <ImageView ... />
  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />
  <ImageView ... />
  <View
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_weight="1" />
  <ImageView ...
</LinearLayout>

With the layout_weight attribute, the "spacer" views will take the maximum of free space, equally

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69681

Try This

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
        android:background="@drawable/newbackground"
        tools:context=".MainActivity">

    <Button
            android:id="@+id/btnRoll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#3498db"
            android:text="btnRoll"
            android:textColor="#ffffff"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

    <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@id/btnRoll"
            app:layout_constraintEnd_toStartOf="@+id/imageView3"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:srcCompat="@drawable/blue_heart" />

    <ImageView
            android:id="@+id/imageView3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:contentDescription="@string/app_name"
            app:layout_constraintBottom_toTopOf="@id/btnRoll"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/imageView2"
            app:srcCompat="@drawable/blue_heart" />
</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Upvotes: 2

Related Questions