Home > Software engineering >  Android Studio 3 Vertical Buttons Same Width
Android Studio 3 Vertical Buttons Same Width

Time:03-10

How do I make the 3 vertical purchase buttons that have different length titles all the same width. The titles can change. Currently I have them in a constraintlayout but as you can see the widths are different. I suspect this is easy as all the questions refer to horizontal buttons.

enter image description here

CodePudding user response:

I would personally solve the problem by adding a Linear Layout with orientation set to "Vertical.

  1. Create a Linear Layout and set constraints.
  2. Set width and height to wrap content (that means the width will be as long as the longest child).
  3. Add 3 Buttons inside the Linear Layout.
  4. Make sure you set the width of each button to "Match Parent". This will set the width of a button to the width of the parent Linear Layout.

Check this example xml code:

   <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/textView2">

       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="0.49$ / Month"/>

       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="4.99$ / Year"/>

       <Button
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="14.99$ / Forever"/>

    </LinearLayout>

I hope that my answer helped you.

  • Related