Skip to content

Commit

Permalink
refactor+docs: use paddingRelative in AlertDialog.customView
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 25, 2024
1 parent 2a909fe commit 63486a3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
6 changes: 3 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class ModelFieldEditor :
fieldNameInput?.let { fieldNameInput ->
fieldNameInput.isSingleLine = true
AlertDialog.Builder(this).show {
customView(view = fieldNameInput, paddingLeft = 64, paddingRight = 64, paddingTop = 32)
customView(view = fieldNameInput, paddingStart = 64, paddingEnd = 64, paddingTop = 32)
title(R.string.model_field_editor_add)
positiveButton(R.string.dialog_ok) {
// Name is valid, now field is added
Expand Down Expand Up @@ -303,7 +303,7 @@ class ModelFieldEditor :
fieldNameInput.setText(fieldsLabels[currentPos])
fieldNameInput.setSelection(fieldNameInput.text!!.length)
AlertDialog.Builder(this).show {
customView(view = fieldNameInput, paddingLeft = 64, paddingRight = 64, paddingTop = 32)
customView(view = fieldNameInput, paddingStart = 64, paddingEnd = 64, paddingTop = 32)
title(R.string.model_field_editor_rename)
positiveButton(R.string.rename) {
if (uniqueName(fieldNameInput) == null) {
Expand Down Expand Up @@ -347,7 +347,7 @@ class ModelFieldEditor :
fieldNameInput?.let { fieldNameInput ->
fieldNameInput.setRawInputType(InputType.TYPE_CLASS_NUMBER)
AlertDialog.Builder(this).show {
customView(view = fieldNameInput, paddingLeft = 64, paddingRight = 64, paddingTop = 32)
customView(view = fieldNameInput, paddingStart = 64, paddingEnd = 64, paddingTop = 32)
title(text = String.format(resources.getString(R.string.model_field_editor_reposition), 1, fieldsLabels.size))
positiveButton(R.string.dialog_ok) {
val newPosition = fieldNameInput.text.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class CustomStudyDialog(
val dialog =
AlertDialog
.Builder(requireActivity())
.customView(view = v, paddingLeft = 64, paddingRight = 64, paddingTop = 32, paddingBottom = 32)
.customView(view = v, paddingStart = 64, paddingEnd = 64, paddingTop = 32, paddingBottom = 32)
.positiveButton(R.string.dialog_ok) {
// Get the value selected by user
val n =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class AddNewNotesType(
AlertDialog
.Builder(activity)
.apply {
customView(dialogView, paddingLeft = 32, paddingRight = 32, paddingTop = 64, paddingBottom = 64)
customView(dialogView, paddingStart = 32, paddingEnd = 32, paddingTop = 64, paddingBottom = 64)
positiveButton(R.string.dialog_ok) { _ ->
val newName =
dialogView.findViewById<EditText>(R.id.notetype_new_name).text.toString()
Expand Down
18 changes: 15 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/utils/AlertDialogFacade.kt
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,24 @@ fun AlertDialog.getCheckBoxPrompt(): CheckBox =
"CheckBox prompt is not available. Forgot to call AlertDialog.Builder.checkBoxPrompt()?"
}

/**
* Sets a custom view for the dialog.
*
* @param view the view to display in the dialog
* @param paddingStart the start padding in pixels
* @param paddingTop the top padding in pixels
* @param paddingEnd the end padding in pixels
* @param paddingBottom the bottom padding in pixels
*
* @see [AlertDialog.Builder.setView]
* @see [View.setPaddingRelative]
*/
fun AlertDialog.Builder.customView(
view: View,
paddingTop: Int = 0,
paddingBottom: Int = 0,
paddingLeft: Int = 0,
paddingRight: Int = 0,
paddingStart: Int = 0,
paddingEnd: Int = 0,
): AlertDialog.Builder {
val container = FrameLayout(context)

Expand All @@ -213,7 +225,7 @@ fun AlertDialog.Builder.customView(
FrameLayout.LayoutParams.WRAP_CONTENT,
)

container.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)
container.setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom)
container.addView(view, containerParams)
setView(container)

Expand Down

0 comments on commit 63486a3

Please sign in to comment.