LoginSignup
13
11

More than 5 years have passed since last update.

AndroidのNotificationでカスタムViewの高さを広げる

Last updated at Posted at 2016-10-12

Notificationの高さを64dpよりも高く広げる

AndroidのNotificationにおいて、通常のスタイルでは表現できない通知を行いたい場合、
RemoteViewsをつかってカスタムビューを作成します。

ただ、RemoteViewsにどんなものも使えるかというとそうではなく、
通常では高さが64dpまでという制限が自動的にかかります。

JellyBean以降の拡張

しかし、API16のJELLY_BEAN以降は以下のようなコードを書くことで、
高さ64dpの制限を256dpまで拡張することができます。

Androidの公式ドキュメントにもこの高さを広げられる仕様について説明されているのですが、
『標準ビューレイアウトでは64dpまでで、拡張ビューレイアウトでは256dpまでです。』とあるだけです。
https://developer.android.com/guide/topics/ui/notifiers/notifications.html?hl=ja#CustomNotification

具体的にどうすればいいのかの説明が見当たらなかったのでまとめておきます。
やることは結構単純で、build済みの notification に対して、
notification.bigContentView = custonView してください。

修正前

builder.setContentで作成したRemoteViewsをセットしてあげればいいのかと思っていました。
しかし、setContentは、閉じた時、つまり高さ64dpの時のViewをセットする役割なので、高さは64dpのままでした。

before.jpg

修正前のコード

NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext());
builder.setSmallIcon(R.drawable.notification_icon);

RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
customView.setTextViewText(R.id.customTitleTextView, "タイトル");
customView.setTextViewText(R.id.customDescTextView, "本文-カスタム通知レイアウトで使用できる高さは、通知ビューによって異なります。標準ビューレイアウトでは64dpまでで、拡張ビューレイアウトでは256dpまでです。AndroidのNotificationを使う際、通常のスタイルでは表現できない通知を行いたい場合、RemoteViewsをつかってカスタムビューを作成します。");
customView.setImageViewBitmap(R.id.customImageView, bitmap);
builder.setContent(customView);

Notification notification = builder.build();
NotificationManagerCompat manager = NotificationManagerCompat.from(context.getApplicationContext());

manager.notify(123, notification);

修正後

notification.bigContentView = custonView とAPIバージョンでの分岐を追加します。
高さが十分に広がり、出したい情報が出せるようになりました。

after.jpg

修正後のコード

NotificationCompat.Builder builder = new NotificationCompat.Builder(context.getApplicationContext());
builder.setSmallIcon(R.drawable.notification_icon);

RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
customView.setTextViewText(R.id.customTitleTextView, "タイトル");
customView.setTextViewText(R.id.customDescTextView, "本文-カスタム通知レイアウトで使用できる高さは、通知ビューによって異なります。標準ビューレイアウトでは64dpまでで、拡張ビューレイアウトでは256dpまでです。AndroidのNotificationを使う際、通常のスタイルでは表現できない通知を行いたい場合、RemoteViewsをつかってカスタムビューを作成します。");
customView.setImageViewBitmap(R.id.customImageView, bitmap);
builder.setContent(customView);

Notification notification = builder.build();
// ここを追加
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
    notification.bigContentView = customView;
}
NotificationManagerCompat manager = NotificationManagerCompat.from(context.getApplicationContext());

manager.notify(123, notification);

参考

Android Remoteviews for expanded notifications

13
11
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
13
11