Androidで長文テキストの通知を作成する方法【BigTextStyleの使い方を解説】
はじめに
Androidの標準通知では、表示できるテキストの量が限られており、長い文章は途中で省略されてしまいます。そこで役立つのがBigTextStyleです。このスタイルを使うことで、通知を展開したときに複数行の長文テキストを表示できるようになります。
この記事では、BigTextStyleを活用して長いテキストを表示する通知を作成する手順を、サンプルコードとともにわかりやすく解説します。
手順1:新規プロジェクトを作成する
Android Studioを開き、メニューから「File」⇒「New Project」を選択して、必要な項目を入力し、新しいプロジェクトを作成します。テンプレートは「Empty Activity」で問題ありません。
手順2:レイアウトファイル(res/layout/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">
<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>ボタンのandroid:onClick属性にcreateNotificationを指定することで、ボタンをタップしたときに同名のメソッドが呼び出されるようになります。
手順3:MainActivityにコードを追加する
次に、src/MainActivityに以下のコードを記述します。ポイントは、NotificationCompat.BigTextStyleを使って長文テキストを設定している箇所です。
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;
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("Notify Me");
mBuilder.setContentText(getResources().getString(R.string.lorem_ipsum));
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(getResources().getString(R.string.lorem_ipsum)));
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());
}
}コードのポイント
- setContentText():折りたたまれた状態で表示される通常のテキストを設定します。
- setStyle()+BigTextStyle:通知を展開した際に、全文を複数行で表示できるようにします。
bigText()に表示したい文字列を渡します。 - setAutoCancel(true):通知をタップすると自動的に消えるようにします。
- 通知チャンネル:Android 8.0(APIレベル26)以降では通知チャンネルの作成が必須のため、バージョン判定を行ってからチャンネルを生成しています。
なお、表示する長文テキストはstrings.xmlにlorem_ipsumという名前で定義しておいてください。
手順4:AndroidManifest.xmlを確認・編集する
次のコードを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スマートフォンをPCに接続していることを前提に説明します。Android Studioからプロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックしてください。接続したモバイル端末を選択して実行すると、端末にアプリのデフォルト画面が表示されます。
画面中央の「create notification」ボタンをタップすると通知が表示され、通知を下にスワイプして展開すると、BigTextStyleによって設定された長文テキストが全文表示されることを確認できます。
まとめ
標準の通知では収まりきらない長いテキストも、NotificationCompat.BigTextStyleを組み合わせるだけで簡単に全文表示できるようになります。ニュースアプリのお知らせやメールの本文プレビューなど、長文を通知に表示したい場面でぜひ活用してみてください。
-
Androidでカスタム通知レイアウトとテキストの色を作成する方法を徹底解説
このチュートリアルでは、Androidアプリにおいて標準的な通知と、RemoteViewsを使ったカスタム通知レイアウトの両方を作成する方法を、サンプルコード付きでわかりやすく解説します。カスタムレイアウトを活用すれば、通知のデザインやテキストの色などを自由にコントロールできるようになります。 全体の流れ 本記事の手順は以下の通りです。 Android Studioで新規プロジェクトを作成する メイン画面のレイアウト(activity_main.xml)を用意する MainActivity.javaに通知の生成処理を実装する 通知・カスタム通知・通知内容表示用のレイアウトXMLを作成する A
-
CSSで光るテキスト(グロー効果)を作成する方法
CSSで光るテキスト(グロー効果)を作成するには、text-shadowプロパティと@keyframesによるアニメーションを組み合わせます。複数の影を重ねて光の輪を表現し、それをアニメーションで明滅させることで、文字が脈動するように輝く演出が可能です。 コード例 <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <style> body {