Reputation: 2912
I am using ConstraintLayout for my layout. I am creating a BottomSheetDialog and setting the layout to it which uses the ConstraintLayout at top.
Note: If i use LinearLayout everything works fine and BottomSheetDialog takes proper height but when i am using ConstraintLayout, it shows only one option.
Please find the screenshot below when i am using ConstraintLayout:
My xml file:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<TextView
android:id="@+id/tv_other_methods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:gravity="center_horizontal"
android:padding="@dimen/dp_12"
android:text="@string/from_other_methods"
android:textColor="@color/blue"
android:textSize="@dimen/sp_17" />
<View
android:id="@+id/view_other_methods"
android:layout_width="match_parent"
android:layout_height="@dimen/dip_1"
android:background="@color/light_grey"
app:layout_constraintBottom_toBottomOf="@id/tv_other_methods" />
<TextView
android:id="@+id/tv_FromChinaUnionPay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:gravity="center_horizontal"
android:padding="@dimen/dp_12"
android:text="@string/from_china_union_pay"
android:textColor="@color/blue"
android:textSize="@dimen/sp_17"
app:layout_constraintBottom_toBottomOf="@id/view_other_methods"
app:layout_constraintTop_toTopOf="@id/view_FromChinaUnionPay" />
<View
android:id="@+id/view_FromChinaUnionPay"
android:layout_width="match_parent"
android:layout_height="@dimen/dip_1"
android:background="@color/light_grey"
app:layout_constraintBottom_toBottomOf="@id/tv_FromChinaUnionPay" />
<TextView
android:id="@+id/tv_Cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:gravity="center_horizontal"
android:padding="@dimen/dp_12"
android:text="@string/cancel"
android:textColor="@color/blue"
android:textSize="@dimen/sp_17"
app:layout_constraintTop_toBottomOf="@+id/view_FromChinaUnionPay" />
</android.support.constraint.ConstraintLayout>
My java code for BottomSheetDialog:
final BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(mContext);
bottomSheetDialog.setContentView(R.layout.dialog_web_view_options);
// dialog.setCancelable(false);
final Window window = bottomSheetDialog.getWindow();
assert window != null;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setLayout(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
window.getDecorView().setPadding(20, 0, 20, 0);
bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
FrameLayout bottomSheet = bottomSheetDialog.findViewById(android.support.design.R.id.design_bottom_sheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setSkipCollapsed(true);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
In my layout if i replaceConstraintLayout with LinearLayout then it works fine and takes full height. Please let me know if i am doing something wrong.
I have tried these solutions but nothing worked:
Any help will be appreciated. Thanks!
Upvotes: 4
Views: 5219
Reputation: 6346
You haven't constrained all the views - the first TextView
has no constraints whatsoever . The constraints you added aren't properly specified to correctly display the views in vertical order. I suggest creating a vertical chain of all the views with the parent ConstraintLayout's
layout_height
set to wrap_content
. The XML should look something like 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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<TextView
android:id="@+id/tv_other_methods"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:gravity="center_horizontal"
android:padding="@dimen/dp_12"
android:text="@string/from_other_methods"
android:textColor="@color/blue"
android:textSize="@dimen/sp_17"
app:layout_constraintBottom_toTopmOf="@id/view_other_methods"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/view_other_methods"
android:layout_width="0dp"
android:layout_height="@dimen/dip_1"
android:background="@color/light_grey"
app:layout_constraintBottom_toTopOf="@id/tv_FromChinaUnionPay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_other_methods" />
<TextView
android:id="@+id/tv_FromChinaUnionPay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:gravity="center_horizontal"
android:padding="@dimen/dp_12"
android:text="@string/from_china_union_pay"
android:textColor="@color/blue"
android:textSize="@dimen/sp_17"
app:layout_constraintBottom_toTopOf="@id/view_FromChinaUnionPay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/view_other_methods" />
<View
android:id="@+id/view_FromChinaUnionPay"
android:layout_width="0dp"
android:layout_height="@dimen/dip_1"
android:background="@color/light_grey"
app:layout_constraintBottom_toTopOf="@id/tv_Cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_FromChinaUnionPay" />
<TextView
android:id="@+id/tv_Cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/ripple"
android:gravity="center_horizontal"
android:padding="@dimen/dp_12"
android:text="@string/cancel"
android:textColor="@color/blue"
android:textSize="@dimen/sp_17"
app:layout_constraintTop_toBottomOf="@+id/view_FromChinaUnionPay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
As a side note, it is not advised to use match_parent
for any views inside a ConstraintLayout
as the documentation suggests:
Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout. Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent".
Upvotes: 1