初めに
今回SnackBarの背景色を変更したい時に悩むことがあったのでここにメモとして残そうと思う。
動作の内容
//MainActivity.kt
val snackbar = Snackbar.make(findViewById(android.R.id.content),"タイトルが入力されていません。", Snackbar.LENGTH_SHORT)
//R.color.errorはColor.xmlを参照する。
//詳しくは下記URLまで、
snackbar.view.setBackgroundResource(R.color.error)
snackbar.show()
Color.xmlについて
動作の解説
スナックバーの作成
今回は、インスタンス化することにより、スナックバーの見た目を変更する。
val snackbar = Snackbar.make([表示される場所],[表示される文字], [表示される長さ])
背景色の変更
snackbar.view.setBackgroundResource([カラーXMLの中に書いてある色を指定する。])
スナックバーの表示
snackbar.show()
参考資料