Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
Add horizontal padding boolean parameter to customView(...), resolves
Browse files Browse the repository at this point in the history
…#1829
  • Loading branch information
afollestad committed Jul 2, 2019
1 parent 6dc7c61 commit b993754
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -62,7 +65,8 @@ fun MaterialDialog.customView(
this.view.contentLayout.addCustomView(
res = viewRes,
view = view,
scrollable = scrollable
scrollable = scrollable,
horizontalPadding = horizontalPadding
)
.also {
if (dialogWrapContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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." }

Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions sample/src/main/res/layout/custom_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
>

<TextView
Expand Down

0 comments on commit b993754

Please sign in to comment.