Two buttons alert dialog
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
package org.koitharu.kotatsu.base.ui.dialog
|
||||
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.view.LayoutInflater
|
||||
import androidx.annotation.DrawableRes
|
||||
import androidx.annotation.StringRes
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.view.isVisible
|
||||
import com.google.android.material.button.MaterialButton
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import org.koitharu.kotatsu.databinding.DialogTwoButtonsBinding
|
||||
|
||||
class TwoButtonsAlertDialog private constructor(
|
||||
private val delegate: AlertDialog
|
||||
) : DialogInterface by delegate {
|
||||
|
||||
fun show() = delegate.show()
|
||||
|
||||
class Builder(context: Context) {
|
||||
|
||||
private val binding = DialogTwoButtonsBinding.inflate(LayoutInflater.from(context))
|
||||
|
||||
private val delegate = MaterialAlertDialogBuilder(context)
|
||||
.setView(binding.root)
|
||||
|
||||
fun setTitle(@StringRes titleResId: Int): Builder {
|
||||
binding.title.setText(titleResId)
|
||||
return this
|
||||
}
|
||||
|
||||
fun setTitle(title: CharSequence): Builder {
|
||||
binding.title.text = title
|
||||
return this
|
||||
}
|
||||
|
||||
fun setIcon(@DrawableRes iconId: Int): Builder {
|
||||
binding.icon.setImageResource(iconId)
|
||||
return this
|
||||
}
|
||||
|
||||
fun setPositiveButton(
|
||||
@StringRes textId: Int,
|
||||
listener: DialogInterface.OnClickListener,
|
||||
): Builder {
|
||||
initButton(binding.button1, DialogInterface.BUTTON_POSITIVE, textId, listener)
|
||||
return this
|
||||
}
|
||||
|
||||
fun setNegativeButton(
|
||||
@StringRes textId: Int,
|
||||
listener: DialogInterface.OnClickListener? = null
|
||||
): Builder {
|
||||
initButton(binding.button2, DialogInterface.BUTTON_NEGATIVE, textId, listener)
|
||||
return this
|
||||
}
|
||||
|
||||
fun create(): TwoButtonsAlertDialog {
|
||||
val dialog = delegate.create()
|
||||
binding.root.tag = dialog
|
||||
return TwoButtonsAlertDialog(dialog)
|
||||
}
|
||||
|
||||
private fun initButton(
|
||||
button: MaterialButton,
|
||||
which: Int,
|
||||
@StringRes textId: Int,
|
||||
listener: DialogInterface.OnClickListener?,
|
||||
) {
|
||||
button.setText(textId)
|
||||
button.isVisible = true
|
||||
button.setOnClickListener {
|
||||
val dialog = binding.root.tag as DialogInterface
|
||||
listener?.onClick(dialog, which)
|
||||
dialog.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
56
app/src/main/res/layout/dialog_two_buttons.xml
Normal file
56
app/src/main/res/layout/dialog_two_buttons.xml
Normal file
@@ -0,0 +1,56 @@
|
||||
<?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"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="?dialogPreferredPadding">
|
||||
|
||||
<ImageView
|
||||
android:id="@android:id/icon"
|
||||
android:layout_width="32dp"
|
||||
android:layout_height="32dp"
|
||||
android:layout_gravity="center_horizontal"
|
||||
app:tint="?colorPrimary"
|
||||
tools:src="@drawable/ic_notification" />
|
||||
|
||||
<TextView
|
||||
android:id="@android:id/title"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginVertical="18dp"
|
||||
android:gravity="center_horizontal"
|
||||
android:textAlignment="center"
|
||||
android:textAppearance="@style/TextAppearance.Material3.LabelLarge"
|
||||
android:textSize="18sp"
|
||||
tools:text="@string/suggestions_summary" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@android:id/button1"
|
||||
style="@style/Widget.Material3.Button.TonalButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="62dp"
|
||||
android:textAllCaps="true"
|
||||
android:visibility="gone"
|
||||
app:shapeAppearance="?shapeAppearanceCornerMedium"
|
||||
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Material3.Corner.Top"
|
||||
tools:text="Enable"
|
||||
tools:visibility="visible" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@android:id/button2"
|
||||
style="@style/Widget.Material3.Button.TonalButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="62dp"
|
||||
android:textAllCaps="true"
|
||||
android:visibility="gone"
|
||||
app:shapeAppearance="?shapeAppearanceCornerMedium"
|
||||
app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Material3.Corner.Bottom"
|
||||
tools:text="No thanks"
|
||||
tools:visibility="visible" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -448,4 +448,6 @@
|
||||
<string name="cancel_all">Cancel all</string>
|
||||
<string name="downloads_wifi_only">Download only via Wi-Fi</string>
|
||||
<string name="downloads_wifi_only_summary">Stop downloading when switching to a mobile network</string>
|
||||
<string name="enable">Enable</string>
|
||||
<string name="no_thanks">No thanks</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user