Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to change dialog buttons text #6

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -25,6 +25,8 @@ new ChromaDialog.Builder()
.initialColor(Color.GREEN)
.colorMode(ColorMode.RGB) // There's also ARGB and HSV
.onColorSelected(color -> /* do your stuff */)
.positiveButtonText("Select") // "OK" by default
.negativeButtonText("Exit") // "Cancel" by default
.create()
.show(getSupportFragmentManager(), "ChromaDialog");
```
Expand Down
25 changes: 22 additions & 3 deletions chroma/src/main/kotlin/me/priyesh/chroma/ChromaDialog.kt
Expand Up @@ -52,6 +52,8 @@ class ChromaDialog constructor() : DialogFragment() {
@ColorInt private var initialColor: Int = ChromaView.DefaultColor
private var colorMode: ColorMode = ChromaView.DefaultModel
private var listener: ColorSelectListener? = null
private var positiveButtonText: String? = null
private var negativeButtonText: String? = null

fun initialColor(@ColorInt initialColor: Int): Builder {
this.initialColor = initialColor
Expand All @@ -68,15 +70,29 @@ class ChromaDialog constructor() : DialogFragment() {
return this
}

fun positiveButtonText(positiveButtonText: String): Builder {
this.positiveButtonText = positiveButtonText
return this
}

fun negativeButtonText(negativeButtonText: String): Builder {
this.negativeButtonText = negativeButtonText
return this
}

fun create(): ChromaDialog {
val fragment = newInstance(initialColor, colorMode)
fragment.listener = listener
return fragment
return newInstance(initialColor, colorMode).apply {
this.listener = listener
this.positiveButtonText = positiveButtonText
this.negativeButtonText = negativeButtonText
}
}
}

private var listener: ColorSelectListener? = null
private var chromaView: ChromaView by Delegates.notNull<ChromaView>()
private var positiveButtonText: String? = null
private var negativeButtonText: String? = null

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
chromaView = if (savedInstanceState == null) {
Expand All @@ -92,6 +108,9 @@ class ChromaDialog constructor() : DialogFragment() {
)
}

positiveButtonText?.let { chromaView.setPositiveButtonText(it) }
negativeButtonText?.let { chromaView.setNegativeButtonText(it) }

chromaView.enableButtonBar(object : ChromaView.ButtonBarListener {
override fun onNegativeButtonClick() = dismiss()
override fun onPositiveButtonClick(color: Int) {
Expand Down
10 changes: 9 additions & 1 deletion chroma/src/main/kotlin/me/priyesh/chroma/internal/ChromaView.kt
Expand Up @@ -19,8 +19,8 @@ package me.priyesh.chroma.internal
import android.content.Context
import android.graphics.Color
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.RelativeLayout
import me.priyesh.chroma.ColorMode
Expand Down Expand Up @@ -92,4 +92,12 @@ internal class ChromaView : RelativeLayout {
}
}
}

internal fun setPositiveButtonText(positiveButtonText: String) {
(findViewById(R.id.positive_button) as Button).text = positiveButtonText
}

internal fun setNegativeButtonText(negativeButtonText: String) {
(findViewById(R.id.negative_button) as Button).text = negativeButtonText
}
}