1
0

More than 1 year has passed since last update.

[Android] 簡単にSnackBarの背景色を変えてみる

Posted at

初めに

今回SnackBarの背景色を変更したい時に悩むことがあったのでここにメモとして残そうと思う。

Screenshot_20220617-154458 (1).png

動作の内容

//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()

参考資料

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0