Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions android/src/toga_android/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from android.graphics.drawable import BitmapDrawable
from android.media import RingtoneManager
from android.os import Build
from android.util import TypedValue
from android.view import Menu, MenuItem
from androidx.activity import OnBackPressedDispatcherKt
from androidx.core.content import ContextCompat
Expand All @@ -14,6 +15,13 @@
from kotlin.jvm.functions import Function1
from org.beeware.android import IPythonApp, MainActivity

try:
from com.google.android.material import R as MaterialR
except ImportError: # pragma: no cover
# If you've got an older project that doesn't include the Material library,
# this import will fail. We can't validate that in CI, so it's marked no cover
MaterialR = None

import toga
from toga.command import Group, Separator
from toga.dialogs import InfoDialog
Expand Down Expand Up @@ -239,9 +247,36 @@ def create(self):
BackPressedHandler(self),
)

# The testbed uses a single theme, so only one side of this check can be
# covered in CI.
if self._uses_material2_theme(): # pragma: no cover
# Toga's Android widgets are designed for Material 3 themes, and Material 2
# themes aren't supported. It's been done for compatibility (2026-11-29).
warnings.warn(
"Material 2 themes aren't supported. Set `base_theme` in your app's "
"Android configuration to a Material 3 theme, such as "
'"Theme.Material3.DayNight".',
FutureWarning,
stacklevel=2,
)

# Call user code to populate the main window
self.interface._startup()

def _uses_material2_theme(self):
# Material 2 themes set `isMaterialTheme`; Material 3 themes inherit it, and
# also set `isMaterial3Theme`.
def theme_flag(attr):
value = TypedValue()
theme = self.native.getTheme()
return theme.resolveAttribute(attr, value, True) and value.data != 0

return (
MaterialR is not None
and theme_flag(MaterialR.attr.isMaterialTheme)
and not theme_flag(MaterialR.attr.isMaterial3Theme)
)

######################################################################
# Platform-specific app lifecycle hooks
######################################################################
Expand Down
13 changes: 0 additions & 13 deletions android/src/toga_android/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from abc import ABC, abstractmethod
from decimal import ROUND_HALF_EVEN, Decimal

from android.graphics import PorterDuff, PorterDuffColorFilter
from android.graphics.drawable import ColorDrawable
from android.view import Gravity, View
from android.widget import RelativeLayout
Expand Down Expand Up @@ -133,18 +132,6 @@ def set_background_color(self, color):
None if color in (None, TRANSPARENT) else ColorDrawable(native_color(color))
)

def set_background_filter(self, color):
# Although setBackgroundColor is defined in the View base class, we can't
# use it as a default implementation on some widgets (e.g. Button), because
# it often overwrites other aspects of the widget's appearance. For example,
# when setBackgroundColor is used on a Button, it makes the button appear
# as a solid rectangle with no bevels or animations.
self.native.getBackground().setColorFilter(
None
if color is None
else PorterDuffColorFilter(native_color(color), PorterDuff.Mode.SRC_IN)
)

def set_text_align(self, alignment):
pass # If appropriate, a widget subclass will implement this.

Expand Down
59 changes: 39 additions & 20 deletions android/src/toga_android/widgets/button.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from decimal import ROUND_UP

from android.content.res import ColorStateList
from android.view import View
from android.widget import Button as A_Button
from java import dynamic_proxy
from travertino.size import at_least

from toga.colors import TRANSPARENT
from toga_android.colors import native_color

from .label import TextViewWidget

try:
from com.google.android.material.button import MaterialButton
except ImportError: # pragma: no cover
# If you've got an older project that doesn't include the Material library,
# this import will fail. We can't validate that in CI, so it's marked no cover
MaterialButton = None


class TogaOnClickListener(dynamic_proxy(View.OnClickListener)):
def __init__(self, button_impl):
Expand All @@ -23,7 +31,18 @@ class Button(TextViewWidget):
focusable = False

def create(self):
self.native = A_Button(self._native_activity)
if MaterialButton is None: # pragma: no cover
raise RuntimeError(
"Unable to import MaterialButton. Ensure that the Material "
"system package (com.google.android.material:material:1.12.0) "
"is listed in your app's dependencies."
)

self.native = MaterialButton(self._native_activity)
# A button shows text or an icon, never both, so it doesn't need a gap
# between them. Without this, an icon is off-center.
self.native.setCompoundDrawablePadding(0)
self._default_background_tint = self.native.getBackgroundTintList()
self.native.setOnClickListener(TogaOnClickListener(button_impl=self))
self.cache_textview_defaults()

