1

I have a layout in mind that I don't know how to create with LinearLayout or ConstraintLayout. So I'd like to write code to do it manually. The code would look at the screen size and button sizes, and compute and set the button positions. Is that possible? Where would I put that code in my Fragment subclass?

Below is the layout I'm going for. In iOS I would express it with "constraints", but I'm new to Android and don't know how to do this. If there is a way to do this without Java/Kotlin code outside the layout XML files, I'd still like to know where that custom code would go.

In the screen below, I'd like:

  1. A minimum side padding around the button text.
  2. All the buttons are the same width.
  3. If the default font is increased, so the buttons fill the screen, I can give up the side padding before to truncating the button text.

enter image description here

Rob N
  • 15,024
  • 17
  • 92
  • 165
  • I think you might want to look at this https://stackoverflow.com/questions/1851633/how-to-add-a-button-dynamically-in-android. It has quite a few examples for adding a button dynamically. It doesn't have everything you're looking for, but it's a start. – Beanjezus Jul 13 '20 at 22:20

1 Answers1

2

You can do this in XML file like this:

<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:id="@+id/mainScreen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity">


    <TextView
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="center"
        android:id="@+id/textView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Some label text, blah blah blah blah"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 1"
        app:layout_constraintBottom_toTopOf="@+id/button2"
        app:layout_constraintEnd_toStartOf="@+id/guidelineRight"
        app:layout_constraintStart_toStartOf="@+id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 2"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintEnd_toStartOf="@+id/guidelineRight"
        app:layout_constraintStart_toStartOf="@+id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@+id/button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button 3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guidelineRight"
        app:layout_constraintStart_toStartOf="@+id/guidelineLeft"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />


    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />

</androidx.constraintlayout.widget.ConstraintLayout>

And effect will look like this:

effect

You can change guidelines percent to set the width of buttons which You want. To expand them equally I use chains YT video with short explanation

iknow
  • 8,358
  • 12
  • 41
  • 68