Skip to content

Use Material buttons on Android, and require a Material 3 theme - #4914

Merged
freakboy3742 merged 6 commits into
beeware:mainfrom
kattni:android-material-button
Dec 2, 2026
Merged

freakboy3742 merged 6 commits into
beeware:mainfrom
kattni:android-material-button

Conversation

@kattni

@kattni kattni commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Refs #2250.

This is the first step in moving Toga's Android backend to Material 3, one widget at a time. It converts Button, and makes the Material library and a Material 3 theme requirements of toga-android. Converting the rest of the widgets will continue under #2250.

Changes

  • On Android, Button is now a com.google.android.material.button.MaterialButton, so it follows the app's Material theme. A background color is applied as a background tint, and TRANSPARENT resets the tint to the theme's default. Widget.set_background_filter() was only used by Button, so it has been removed.
  • toga-android now requires the Material package (com.google.android.material:material:1.12.0 or later) and a Material 3 base theme, such as Theme.Material3.DayNight. If the app's theme is a Material 2 theme (for example Theme.MaterialComponents.Light.DarkActionBar), the app issues a FutureWarning when it starts. There is no Material 2 support code; this is intended to land as part of a major version change. If the Material package is missing, creating a Button raises a RuntimeError, as OptionContainer already does.
  • The testbed's Android base_theme is now Theme.Material3.DayNight.
  • The Android platform docs and the Button docs describe the new requirements.

Dependencies

This PR depends on beeware/briefcase#3168 and beeware/briefcase-android-gradle-template#137, which are both still in review. Together, they give new Briefcase projects a Theme.Material3.DayNight base theme, the Material dependency, and day/night colors. Toga doesn't need them to work: an app that sets a Material 3 base_theme and declares the Material dependency in build_gradle_dependencies works with this PR today, as the testbed does.

Testing

Backends exercised: Android, with the full testbed on an API 31 emulator (beePhone, as in CI), using Theme.Material3.DayNight: 514 passed, 99 skipped, 56 xfailed, 6 xpassed, with 100.0% coverage. The core test suite passes with 100% coverage, and pre-commit run --all-files passes.

The Material 2 warning branch is marked # pragma: no cover, because the testbed can only run with one theme.

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

Assisted-by: Claude Code (Claude Opus 5.5)

Android buttons are now MaterialButtons, so they follow the app's
Material theme. Background colours are applied as a tint.

The Android backend now requires the Material library, and warns at
startup when the app uses a Material 2 theme.
The testbed now uses Theme.Material3.DayNight, the theme Toga's Android
backend requires.
kattni added a commit to kattni/temp-tracker that referenced this pull request Nov 30, 2026
Use the android-material-button branch from beeware/toga#4914 instead of
a local worktree, so the app builds from a clean checkout. travertino and
toga-core come from the same branch, because each Toga package pins the
others to its own version.

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is much cleaner than I was expecting - the discussion around adopting Material 3 made it sound like switching was going to require major surgery. Not only is the set of changes relatively minor, it cleans up both the messy areas of buttons (the handling of background colours, and the capitalisation of text labels).

One cleanup flagged inline. Two other pieces that need fixes:

  • The examples - they all currently hard-code references to the older Material2 DarkActionBar theme
  • The tests/widgets/test_button.py::test_icon testbed test passes, but visual inspection of the test case shows a button that isn't big enough to display the icon.

Comment thread docs/en/reference/api/widgets/button.md Resolved
Material 3 is now a requirement of the Android backend as a whole, so the
Button docs don't need to repeat it.
The examples, and the Positron FastAPI bootstrap, used the Material 2
Theme.MaterialComponents.Light.DarkActionBar theme. They now use
Theme.Material3.DayNight, as the testbed does.
A Material button has 24dp of padding at each side, so the fixed 68px width
Toga gave icon buttons cut off the right side of the icon. Measure icon
buttons natively, like text buttons, so the size includes the icon and its
padding, and remove the gap Material leaves between an icon and the text,
which put the icon off center.
@kattni

