2

I'm trying to display a toast message using jnius on QPython. I'm not able to use the Py4A library, since the final result should work inside a Kivy application, which doesn't have SL4A's library.

from jnius import autoclass

activity = autoclass("org.renpy.android.PythonActivity").mActivity
toaster = autoclass("android.widget.Toast")

toast = toaster.makeText(activity.getApplicationContext(), "I'm a Toast", toaster.LENGTH_SHORT)
toast.show()

However, I'm getting a jnius.jnius.JavaException: No methods matching your arguments.

How do I correctly use the toast notification with jnius?

dda
  • 6,030
  • 2
  • 25
  • 34
redevined
  • 772
  • 2
  • 6
  • 23

3 Answers3

3

I know the question is old, but maybe this help some other. The first problem is because you send a string as parameter, but you need to send java.lang.CharSequence. You can use jnius.cast for do that. The next trick is use runOnUIThread, but for a fast toast you can simply use this:

from jnius import autoclass
PythonActivity = autoclass('org.renpy.android.PythonActivity')
PythonActivity.toastError("Hello!")

Best regards!,

Sebastian

slezica
  • 31
  • 2
0

Why don't you simply creaty a popup in Kivy, pack it for Android & see if it works, if you are going to use Kivy anyway? You could also use the nice Plyer API included in Kivy to send a notification on Android via Pyjnius: Look here http://kivy.org/docs/guide/android.html and here https://plyer.readthedocs.org/en/latest/

barrios
  • 1,104
  • 1
  • 12
  • 21
  • I'm going to use the [DatePickerDialog](http://developer.android.com/reference/android/app/DatePickerDialog.html)/[TimePickerDialog](http://developer.android.com/reference/android/app/TimePickerDialog.html) too, and they aren't supported by Plyer right now. The popup would work, but I thought of using toast notifications since they fit exactly what I wanted. – redevined Oct 06 '14 at 08:46
  • 1
    This github repo seems to do what you want: https://github.com/knappador/kivy-toaster – barrios Oct 06 '14 at 16:52
  • thanks for the link! I'll try to construct the TimePicker the same way – redevined Oct 09 '14 at 12:13
0

makeToast takes CharSequence and not a String, you need to cast the String to CharSequence like this:

from jnius import autoclass, cast
PythonActivity = autoclass("org.kivy.android.PythonActivity")
context = PythonActivity.mActivity
AndroidString = autoclass('java.lang.String')
Toast = autoclass('android.widget.Toast')
duration = Toast.LENGTH_SHORT
text='Hello Toast!'
text_char_sequence = cast('java.lang.CharSequence', AndroidString(text))
toast = Toast.makeText(context, text_char_sequence, duration)
toast.show()
Khaled Ahmed Sobhy
  • 353
  • 2
  • 5
  • 16