Using ConstraintLayout with custom Android Dialog - android

I'm creating a custom Dialog by extending Dialog and using setContentView(). The problem is when I try to show the Dialog it only shows the background shadow instead of my custom layout. I've managed to get the custom layout to show by wrapping it in a RelativeLayout, but I'm wondering if there is a better solution or something I'm doing wrong. Using constraintLayout version 1.0.2. Thanks!
Here is my Custom Dialog:
class EventsFilterDialog(context: Context): Dialog(context) {
init {
setContentView(R.layout.dialog_events_filter)
setCancelable(true)
}
}
Here is a simplified version of my R.layout.dialog_events_filter that has the exact same result:
<?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:background="#color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/headerTv"
android:text="#string/events_filters"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
android:gravity="center"
android:background="#color/colorPrimaryLight"
android:padding="16dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<View
android:id="#+id/middleView"
android:layout_width="0dp"
android:layout_height="256dp"
app:layout_constraintTop_toBottomOf="#+id/headerTv"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/closeTv"
android:text="#string/close"
android:textColor="#color/white"
android:textAllCaps="true"
android:textStyle="bold"
android:gravity="center"
android:background="#color/colorPrimaryLight"
android:layout_width="0dp"
android:layout_height="48dp"
app:layout_constraintTop_toBottomOf="#+id/middleView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="#+id/applyTv" />
<TextView
android:id="#+id/applyTv"
android:text="#string/apply_filter"
android:textColor="#color/white"
android:textAllCaps="true"
android:textStyle="bold"
android:gravity="center"
android:background="#color/colorAccent"
android:layout_width="0dp"
android:layout_height="48dp"
app:layout_constraintTop_toBottomOf="#+id/middleView"
app:layout_constraintLeft_toRightOf="#+id/closeTv"
app:layout_constraintRight_toRightOf="parent"/>
</android.support.constraint.ConstraintLayout>

Set LayoutParams of window of the dialog at run time. This one worked for me.
dialog.setContentView(R.layout.dialog_select_location);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
Best of luck!

It seems to be working if you set a width on the parent ConstraintLayout instead of using match_parent. That's the best solution I was able to find.

Related

What is the right way to add margin to this views?

I have a problem.
I want to add background to a group of buttons and text views. the thing is that I want the background to have a margin over the inputs so it doesn't look bad.
I can't make the views inside a container layout and then add background to him because I need the views in the same layout as all the other views. So, I did a View with a background and constrained it to the desired views.
How can I add margin to this background over the views? without adding margin manually to each of the views.
Maybe there is another option other than creating a View with a background?
how it looks like
how I want it
thanks in advance
Edit:
Forgot to add the code. Here it is:\
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#drawable/edit_add_card_group_shape"
app:layout_constraintStart_toStartOf="#id/tvContact"
app:layout_constraintEnd_toEndOf="#id/etPhoneNumber"
app:layout_constraintTop_toTopOf="#id/btAreaCode"
app:layout_constraintBottom_toBottomOf="#id/btAddPhone" />
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/etPhoneNumber"
style="#style/Widget.App.EditText.TextSize"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:autofillHints=""
android:hint="#string/phone_number"
android:inputType="phone"
android:layout_marginEnd="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/guideline3"
app:layout_constraintTop_toBottomOf="#id/tvContact" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btAreaCode"
style="#style/Widget.App.Button.TextButton.RegularColor"
android:layout_width="0dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:drawableEnd="#drawable/ic_baseline_arrow_forward_ios_24"
android:text="#string/phone"
android:textAlignment="textStart"
app:layout_constraintBottom_toBottomOf="#id/etPhoneNumber"
app:layout_constraintEnd_toStartOf="#+id/etPhoneNumber"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/tvContact"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/etPhoneNumber" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btAddPhone"
style="#style/Widget.App.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/add_phone_number"
android:textAlignment="textStart"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/etPhoneNumber2" />
</androidx.constraintlayout.widget.ConstraintLayout>
Use android:padding in your ConstraintLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:padding="12dp">
</androidx.constraintlayout.widget.ConstraintLayout>

How to make dialog larger without using absolute values?

I have an activity that I want to appear as a dialog and not a separate screen. When I do so, it gets squished as small as possible. I would expect something between the two images below.
Is there a way to have the dialog fill a certain percentage of the screen? I'd prefer to stay away from defining static/absolute values for sizing things. As far as I can guess, the layout is trying to match parent, but the dialog is trying to wrap contents, which results in this small dialog.
EDIT: Here's the XML for the activity:
<?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"
android:padding="8dp"
tools:context=".EditPlayerActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player name"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/playerNameEntry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter player's name"
android:inputType="textCapWords"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/textView"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="#+id/playerNameEntry"
app:layout_constraintTop_toBottomOf="#+id/playerNameEntry" />
<Button
android:id="#+id/cancelButton"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:onClick="cancelEdit"
android:text="Cancel"
app:layout_constraintEnd_toStartOf="#+id/saveButton"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/colorScroll" />
<Button
android:id="#+id/saveButton"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:onClick="saveEdit"
android:text="Save"
app:layout_constraintBottom_toBottomOf="#+id/cancelButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="#+id/cancelButton"
app:layout_constraintTop_toTopOf="#+id/cancelButton" />
<HorizontalScrollView
android:id="#+id/colorScroll"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2">
<LinearLayout
android:id="#+id/colorScrollContents"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp" />
</HorizontalScrollView>
<androidx.constraintlayout.widget.Barrier
android:id="#+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="right"
tools:layout_editor_absoluteX="371dp"
tools:layout_editor_absoluteY="8dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
you make an use of windowmanager
dialog = new Dialog //declaration
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.loading_screen);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.CENTER;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_BLUR_BEHIND;
window.setAttributes(wlp);
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);//full screen
dialog.show();
I had the exact same problem. I replaced the LinearLayout parent of the DialogFragment with RelativeLayout and everything worked perfectly.

