I want to set my button width with percentage value. I searched and read that I should using ConstrainLayout
, But when I using app:layout_constraintWidth_percent
instead of layout_width
for my width of button the application crashed.
The code
<?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">
<ScrollView
android:id="@ id/sv_intro_main_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@ id/BottomNavigation_main_id"
android:layout_centerHorizontal="true"
android:background="#E6E6E6"
android:padding="20sp"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="-16dp">
<androidx.cardview.widget.CardView
android:id="@ id/cv_buyButtons_main_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="10sp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/CL_buyButtons_main_id"
android:layout_width="match_parent"
android:layout_height="150sp"
tools:layout_editor_absoluteX="20dp"
tools:layout_editor_absoluteY="20dp">
<Button
android:id="@ id/button_fly_main_id"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_height="50sp"
android:fontFamily="@font/samim_bold"
android:text="@string/fly"
android:textAlignment="textStart"
android:textColor="#191919"
android:textSize="16sp"
app:icon="@drawable/ic_plane_svgrepo_com"
app:iconGravity="end"
app:iconSize="40sp"
app:iconTint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/button_train_main_id"
app:layout_constraintWidth_percent="0.5"/>
<Button
android:id="@ id/button_train_main_id"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_height="50sp"
android:fontFamily="@font/samim_bold"
android:text="@string/train"
android:textAlignment="textStart"
android:textColor="#191919"
android:textSize="16sp"
app:icon="@drawable/ic_train_svgrepo_com"
app:iconGravity="end"
app:iconSize="40sp"
app:iconTint="#000000"
app:layout_constraintEnd_toStartOf="@ id/button_fly_main_id"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</ScrollView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@ id/BottomNavigation_main_id"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentBottom="true"
android:background="#FFFFFF"
app:layout_constraintBottom_toBottomOf="@ id/sv_intro_main_id"
tools:layout_editor_absoluteX="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error log
2022-03-22 21:42:41.793 6096-6096/com.masjedsafa.ali_baba_appearance E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.masjedsafa.ali_baba_appearance, PID: 6096
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.masjedsafa.ali_baba_appearance/com.masjedsafa.ali_baba_appearance.MainActivity}: android.view.InflateException: Binary XML file line #33 in com.masjedsafa.ali_baba_appearance:layout/activity_main: Binary XML file line #33: You must supply a layout_width attribute.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: android.view.InflateException: Binary XML file line #33 in com.masjedsafa.ali_baba_appearance:layout/activity_main: Binary XML file line #33: You must supply a layout_width attribute.
Caused by: java.lang.UnsupportedOperationException: Binary XML file line #33: You must supply a layout_width attribute.
at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:831)
at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:8230)
at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:8428)
at androidx.constraintlayout.widget.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2910)
at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1934)
at androidx.constraintlayout.widget.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:486)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1129)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:1130)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1088)
at android.view.LayoutInflater.inflate(LayoutInflater.java:686)
at android.view.LayoutInflater.inflate(LayoutInflater.java:538)
at android.view.LayoutInflater.inflate(LayoutInflater.java:485)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.masjedsafa.ali_baba_appearance.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:8051)
at android.app.Activity.performCreate(Activity.java:8031)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210)
at android.os.Handler.dispatchMessage(Handler.java:106)
2022-03-22 21:42:41.793 6096-6096/com.masjedsafa.ali_baba_appearance E/AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7839)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
I try ConstraintLayout
and I was expecting it be worked.
CodePudding user response:
Add this attribute to your widget which you want to define width with constraintWidth_percent:
android:layout_width="0dp"
Each element should have both layout_width
and layout_height
. When you want to make width or height dynamic and dependent on other elements, you should set the element's related property to "0dp"
.