diff --git a/core/src/main/java/com/afollestad/materialdialogs/customview/DialogCustomViewExt.kt b/core/src/main/java/com/afollestad/materialdialogs/customview/DialogCustomViewExt.kt index 26a9d16cc..152d5f244 100644 --- a/core/src/main/java/com/afollestad/materialdialogs/customview/DialogCustomViewExt.kt +++ b/core/src/main/java/com/afollestad/materialdialogs/customview/DialogCustomViewExt.kt @@ -43,12 +43,15 @@ internal const val CUSTOM_VIEW_NO_VERTICAL_PADDING = "md.custom_view_no_vertical * @param view The view to insert as the custom view. * @param scrollable Whether or not the custom view is automatically wrapped in a ScrollView. * @param noVerticalPadding When set to true, vertical padding is not added around your content. + * @param horizontalPadding When true, 24dp horizontal padding is applied to your custom view. + * @param dialogWrapContent When true, the dialog will wrap the content width. */ fun MaterialDialog.customView( @LayoutRes viewRes: Int? = null, view: View? = null, scrollable: Boolean = false, noVerticalPadding: Boolean = false, + horizontalPadding: Boolean = false, dialogWrapContent: Boolean = false ): MaterialDialog { assertOneSet("customView", view, viewRes) @@ -62,7 +65,8 @@ fun MaterialDialog.customView( this.view.contentLayout.addCustomView( res = viewRes, view = view, - scrollable = scrollable + scrollable = scrollable, + horizontalPadding = horizontalPadding ) .also { if (dialogWrapContent) { diff --git a/core/src/main/java/com/afollestad/materialdialogs/internal/message/DialogContentLayout.kt b/core/src/main/java/com/afollestad/materialdialogs/internal/message/DialogContentLayout.kt index f63f20eda..0e50abf8e 100644 --- a/core/src/main/java/com/afollestad/materialdialogs/internal/message/DialogContentLayout.kt +++ b/core/src/main/java/com/afollestad/materialdialogs/internal/message/DialogContentLayout.kt @@ -62,6 +62,10 @@ class DialogContentLayout( get() = parent as DialogLayout private var scrollFrame: ViewGroup? = null private var messageTextView: TextView? = null + private var useHorizontalPadding: Boolean = false + private val frameHorizontalMargin: Int by lazy { + resources.getDimensionPixelSize(R.dimen.md_dialog_frame_margin_horizontal) + } var scrollView: DialogScrollView? = null var recyclerView: DialogRecyclerView? = null @@ -109,7 +113,8 @@ class DialogContentLayout( fun addCustomView( @LayoutRes res: Int?, view: View?, - scrollable: Boolean + scrollable: Boolean, + horizontalPadding: Boolean ): View { check(customView == null) { "Custom view already set." } @@ -120,10 +125,21 @@ class DialogContentLayout( } if (scrollable) { + // Since the view is going in the main ScrollView, apply padding to custom view. + this.useHorizontalPadding = false addContentScrollView() customView = view ?: inflate(res!!, scrollFrame) - scrollFrame!!.addView(customView) + scrollFrame!!.addView(customView?.apply { + if (horizontalPadding) { + updatePadding( + left = frameHorizontalMargin, + right = frameHorizontalMargin + ) + } + }) } else { + // Since the view is NOT going in the main ScrollView, we'll offset it in the layout. + this.useHorizontalPadding = horizontalPadding customView = view ?: inflate(res!!) addView(customView) } @@ -190,7 +206,11 @@ class DialogContentLayout( continue } currentChild.measure( - makeMeasureSpec(specWidth, EXACTLY), + if (currentChild == customView && useHorizontalPadding) { + makeMeasureSpec(specWidth - (frameHorizontalMargin * 2), EXACTLY) + } else { + makeMeasureSpec(specWidth, EXACTLY) + }, makeMeasureSpec(heightPerRemainingChild, AT_MOST) ) totalChildHeight += currentChild.measuredHeight @@ -210,11 +230,20 @@ class DialogContentLayout( for (i in 0 until childCount) { val currentChild = getChildAt(i) val currentBottom = currentTop + currentChild.measuredHeight + val childLeft: Int + val childRight: Int + if (currentChild == customView && useHorizontalPadding) { + childLeft = frameHorizontalMargin + childRight = measuredWidth - frameHorizontalMargin + } else { + childLeft = 0 + childRight = measuredWidth + } currentChild.layout( - 0, - currentTop, - measuredWidth, - currentBottom + /*left= */childLeft, + /*top= */currentTop, + /*right= */childRight, + /*bottom= */currentBottom ) currentTop = currentBottom } diff --git a/sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt b/sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt index 9f25b421b..d0956b761 100644 --- a/sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt +++ b/sample/src/main/java/com/afollestad/materialdialogssample/MainActivity.kt @@ -821,7 +821,7 @@ class MainActivity : AppCompatActivity() { private fun showCustomViewDialog(dialogBehavior: DialogBehavior = ModalDialog) { val dialog = MaterialDialog(this, dialogBehavior).show { title(R.string.googleWifi) - customView(R.layout.custom_view, scrollable = true) + customView(R.layout.custom_view, scrollable = true, horizontalPadding = true) positiveButton(R.string.connect) { dialog -> // Pull the password out of the custom view when the positive button is pressed val passwordInput: EditText = dialog.getCustomView() diff --git a/sample/src/main/res/layout/custom_view.xml b/sample/src/main/res/layout/custom_view.xml index bea8a1cac..d0c1cab49 100644 --- a/sample/src/main/res/layout/custom_view.xml +++ b/sample/src/main/res/layout/custom_view.xml @@ -5,8 +5,6 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" - android:paddingLeft="@dimen/md_dialog_frame_margin_horizontal" - android:paddingRight="@dimen/md_dialog_frame_margin_horizontal" >