test-kivy-app/kivy_venv/lib/python3.11/site-packages/kivymd/toast/androidtoast.py

83 lines
2.3 KiB
Python
Raw Normal View History

2024-09-15 12:12:16 +00:00
"""
AndroidToast
============
.. rubric:: Native implementation of toast for Android devices.
.. code-block:: python
# Will be automatically used native implementation of the toast
# if your application is running on an Android device.
2024-09-15 17:57:02 +00:00
# On desktop use `MDSnackbar < https://kivymd.readthedocs.io/en/latest/components/snackbar/>`_
2024-09-15 12:12:16 +00:00
from kivy.lang import Builder
from kivymd.toast import toast
from kivymd.app import MDApp
KV = '''
MDScreen:
2024-09-15 17:57:02 +00:00
md_bg_color: self.theme_cls.backgroundColor
2024-09-15 12:12:16 +00:00
2024-09-15 17:57:02 +00:00
MDButton:
2024-09-15 12:12:16 +00:00
pos_hint:{"center_x": .5, "center_y": .5}
on_press: app.show_toast()
2024-09-15 17:57:02 +00:00
MDButtonText:
text: "Make toast"
2024-09-15 12:12:16 +00:00
'''
2024-09-15 17:57:02 +00:00
class Example(MDApp):
2024-09-15 12:12:16 +00:00
def build(self):
return Builder.load_string(KV)
def show_toast(self):
toast("Hello World", True, 80, 200, 0)
2024-09-15 17:57:02 +00:00
Example().run()
2024-09-15 12:12:16 +00:00
"""
__all__ = ("toast",)
from kivy import platform
if platform != "android":
raise TypeError(
f"{platform.capitalize()} platform does not support Android Toast"
)
2024-09-15 17:57:02 +00:00
from android import mActivity
2024-09-15 12:12:16 +00:00
from android.runnable import run_on_ui_thread
from jnius import autoclass
Toast = autoclass("android.widget.Toast")
String = autoclass("java.lang.String")
@run_on_ui_thread
def toast(text, length_long=False, gravity=0, y=0, x=0):
"""
Displays a toast.
:param length_long: the amount of time (in seconds) that the toast is
visible on the screen;
:param text: text to be displayed in the toast;
2024-09-15 17:57:02 +00:00
:param length_long: duration of the toast, if `True` the toast
2024-09-15 12:12:16 +00:00
will last 2.3s but if it is `False` the toast will last 3.9s;
:param gravity: refers to the toast position, if it is 80 the toast will
be shown below, if it is 40 the toast will be displayed above;
:param y: refers to the vertical position of the toast;
:param x: refers to the horizontal position of the toast;
Important: if only the text value is specified and the value of
the `gravity`, `y`, `x` parameters is not specified, their values will
be 0 which means that the toast will be shown in the center.
"""
duration = Toast.LENGTH_SHORT if length_long else Toast.LENGTH_LONG
2024-09-15 17:57:02 +00:00
t = Toast.makeText(mActivity, String(text), duration)
2024-09-15 12:12:16 +00:00
t.setGravity(gravity, x, y)
t.show()