Recyclerview in DialogFragment

I'm using following code to create xml layout resource.
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="#dimen/dp48"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:fontFamily="#font/trebuchet"
android:text="#string/select_a_folder"
android:textColor="?attr/colorAccent"
android:textSize="16sp"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintBottom_toTopOf="#+id/recyclerViewFolderList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewFolderList"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="350dp"
android:maxHeight="350dp"
app:layout_constraintBottom_toTopOf="#+id/imgViewClose2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
<com.google.android.material.button.MaterialButton
android:id="#+id/imgViewClose2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/close"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:src="#drawable/ic_baseline_close_24dx"
android:text="#string/cancel"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recyclerViewFolderList"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
And when I load recyclerview with few items I'm getting expected output,
But if I load near 50 items, recyclerview fills the whole view, so my title and cancel button missing.
If I set the layout_height="0dp" for recyclerview, recyclerview totally gone like following.
I don't want to fix the size of recyclerview; if I fix it, it will work, but its always fixed, even if I pass only 2 items empty space will be there.
So I want the recyclerview should be wrap_content and also title and button should be visible.
This simple change makes it works.
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewFolderList"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintBottom_toTopOf="#+id/imgViewClose2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView1" />
Thanks to all those who all tried to help me.
I had this issue. Eventually I gave up on using ConstraintLayout here, and went back to RelativeLayout - easy and worked like a charm.
ConstraintLayout is great, but sometimes the best solution is old school.
After adding app:layout_constraintHeight_default="wrap" in my recyclerView constraints started working
use this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/roundable_bg">
<android.support.v7.widget.RecyclerView
android:id="#+id/wv_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bt_ok"
android:background="#android:color/white"
/>
<TextView
android:id="#+id/bt_ok"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:padding="12dp"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:background="#drawable/button_selector"
android:gravity="center"
android:textColor="#android:color/black"
android:text="#string/ok" />
</RelativeLayout>
try using minWidth, minHeight and maxWidth, maxHeight on the root layout of the dialog.
In your Recyclerview
set
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
remove
app:layout_constraintHeight_max="350dp"
android:maxHeight="350dp"
I think setting android:layout_height="match_parent" to the ConstraintLayout will fix your problem.
As of the documentation app:layout_constraintHeight_default="wrap" is deprecated. Use the below instead:
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

ImageView inside ConstraintLayout does not work

I have a problem with correct display of ImageView. I want to display ImageView inside ConstraintLayout. On preview it looks exactly as i need, but when i'm starting it on device it looks completly dirrerent. This layout is places inside recycle view. What is wrong with this code?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/promotionRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp"
android:background="#fff"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/promotionImageLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintHeight_default="spread"
android:background="#color/colorPrimary">
<ImageView
android:id="#+id/promotionImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#mipmap/ic_start_promotion"
android:background="#mipmap/ic_start_promotion"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHeight_min="150dp" />
<ImageView
android:id="#+id/fadeGradientImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#drawable/fade_image_background" />
<TextView
android:text="Sample title"
android:textSize="16sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="6dp"
android:textColor="#ffffff"
android:id="#+id/promotionNameTextView"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:paddingBottom="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
<TextView
android:id="#+id/promotionDescriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="13sp"
android:layout_marginTop="12dp"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:text="Sampe description" />
</LinearLayout>
EDIT: Deep explanation:
I want to create row for RecycleView. Each row have to contains image, title and description. Title have to be in the left bottom corner of the image. Description have to be below the image. After that i have to put gradient (black in the bottom) into the image. Screen with "Preview" is exactly what i need.
EDIT2: Everything with this layout is ok. It is working as expected, i forget that i made some changes in kotlin code... Sory for problem.
First things first, every view should apply the attribute rules of its parent ViewGroup. ConstraintLayout doesn't support match_parent. It supports the 0dp value which means "match constraint". This way the view will expand to fill the constraint bounded space.
Next, ConstraintLayout was created to achieve a flat view hierarchy for better layout performance. So, never nest it inside a LinearLayout as it has the chains feature to get the same behavior in a more flexible way. Plus, you can achieve the structure with a ConstraintLayout at the top level .
Another thing, If you are going to define the same margin in all directions, you can just use layout_margin.
Finally, you have overdraw problems. ConstraintLayout is flexible enough to allow us to position views as backgrounds and help us avoid overlapped backgrounds.
Here's a solution:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/promotionRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp">
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="#+id/promotion_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/promotion_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#mipmap/ic_start_promotion"
app:layout_constraintHeight_min="150dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/shadow"
android:layout_width="0dp"
android:layout_height="80dp"
android:adjustViewBounds="true"
android:background="#drawable/fade_image_background"
app:layout_constraintBottom_toBottomOf="#+id/promotion_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/promotion_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="Sample title"
android:textColor="#android:color/white"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/promotion_image"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:padding="12dp"
android:text="Sampe description"
android:textSize="13sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/promotion_image" />
</android.support.constraint.ConstraintLayout>
Try it. Hope this helps!
First of all, trying to write more presicely what do you want? Display image view inside layout it's somthing common words. As for your code, beside you don't have any constraints. You have strange view height, for second ImageView:
android:layout_height="match_parent"
It may overlay all other children view, it's very strange parameter.

Android: BottomSheetDialog does not expand completely with ConstraintLayout

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:
Set state of BottomSheetDialogFragment to expanded
BottomSheetDialogFragment - How to set expanded height (or min top offset)
Android BottomSheetDialogFragment does not expand completely
Any help will be appreciated. Thanks!
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".

Categories

Resources