Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีทำ Expand/Collapse Notification ใน Android (BigTextStyle)

ภาพรวม

ตัวอย่างนี้สาธิตการสร้างการแจ้งเตือนที่สามารถขยาย (Expand) และยุบ (Collapse) ได้ โดยใช้ BigTextStyle ซึ่งเป็นพฤติกรรมมาตรฐานของ Android

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

Android Studio → File → New Project

ขั้นตอนที่ 2: 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: strings.xml - ข้อความยาวสำหรับทดสอบ

<resources>
    <string name="lorem_ipsum">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, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</string>
</resources>

ขั้นตอนที่ 4: MainActivity - Expandable Notification

package app.tutorialspoint.com.notifyme;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
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");
        
        String longText = getResources().getString(R.string.lorem_ipsum);
        mBuilder.setContentText(longText); // แสดงข้อความย่อ (บรรทัดเดียว)
        
        // BigTextStyle ทำให้ขยาย/ยุบได้อัตโนมัติ
        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
            .bigText(longText)                           // ข้อความเต็มเมื่อขยาย
            .setBigContentTitle("📖 เนื้อหาเต็ม")        // หัวข้อเมื่อขยาย
            .setSummaryText("ลากลงเพื่ออ่านต่อ • ลากขึ้นเพื่อยุบ"); // คำแนะนำ
        
        mBuilder.setStyle(bigTextStyle);
        mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
        mBuilder.setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= 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

<?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>

พฤติกรรม Expand/Collapse แบบมาตรฐาน

1. Gesture ควบคุม

  • ลากลง (Swipe down) บนการแจ้งเตือน → ขยาย (Expand)
  • ลากขึ้น (Swipe up) บนการแจ้งเตือนที่ขยายแล้ว → ยุบ (Collapse)
  • กดปุ่มลูกศร (บางอุปกรณ์/Launcher) → ขยาย/ยุบ

2. สถานะการแสดงผล

สถานะแสดงผลความสูงประมาณ
Collapsed (ยุบ)Title + บรรทัดแรกของ ContentText + ไอคอน~64-80dp
Expanded (ขยาย)BigContentTitle + BigText + SummaryText + Actions~256-512dp (ขึ้นอยู่กับเนื้อหา)
Heads-up (แจ้งเตือนลอย)แสดงบนหน้าจอทับแอปอื่น (High priority)~120-160dp

3. ควบคุมด้วยโค้ด (Advanced)

// บังคับให้ขยายตั้งแต่แรก (Android 9+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
    mBuilder.setShowWhen(true);
    // ไม่มี API สำหรับบังคับ Expand โดยตรง แต่สามารถตั้ง Priority HIGH
}

// ตรวจสอบสถานะ (ไม่มี API สาธารณะโดยตรง)
// ใช้ NotificationListenerService หากต้องการรู้สถานะจริง

แนวทางปฏิบัติที่ดี

  • ใช้ setContentText() สำหรับข้อความสั้น (แสดงเมื่อยุบ)
  • ใช้ bigText() สำหรับข้อความเต็ม (แสดงเมื่อขยาย)
  • เพิ่ม setSummaryText() ให้ชัดเจนว่าเป็นข้อความยาว
  • ทดสอบทั้งบน Lock Screen, Notification Shade, และ Heads-up
  • พิจารณาใช้ InboxStyle หากเป็นรายการแยกบรรทัด