Skip to content

Migrate Create a custom notification layout snippets - #1108

Draft
hamen wants to merge 1 commit into
android:mainfrom
hamen:hamen/custom-notification-snippets
Draft

hamen wants to merge 1 commit into
android:mainfrom
hamen:hamen/custom-notification-snippets

Conversation

@hamen

@hamen hamen commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Code snippets are for:

Why the XML layouts were extracted too:

The Kotlin snippet on this page cannot compile on its own. It resolves R.layout.notification_small and R.layout.notification_large through RemoteViews(packageName, ...), so both layout resources have to exist in the module for :views:compileDebugKotlin to succeed. That is why the two <LinearLayout> blocks from the page were extracted into real layout resources in views/src/main/res/layout/ rather than left hardcoded on DAC. The same reasoning applies to R.drawable.notification_icon, which setSmallIcon(...) references. Hosting them also keeps the layouts and the Kotlin that inflates them in sync in one place, and it follows the precedent already set by views/src/main/res/layout/widget_layout.xml on the App Widgets pages.

List of modifications:

  • Custom-notification page: Migrated the collapsed notification layout to views/src/main/res/layout/notification_small.xml with region tag android_views_notifications_custom_layout_small. Required for the Kotlin snippet to compile.
  • Custom-notification page: Migrated the expanded notification layout to views/src/main/res/layout/notification_large.xml with region tag android_views_notifications_custom_layout_large. Required for the Kotlin snippet to compile.
  • Custom-notification page: Migrated the Kotlin "Build and show the notification" block to views/src/main/java/com/example/example/snippet/views/notifications/CustomNotification.kt with region tag android_views_notifications_custom_content_view.
  • Custom-notification page: Added views/src/main/res/drawable/notification_icon.xml, a 24dp bell vector drawable required by setSmallIcon(R.drawable.notification_icon) in the migrated Kotlin snippet. Origin: hand-authored <vector> based on the Material Symbols "notifications" (bell) path, Apache 2.0, matching the house style of the existing views/src/main/res/drawable/ic_cloud.xml.

No new top-level module was added; everything lands in the existing views module.

Code between region tags is byte-identical to the page, with these exceptions:

  • The <?xml version="1.0" encoding="utf-8"?> declaration of both layouts sits outside the region tag, because an XML declaration must be the first line of the file and the license header must precede the region tag. This matches the existing precedent in views/src/main/res/layout/widget_layout.xml.
  • spotlessApply re-indented the 5 chained builder calls in the Kotlin snippet from an 8-space to a 4-space continuation indent. No tokens changed.

Boilerplate kept outside the region tags: private const val CHANNEL_ID, the private class CustomNotificationActivity : ComponentActivity() wrapper (so the page's bare getSystemService(...) and packageName calls resolve against a real Context), and the val context: Context = this binding. No @RequiresApi was added, since the views module minSdk is 36.

Snippets not migrated:

  • Lines 136-153 because the block is Java, which is removed under the Kotlin-first guidance rather than hosted.
  • Lines 164-172 because the block is an incomplete, non-compilable View-based XML fragment (a bare <TextView> with no root element or namespace) whose teaching point relies on inline <b> highlighting. It stays hardcoded on the page and is wrapped with disableFinding(SNIPPET_GITHUB).

Verification: ./gradlew :views:compileDebugKotlin and ./gradlew :views:spotlessApply both pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant