Code for a first app I made: (displpays a random ...
# codereview
t
Code for a first app I made: (displpays a random snackbar.)
Copy code
private lateinit var trueButton: Button
private lateinit var falseButton: Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        trueButton = findViewById(R.id.true_button)
        falseButton = findViewById(R.id.false_button)

        trueButton.setOnClickListener { makeSnackbar(R.string.correct_toast, it, themeConfig = RandomSnackbarConfiguration()) }
        falseButton.setOnClickListener { makeSnackbar(R.string.incorrect_toast, it, themeConfig = RandomSnackbarConfiguration()) }
    }
    fun makeSnackbar(resId: Int,
                     viewContext: View,
                     themeConfig: SnackbarThemeConfiguration = DefaultSnackbarConfiguration()) =
            Snackbar.make(viewContext, resId, Snackbar.LENGTH_SHORT)
            .setBackgroundTint(themeConfig.backgroundColor)
            .setTextColor(themeConfig.textColor).let {
                        it.setAnimationMode(BaseTransientBottomBar.ANIMATION_MODE_SLIDE)
                        it.show()
                    }
    abstract class SnackbarThemeConfiguration(val backgroundColor: Int, val textColor: Int)
    class DefaultSnackbarConfiguration() : SnackbarThemeConfiguration(Color.DKGRAY, Color.WHITE)
    class RandomSnackbarConfiguration() : SnackbarThemeConfiguration(
            Color.rgb(Random.nextInt(0,256),
                Random.nextInt(0, 256),
                Random.nextInt(0, 256)),
            Color.rgb(Random.nextInt(0,256),
                    Random.nextInt(0, 256),
                    Random.nextInt(0, 256)))
}
e
Android Lint should be warning you about context leakage
t
@ephemient what should I do instead?
@ephemient also how do I add different animations? I only figured out how to use 'BaseTransientBottomBar.Slide' and 'Fade'. thank you 🙂
b
You can improve readability by adding empty spaces between methods. Also, I suggest for you to take a look into ViewBinding for accessing view components from the layout file. Good job!! 😉 ViewBinding documentation: https://developer.android.com/topic/libraries/view-binding
🌹 1