簡単な通知をユーザーに出すには、Toastがありますが、似たようなポップアップ機能として Snackbar があり、こちらが推奨されています。
2021.2.1
Snackbar
SnackbarはToastと同じようにある短い期間だけ表示して消えます。ただ、設定によっては表示期間を決めたり、dismissするまで継続させたりと色々とできます。
尚、Kotlinはこちら
Snackbarの表示
SnackbarはToastと似たような設定ですが、引数としてViewを取ります。因みにToastはContextでした。Snackbarの設定は、
1 |
Snackbar.make(view, "メッセージ", Snackbar.LENGTH_SHORT).show(); |
サポートライブラリーが必要です。
1 |
import android.support.design.widget.Snackbar; |
build.gradleでは
1 |
implementation 'com.google.android.material:material:x.x.x' |
が必要です。最近のAndroid Studioではデフォルトで入っています。
尚、バージョンは適宜調整してください。
以下簡単にSnackbarをボタンタップのタイミングで表示させるコードです。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//package your.package.name; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.snackbar.Snackbar; import android.os.Bundle; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.button); // expression lambda button.setOnClickListener( v -> Snackbar.make(v, "メッセージ", Snackbar.LENGTH_SHORT).show()); } } |
レイアウトです。ボタンをおいただけです。
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout> |
strings.xml
1 2 3 4 |
<resources> <string name="app_name">Your App Name</string> <string name="button">Button</string> </resources> |
build.gradle
1 2 3 4 5 6 |
... dependencies { ... implementation 'com.google.android.material:material:1.4.0' } |
ボタンタップでSnackbarが表示されました。
Snackbarのアクション設定
Snackbarにアクションを設定してみましょう。
バーの右横にボタンのようにmessageが設定されてそれをタップするとアクションが実行されます。Buttonのような感じに使えます。
1 |
Snackbar setAction (CharSequence text, View.OnClickListener listener) |
その他にバーの背景色を変えたり、Actionの文字列色を変更するメソッドがあります。
まとめてアクションによりTextViewから応答メッセージを表示させてみます。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
//package your.package.name; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.snackbar.Snackbar; import android.graphics.Color; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView textView; private Snackbar snackbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text_view); Button button = findViewById(R.id.button); // statement lambda button.setOnClickListener( v -> { snackbar = Snackbar.make(v, R.string.ask, Snackbar.LENGTH_SHORT); snackbar.setDuration(10000); snackbar.getView().setBackgroundColor(Color.rgb(32, 125, 98)); // expression lambda snackbar.setAction("Reply", view -> textView.setText(R.string.message) ); snackbar.setActionTextColor(Color.YELLOW); snackbar.show(); }); } } |
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#dfe" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:layout_margin="20dp" android:textColor="#000" android:textSize="24sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" /> </LinearLayout> |
strings.xml
1 2 3 4 5 6 |
<resources> <string name="app_name">Your App Name</string> <string name="button">Button</string> <string name="ask">Are you Clear?</string> <string name="message">Understood !</string> </resources> |
build.gradle
1 2 3 4 5 6 7 |
... dependencies { ... implementation 'com.google.android.material:material:1.4.0' ... } |
サンプル動画
setDuration()を使って表示期間を長くしてみましたが、Actionで終了してくれます。正直、こういった通知がユーザーによっては邪魔な時があるのですぐに消す目的に使えるかもしれません。(Swipeでも消えますけど)
Reference:
Snackbar | Android Developers