Reputation: 599
I'm trying to make a layout using a ConstraintLayout
as parent
which has three buttons
like in the image below.
The xml layout file looks like this:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@+id/splash_facebook_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="15dp"/>
<Button
android:id="@+id/splash_sign_in_btn"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn" />
<Button
android:id="@+id/splash_sign_up_btn"
android:layout_width="180dp"
android:layout_height="45dp"
android:layout_marginEnd="15dp"
app:layout_constraintBottom_toBottomOf="@id/splash_sign_in_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.995"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/splash_sign_in_btn"
app:layout_constraintTop_toTopOf="@id/splash_sign_in_btn"
app:layout_constraintVertical_bias="0.0" />
I want to do this without setting fixed values for the two buttons on bottom.
I know I can achieve this by changing to LinearLayout
and using layout_weight
but I want to do this while having a ConstraintLayout
as parent.
Is there a way to do this?
Upvotes: 0
Views: 807
Reputation: 199
Remove the fixed values, add constraints to those buttons: left and right and set their width to 0dp. here's the image here's the result:
<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">
<Button
android:id="@+id/splash_facebook_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="continua cu cancer frate" />
<Button
android:id="@+id/splash_sign_in_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toStartOf="@id/splash_sign_up_btn"
app:layout_constraintStart_toStartOf="@id/splash_facebook_btn"
app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn"
tools:text="intra aici frate" />
<Button
android:id="@+id/splash_sign_up_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
app:layout_constraintEnd_toEndOf="@id/splash_facebook_btn"
app:layout_constraintStart_toEndOf="@id/splash_sign_in_btn"
app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn"
tools:text="Creeaza cont frate" />
PS: I added tools:text so that there'll be some text in the Android Studio preview. PSS: Good luck frate
Upvotes: 1
Reputation: 26
Use a Guideline
like:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Button
android:id="@+id/splash_facebook_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="15dp"/>
<Button
android:id="@+id/splash_sign_in_btn"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="12dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/guideline_vertical"
app:layout_constraintTop_toBottomOf="@id/splash_facebook_btn" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline_vertical"
android:layout_width="1dp"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5"/>
<Button
android:id="@+id/splash_sign_up_btn"
android:layout_height="0dp"
android:layout_width="0dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
app:layout_constraintBottom_toBottomOf="@id/splash_sign_in_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/guideline_vertical"
app:layout_constraintTop_toTopOf="@id/splash_sign_in_btn"
app:layout_constraintVertical_bias="0.0"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 1
Reputation: 1040
For left button set android:layout_marginEnd="8dp"
for right android:layout_marginStart="8dp"
and then programatically set their width:
public int getScreenWidth() {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size.x/2;
}
This way you'll get screen width/2
and then you can set the buttons width with this value.
If you prefer XML only, try this:
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/splash_facebook_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginTop="8dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintTop_toBottomOf="@+id/splash_facebook_btn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
>
<Button
android:id="@+id/splash_sign_in_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:layout_weight="1" />
<Button
android:id="@+id/splash_sign_up_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="8dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Upvotes: 1