【Android】通知に大きなアイコンを表示する方法をサンプルコード付きで解説
この記事では、Androidの通知(Notification)で小さなアイコンの代わりに大きなアイコン(Large Icon)を表示する方法を、サンプルコードとともにわかりやすく解説します。通知の見た目をカスタマイズしたい方は、ぜひ参考にしてください。
手順1:Android Studioで新規プロジェクトを作成する
まず、Android Studioを開き、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。
手順2:res/layout/activity_main.xml にコードを追加する
次に、レイアウトファイルに以下のコードを追加します。ここでは、タップすると通知を作成するボタンを1つ配置しています。
<?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:src/MainActivity.java にコードを追加する
続いて、MainActivityに以下のコードを追加します。ポイントは setLargeIcon() メソッドです。BitmapFactory.decodeResource() でリソースからBitmapをデコードし、それを大きなアイコンとして設定することで、通知に大きなアイコンが表示されるようになります。
package app.tutorialspoint.com.notifyme;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
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) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContentTitle("Notification Title");
mBuilder.setContentText("Content to be added");
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground));
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());
}
}
補足:通知チャンネルについて
Android 8.0(APIレベル26)以降では、すべての通知をいずれかの通知チャンネルに割り当てる必要があります。上記のコードでは Build.VERSION.SDK_INT をチェックし、Android 8.0以降でのみ NotificationChannel を作成するようにしています。
手順4:AndroidManifest.xml にコードを追加する
最後に、AndroidManifest.xmlに以下のコードを追加します。
<?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端末を実機としてPCに接続しているものとします。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコン
をクリックしてください。実行デバイスとして自分のモバイル端末を選択すると、端末のデフォルト画面が表示されます。
ボタンをタップして通知を表示すると、ステータスバーには小さなアイコンが、通知シェード(通知ドロワー)を展開した画面には setLargeIcon() で設定した大きなアイコンが表示されていることが確認できます。
-
AndroidでVPNを設定する方法【専用アプリ・OpenVPNの手順を徹底解説】
モバイルデバイスでのブラウジングをもっと安全にしたいと思いませんか?それとも、Netflixの地域制限を回避したいだけでしょうか?VPN(仮想プライベートネットワーク)はそのどちらにも最適な選択肢です。では、AndroidでもVPNを設定できるのでしょうか? この記事では、AndroidデバイスにモバイルVPNをインストールして使う方法、そしてどのVPNを選ぶべきかについて詳しく解説します。さらに、スマートフォンでVPNの使用を避けるべき場面についても触れていきます。 なぜモバイルでVPNを使うのか? VPNといえば、デスクトップPCやルーター向けのツールというイメージが強いかもしれません。で
-
Androidでアプリのカスタムアイコンを設定する方法
Androidは、スマートフォンを自分好みにカスタマイズできる多彩な機能を備えています。中には専門的な知識や手間が必要な設定もありますが、数回のタップだけで完了する簡単なカスタマイズも数多くあります。カスタムアイコンの設定はまさに後者にあたるもので、ほんの数分あれば、デフォルトのアプリアイコンをおしゃれなデザインへと変更できます。 Androidでアプリのカスタムアイコンを設定する手順 カスタムアイコン対応のランチャーをインストールするまず、カスタムアイコンに対応したAndroidランチャーをインストールしましょう。残念ながら、PixelランチャーやSamsung純正ランチャーなど、多くの端