Reputation: 3753
i want to implement some thing like below image in constraint layout
Requirements :
BTN 1
can be VISIBLE
or GONE
BTN 1
set to GONE , BTN 2
should remain width and move to centerI tried to use Chains but when BTN 1
gets GONE
BTN 2
gets full width. Then i set layout_constraintWidth_percent="0.5"
to both buttons but in this situation i can't set margin between two buttons. When is set margin , button moving out of screen.
Any idea to solve this problem?
Here is my code
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_negative"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/_24sdp"
android:background="@drawable/btn_solid_white_round"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:padding="@dimen/_8sdp"
android:textColor="@color/darkTextColor"
android:textSize="@dimen/_11ssp"
app:fontFamily="@font/iran_yekan"
app:layout_constraintEnd_toStartOf="@+id/btn_positive"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txt_message"
app:layout_constraintWidth_percent="0.5"
tools:text="BTN 2" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_positive"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/_12sdp"
android:background="@drawable/btn_solid_accent_round"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:padding="@dimen/_8sdp"
android:textColor="@color/white"
android:textSize="@dimen/_11ssp"
app:fontFamily="@font/iran_yekan"
app:layout_constraintBottom_toBottomOf="@+id/btn_negative"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn_negative"
app:layout_constraintTop_toTopOf="@+id/btn_negative"
app:layout_constraintWidth_percent="0.5"
tools:text="BTN 1" />
Upvotes: 1
Views: 1058
Reputation: 1084
Don't use percent 0.5 and margin. User percent and chain only.
<?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"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_negative"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/red"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:padding="8dp"
android:textColor="@android:color/white"
android:textSize="11sp"
android:visibility="gone"
app:layout_constraintWidth_percent="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="@id/btn_positive"
tools:text="BTN 2" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_positive"
style="?attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="@android:color/holo_green_dark"
android:ellipsize="end"
app:layout_constraintWidth_percent="0.4"
android:gravity="center"
android:maxLines="1"
android:padding="8dp"
android:textColor="@android:color/white"
android:textSize="11sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/btn_negative"
tools:text="BTN 1" />
</androidx.constraintlayout.widget.ConstraintLayout>
Upvotes: 2