Expand Down Expand Up @@ -52,23 +71,23 @@ def set_enabled(self, value):
self.native.setEnabled(value)

def set_background_color(self, color):
self.set_background_filter(None if color is TRANSPARENT else color)
# MaterialButton draws its own background, so tint it rather than replacing
# it. TRANSPARENT resets the button to its default color.
self.native.setBackgroundTintList(
self._default_background_tint
if color in (None, TRANSPARENT)
else ColorStateList.valueOf(native_color(color))
)

def rehint(self):
if self._icon:
# Icons aren't considered "inside" the button, so they aren't part of the
# "measured" size. Hardcode a button size of 48x48 pixels with 10px of
# padding (in CSS pixels).
self.interface.intrinsic.width = at_least(68)
self.interface.intrinsic.height = 68
else:
self.native.measure(
View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED,
)
self.interface.intrinsic.width = self.scale_out(
at_least(self.native.getMeasuredWidth()), ROUND_UP
)
self.interface.intrinsic.height = self.scale_out(
self.native.getMeasuredHeight(), ROUND_UP
)
# The measured size includes the icon, and the padding around it.
self.native.measure(
View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED,
)
self.interface.intrinsic.width = self.scale_out(
at_least(self.native.getMeasuredWidth()), ROUND_UP
)
self.interface.intrinsic.height = self.scale_out(
self.native.getMeasuredHeight(), ROUND_UP
)
20 changes: 18 additions & 2 deletions android/tests_backend/widgets/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
from java import jclass

from .label import LabelProbe
from .properties import toga_color


# On Android, a Button is just a TextView with a state-dependent background image.
# On Android, a Button is a TextView that draws its own Material background.
class ButtonProbe(LabelProbe):
native_class = jclass("android.widget.Button")
native_class = jclass("com.google.android.material.button.MaterialButton")

# Heavier than sans-serif, but lighter than sans-serif bold
default_font_family = "sans-serif-medium"

@property
def background_color(self):
tint = self.native.getBackgroundTintList()
return toga_color(
tint.getColorForState(
self.native.getDrawableState(), tint.getDefaultColor()
)
)

def assert_no_icon(self):
return self.native.getCompoundDrawablesRelative()[0] is None

Expand All @@ -19,5 +29,11 @@ def assert_icon_size(self):
if icon:
scaled_size = (self.impl.scale_in(48), self.impl.scale_in(48))
assert (icon.getIntrinsicWidth(), icon.getIntrinsicHeight()) == scaled_size
# The icon fits between the button's padding.
assert (
self.native.getPaddingStart()
+ icon.getIntrinsicWidth()
+ self.native.getPaddingEnd()
) <= self.native.getWidth()
else:
pytest.fail("Icon does not exist")
1 change: 1 addition & 0 deletions changes/4914.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
On Android, buttons now use the Material Design style.
1 change: 1 addition & 0 deletions changes/4914.removal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Toga's Android backend now requires the Material package (`com.google.android.material:material:1.12.0` or later) in your project's Gradle dependencies, and a Material 3 base theme, such as `Theme.Material3.DayNight`. Apps that use a Material 2 theme get a warning when they start.
2 changes: 2 additions & 0 deletions docs/en/reference/platforms/android.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The Toga backend for Android is [`toga-android`](https://github.com/beeware/toga

`toga-android` requires Android SDK 24 (Android 7 / Nougat) or newer.

`toga-android` also requires the Material package in your project's Gradle dependencies, and a Material 3 base theme. Ensure your app declares a dependency on `com.google.android.material:material:1.12.0` or later, and sets `base_theme` to a Material 3 theme, such as `Theme.Material3.DayNight`. If your app uses a Material 2 theme, such as `Theme.MaterialComponents.Light.DarkActionBar`, Toga displays a warning when the app starts.

## Installation

`toga-android` must be manually installed into an Android project; The recommended approach for deploying `toga-android` is to use [Briefcase](https://briefcase.readthedocs.org) to package your app.
Expand Down
2 changes: 1 addition & 1 deletion examples/activityindicator/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/beeliza/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/box/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/button/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/colors/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/command/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/customwidget/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/date_and_time/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/detailedlist/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/dialogs/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/divider/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/focus/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/font/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/font_size/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/handlers/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/hardware/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/imageview/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/layout/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ requires = [
"../../android",
]

base_theme = "Theme.MaterialComponents.Light.DarkActionBar"
base_theme = "Theme.Material3.DayNight"

build_gradle_dependencies = [
"com.google.android.material:material:1.13.0",
Expand Down
Loading