Android通知右侧添加图标按钮的实现及MediaStyle问题咨询
Hey Ian, I totally feel your pain—having to swipe a notification just to access its action buttons is a clunky user experience that's easy to fix. Let's go through two solid approaches depending on your target Android version, and fix those MediaStyle issues you ran into along the way.
Option 1: Android 13+ (API 33+) – The Cleanest Solution
Google added a native way to show action buttons directly inline with your notification (no swipe needed!) using setActionsInline(true). This is the best approach if you're targeting newer devices:
Step 1: Create Your Action (e.g., Settings Gear)
First, define the action intent and build the action itself:
// Intent for your settings screen val settingsIntent = Intent(context, SettingsActivity::class.java).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK } val settingsPendingIntent = PendingIntent.getActivity( context, 0, settingsIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) // Build the settings action with your gear icon val settingsAction = NotificationCompat.Action.Builder( R.drawable.ic_settings, // Replace with your gear drawable "Open Settings", settingsPendingIntent ).build()
Step 2: Build the Notification with Inline Actions
Add your action and enable inline display:
val notification = NotificationCompat.Builder(context, YOUR_CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Your Notification Title") .setContentText("Notification content goes here") .setColor(ContextCompat.getColor(context, R.color.your_notification_accent)) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .addAction(settingsAction) // Add your gear action .addAction(anotherAction) // Add any other actions you need .setActionsInline(true) // This makes buttons show up directly on the right! .build()
That's it—no more swiping. The buttons will appear right next to your notification content.
Option 2: Android 12 & Below – Fixing MediaStyle Issues
If you need to support older devices, MediaStyle is still the way to go, but we can fix the "all black" and "always expanded" problems you mentioned:
Fix 1: Stop the All-Black Notification
The dark background isn't a MediaStyle bug—it's likely due to misconfigured notification channels or missing color settings:
- Set a notification accent color: Use
setColor()on your builder to define a custom accent (this will also tint your action icons). - Avoid forced dark backgrounds: When creating your notification channel, don't manually set a dark background—let it follow the system theme.
Here's how to create a properly configured channel:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( YOUR_CHANNEL_ID, "Your Channel Name", NotificationManager.IMPORTANCE_DEFAULT ).apply { description = "Channel for notifications with direct actions" lightColor = ContextCompat.getColor(context, R.color.your_notification_accent) setShowBadge(true) // DO NOT set setBackgroundColor() unless you explicitly want a fixed color } val notificationManager = context.getSystemService(NotificationManager::class.java) notificationManager.createNotificationChannel(channel) }
Fix 2: Prevent Default Expanded State
MediaStyle defaults to a compact view—you're probably seeing it expanded because you didn't specify which actions to show in compact mode. Use setShowActionsInCompactView() to pick which buttons appear in the collapsed notification:
val notification = NotificationCompat.Builder(context, YOUR_CHANNEL_ID) .setSmallIcon(R.drawable.ic_notification) .setContentTitle("Your Notification Title") .setContentText("Notification content goes here") .setColor(ContextCompat.getColor(context, R.color.your_notification_accent)) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .addAction(settingsAction) .addAction(anotherAction) .setStyle(NotificationCompat.MediaStyle() .setShowActionsInCompactView(0) // Show the first action (gear) in compact mode .setMediaSession(null) // Skip this if you're not building a media notification ) .build()
Now your notification will start collapsed, and the gear button will be accessible without swiping.
Final Notes
- For the widest compatibility, combine both approaches: check the Android version at runtime, use
setActionsInline()for API 33+ and MediaStyle for older versions. - Make sure your action icons are vector drawables (or properly sized bitmaps) to look good across all devices.
内容的提问来源于stack exchange,提问作者Ian

