Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Buttons with equal width in Constraint Layout

Tags:

How can I get two buttons side by side in ConstraintLayout with fixed and equal length?

Something like this:

enter image description here

like image 466
bhavya_karia Avatar asked Jul 03 '18 12:07

bhavya_karia


People also ask

How to use constraint layout?

To know about constraint layout you can refer Constraint layout . Step 1: Use constraint layout in your application. Step 2: Click on the icon shown below or you can also search horizontal or vertical guidelines in palette. Step 3: Select guidelines which you want to use (horizontal or vertical).

How to make a linearlayout equal to the size of button?

As you can see in LinearLayout we have set, android:weightSum=”2″ and then in each of the button we set android:layout_weight=”1″ to make those of equal size, and also another important is android:layout_width=”0dip” The media could not be loaded, either because the server or network failed or because the format is not supported.

What is the difference between greater than equal and optional constraints?

A required, greater-than-or-equal constraint defines the minimum distance between that control and the layout guide, while an optional constraint tries to pull the control to exactly 20.0 points from the layout guide. Both constraints are satisfiable for the taller constraint, so the system places it exactly 20.0 points from the layout guide.

How do I constrain a view to a percentage width or height?

In Constraint Layout 1.1 it’s been made simpler by allowing you to easily constrain any view to a percentage width or height. Isn’t this fantastic? All views support layout_constraintWidth_percent and layout_constraintHeight_percent attributes. These will cause the constraint to be fixed at a percentage of the available space.


1 Answers

Try this code..

<?xml version="1.0" encoding="utf-8"?>
<androidx.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:text="Button 1"
    app:layout_constraintLeft_toLeftOf="parent"
    android:textAllCaps="false"
    app:layout_constraintRight_toLeftOf="@+id/guideline"
    />

<androidx.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5" />

<Button
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/button2"
    android:text="Button 2"
    android:textAllCaps="false"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/guideline"
    />
 </androidx.support.constraint.ConstraintLayout>
like image 115
Android Team Avatar answered Sep 28 '22 01:09

Android Team