Last active
April 11, 2024 11:03
-
-
Save antoniolg/cdd070245b431b226ba3 to your computer and use it in GitHub Desktop.
Snackbar extensions on Kotlin, to create a useful small DSL.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class HomeActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_home) | |
setSupportActionBar(toolbar) | |
fab.setOnClickListener { | |
it.snack("Snack message") { | |
action("Action") { toast("Action clicked") } | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun View.snack(@IntegerRes messageRes: Int, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) { | |
snack(resources.getString(messageRes), length, f) | |
} | |
inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) { | |
val snack = Snackbar.make(this, message, length) | |
snack.f() | |
snack.show() | |
} | |
fun Snackbar.action(@IntegerRes actionRes: Int, color: Int? = null, listener: (View) -> Unit) { | |
action(view.resources.getString(actionRes), color, listener) | |
} | |
fun Snackbar.action(action: String, color: Int? = null, listener: (View) -> Unit) { | |
setAction(action, listener) | |
color?.let { setActionTextColor(color) } | |
} |
setActionTextColor needs to use a ColorInt. With the current support library, you can use this:
fun Snackbar.action(action: String, color: Int? = null, listener: (View) -> Unit) {
setAction(action, listener)
color?.let { setActionTextColor(ContextCompat.getColor(context, color)) }
}
All changes:
import android.view.View
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import com.google.android.material.snackbar.Snackbar
inline fun View.snack(@StringRes messageRes: Int, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
snack(resources.getString(messageRes), length, f)
}
inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) {
val snack = Snackbar.make(this, message, length)
snack.f()
snack.show()
}
fun Snackbar.action(@StringRes actionRes: Int, color: Int? = null, listener: (View) -> Unit) {
action(view.resources.getString(actionRes), color, listener)
}
fun Snackbar.action(action: String, color: Int? = null, listener: (View) -> Unit) {
setAction(action, listener)
color?.let { setActionTextColor(ContextCompat.getColor(context, color)) }
}
All changes:
import android.view.View import androidx.annotation.StringRes import androidx.core.content.ContextCompat import com.google.android.material.snackbar.Snackbar inline fun View.snack(@StringRes messageRes: Int, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) { snack(resources.getString(messageRes), length, f) } inline fun View.snack(message: String, length: Int = Snackbar.LENGTH_LONG, f: Snackbar.() -> Unit) { val snack = Snackbar.make(this, message, length) snack.f() snack.show() } fun Snackbar.action(@StringRes actionRes: Int, color: Int? = null, listener: (View) -> Unit) { action(view.resources.getString(actionRes), color, listener) } fun Snackbar.action(action: String, color: Int? = null, listener: (View) -> Unit) { setAction(action, listener) color?.let { setActionTextColor(ContextCompat.getColor(context, color)) } }
Thanks @webserveis
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, you should replace the
@IntegerRes
annotations with@StringRes
ones.