【Android】スナックバー(Snackbar)の使い方をステップごとに解説
このチュートリアルでは、Androidアプリでスナックバー(Snackbar)を使用する方法を、サンプルコードとともに段階的に解説します。スナックバーは画面下部に短いメッセージを一時的に表示できるUIコンポーネントで、Toastとは異なり「再試行(RETRY)」などのアクションボタンを組み込めるのが大きな特徴です。
手順1:新規プロジェクトを作成する
Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。
手順2:レイアウトファイル(activity_main.xml)を編集する
res/layout/activity_main.xml に以下のコードを追加します。
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Click button below to bring up snackBar" /> <Button android:id="@+id/callbackButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_below="@id/textView" android:layout_marginTop="16sp" android:text="Tap To Display SnackBar" android:textStyle="bold" android:textSize="16sp"/> </RelativeLayout>
手順3:build.gradleに依存関係を追加する
build.gradle(Module: app)を開き、以下の依存関係を追記します。スナックバーはDesign Support Libraryに含まれているため、この宣言が必須となります。
implementation 'com.android.support:design:28.0.0'
手順4:MainActivity.javaを実装する
src/MainActivity.java に以下のコードを記述します。ボタンをタップするとスナックバーが表示され、メッセージ部分は赤色、アクションボタン「RETRY」は青色で表示されるようにカスタマイズしています。
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.callbackButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Snackbar snackBar = Snackbar .make(v, "An Error Occurred!", Snackbar.LENGTH_LONG) .setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
snackBar.setActionTextColor(Color.BLUE);
View snackBarView = snackBar.getView();
TextView textView = snackBarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.RED);
snackBar.show();
}
});
}
}
手順5:AndroidManifest.xmlを確認する
androidManifest.xml には以下のように記述します。特別な権限は不要ですが、ランチャーアクティビティの設定が正しく行われていることを確認しておきましょう。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリを実行してみよう
それでは、アプリケーションを実行してみましょう。ここでは、実機のAndroid端末がパソコンに接続されているものとして説明を進めます。Android Studioからアプリを実行するには、プロジェクト内のいずれかのアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。実行デバイスとして自分のモバイル端末を選択すると、端末に以下のような初期画面が表示されます。

-
【Android】ViewFlipperの使い方を徹底解説!ビュー切り替えアニメーションの実装方法
ViewFlipperとは?ViewFlipperは、複数の子ビューを重ねて保持し、一定間隔での自動切り替えやボタン操作による手動切り替えを、アニメーション効果付きで実現できるAndroidのウィジェットです。画像スライダー、オンボーディング画面、シンプルなカルーセルUIなどを作りたいときに非常に便利です。本記事では、ViewFlipperを使ってImageView・Button・TextViewをスライドアニメーションで切り替えるサンプルアプリを、ステップごとに詳しく解説します。Step 1:新規プロジェクトを作成するAndroid Studioを起動し、メニューから「File」→「New
-
【Android】NavigationViewの実装方法をステップごとに解説
この記事では、AndroidアプリでNavigationView(ナビゲーションビュー)を使用してドロワーメニューを実装する方法を、ステップごとに詳しく解説します。ハンバーガーアイコンから開閉できるサイドメニューは、多くのアプリで採用されている定番のUIです。ステップ1:新しいプロジェクトを作成するAndroid Studioを開き、File → New Project を選択して、必要な情報を入力し新しいプロジェクトを作成します。テンプレートには「Navigation Drawer Activity」を選ぶと、後の作業がスムーズになります。ステップ2:activity_main.xml にコ