Androidで通知の背景色を変更する方法|RemoteViewsによるカスタム通知レイアウトの実装手順
はじめに
この記事では、Androidの通知(Notification)の背景色を変更する方法を、サンプルコードとともにわかりやすく解説します。ポイントは、システム標準の通知スタイルを使わず、RemoteViewsで独自のカスタムレイアウトを通知に適用することです。カスタムレイアウト内で android:background 属性を指定すれば、通知の背景色や文字色を自由にデザインできます。なお、このサンプルではテキストを横方向に流す「マーキー表示」もあわせて実装しています。
ステップ1:Android Studioで新規プロジェクトを作成
Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目を入力して、新しいプロジェクトを作成しましょう。
ステップ2: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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:onClick="createNotification"
android:text="create notification" />
</RelativeLayout>
ステップ3:res/layout/custom_notification_layout.xml にコードを追加
通知用のカスタムレイアウトファイルを作成し、以下のコードを記述します。背景色を変更する鍵となるのは、ルート要素のRelativeLayoutに指定した android:background="@color/colorAccent" です。また、テキスト部分では ellipsize="marquee" によってマーキー表示を有効にしています。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:background="@color/colorAccent"
android:padding="10dp">
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_marginEnd="10dp"
android:contentDescription="@string/app_name"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/image"
android:text="Testing"
android:textColor="#000"
android:textSize="13sp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_toEndOf="@id/image"
android:ellipsize="marquee"
android:singleLine="true"
android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
android:textColor="#000"
android:textSize="13sp" />
</RelativeLayout>
ステップ4:src/MainActivity.java にコードを追加
アクティビティに以下のコードを記述します。RemoteViewsでカスタムレイアウトを読み込み、NotificationCompat.Builderにセットすることで、独自デザインの通知を表示できます。Android 8.0(API 26)以降では、通知チャンネルの作成が必要になる点にも注意してください。
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void createNotification(View view) {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContent(contentView);
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
}
ステップ5:AndroidManifest.xml にコードを追加
マニフェストファイルに以下のコードを記述します。サンプルでは振動の権限(VIBRATE)も宣言しています。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<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(実行)」アイコンをクリックしてください。デバイス選択ダイアログで自分のスマートフォンを選択すると、端末にアプリの初期画面が表示されます。
ボタンをタップすると、カスタムレイアウトが適用された通知が表示され、指定した背景色(colorAccent)と、マーキーで流れるテキストを確認できます。

-
Androidのキーボードを変更する方法を徹底解説!機種別の手順とおすすめアプリもご紹介
Android OSの大きな魅力のひとつは、自由度の高いカスタマイズ性です。その代表格ともいえるのが、スマートフォンの初期搭載(ストック)キーボードを好みのものへ変更できる機能です。この変更は、Samsung、Google、Huawei、Xiaomiなど、端末のブランドを問わず行うことができます。多くの純正キーボードは十分に優秀ですが、ユーザーのニーズによっては別のキーボードが必要になることもあります。例えば、他の言語で入力したい場合や、数式で数学記号を使いたい場合など、標準キーボードが対応していない機能が必要なケースです。Androidキーボードの変更手順は基本的にどの端末でも共通しています
-
【Android】Snapchatの通知音を変更する4つの方法を徹底解説
Snapchatは、Z世代を中心に絶大な人気を誇るSNSアプリです。カメラ撮影や写真フィルター、音声通話、ビデオ通話、チャットといった多彩な機能をひとつのアプリで完結できることから、「オールインワン」と呼ばれるほど多くのユーザーに愛用されています。さらに「スナップストリーク(Streak)」機能を利用すれば、毎日写真や動画を送り合うことで友だちとのつながりを楽しく維持できます。ただし、スナップが届くたびに通知バーから音が鳴るため、ストリークを続けている方の中には「通知音が何度も鳴って気になる」「デフォルトの音に飽きた」と感じている方も多いのではないでしょうか。そこで本記事では、Androidス