kattni commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! I've addressed the other two points:

  • Examples (e8a5112): every example now uses Theme.Material3.DayNight, as the testbed does. The Positron FastAPI bootstrap had the same Material 2 theme hard-coded, so I've updated that too.
  • test_icon (9ff80de): a Material button has 24dp of padding at each side, so the fixed 68px width Toga gave icon buttons cut off the right side of the icon. Icon buttons are now measured natively, like text buttons, and the gap Material leaves between the icon and the (empty) text is set to zero, so the icon is centered. The Android probe's assert_icon_size now also checks that the icon fits between the button's padding; that check fails without the fix.

The Android testbed failure on the latest push happened while Gradle was resolving dependencies, before any tests ran. It's the same Maven Central "429 Too Many Requests" failure as main's most recent run, so it needs a re-run rather than a fix.

@mhsmith

mhsmith commented Dec 1, 2026

Copy link
Copy Markdown
Member

Maven Central "429 Too Many Requests" failure

I've seen this in several projects over the last week. If necessary, there's a workaround in numpy/numpy-release@25a5d01.

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those updates are great - thanks!

@freakboy3742
freakboy3742 merged commit 13767c2 into beeware:main Dec 2, 2026
121 of 122 checks passed
jkingok added a commit to jkingok/mypystub that referenced this pull request Dec 4, 2026
* Point Toga back at upstream now the WebView fix has merged

The WebView ValueError fix landed upstream as beeware/toga#4915, so the
launcher and the new-project template drop the fork branch
(jkingok/beeware-toga@claude/project-thread-cbqd4n) and use
beeware/toga main again. uv.lock moves to 13767c2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Move Android to a Material 3 theme, which Toga now requires

beeware/toga#4914 makes Toga's Android backend expect a Material 3 base
theme and warn on Material 2. Use Theme.Material3.DayNight, as Toga's own
examples now do, in the launcher and the new-project template.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Follow dark mode on Android: in-app pages and list icons

With the Material 3 DayNight theme the app can be dark, but:

- Android's WebView kept prefers-color-scheme light, and the page used
  light-dark(), which older WebViews (before Chrome 123) don't support.
  The page's colours are now plain CSS variables chosen by a media
  query, and on Android html_page fixes the appearance to the app's own
  dark_mode (Toga API, now allowed in test_ui_app_isolation).
- Android's DetailedList draws row icons as they are, so the black
  line-art icons vanished on a dark background. They are now tinted
  with the theme's primary text colour, as iOS already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Drop the page palette; let the web view's own colours follow dark mode

The in-app pages now set only color-scheme (both, or the app's
appearance on Android) and leave text and background to the browser's
defaults. Code blocks and table borders use translucent greys, and
Pygments colours stay plain rules under a prefers-color-scheme query,
so older web views degrade to readable light colours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Use the Android template's main branch for Material 3 dark colours

On a dark Material 3 theme, buttons drew their text and icons in the
theme's default colorOnPrimary (dark purple) on Briefcase's teal
colorPrimary. Briefcase's released Android template doesn't set
colorOnPrimary; its main branch does, with day and night colours
(briefcase-android-gradle-template#137). The launcher and the
new-project template both use it, as iOS already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Keep disabled buttons' dim text on Android until Toga has the fix

Toga's TextViewWidget remembers the default text colour as a single
int and restores it on a style reset, dropping the theme's state list.
A disabled Material button then keeps its enabled white text on the
pale disabled background. Patch the method to remember getTextColors(),
as jkingok/beeware-toga#3 does, installed beside android_list. Remove
it once Toga has the fix.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Tint the launcher's list icons on macOS (part of #269)

The line-art icons are marked as template images, so AppKit draws them
in the label colour, and in the selection's text colour on a selected
row, following light and dark mode as on iOS and Android. A prototype's
own icon is left in its colours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Draw macOS list icons in the row's text colour with a DetailedList impl

Marking the icons as template images did nothing: Toga's Cocoa
TogaDetailedCell draws the icon itself with drawInRect, so AppKit never
tints it. Instead register a DetailedList (toga.platform.register_impl)
whose cell draws icons marked by the launcher in the colour Toga gives
the row's text (selected or not), then draws as Toga's cell does. A
prototype's own icon keeps its colours.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

* Say why macOS list icons aren't tinted, and register once

The skip when Toga's DetailedList is already loaded now logs a warning
(it was debug, so a silent failure looked like success), and a failure
while tinting an icon is logged and the icon drawn as it is. Registering
happens once per process, so a second launcher doesn't warn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JRRpxM667mcp32Z5NTMPAd

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

3 participants