Toast message on Android

Hi all,

I need help with toast message on Android

Now I work on service plugin that must show toast message time to time while app is out of focus.

But Logcat always shows “Can’t create handler inside thread that has not called Looper.prepare()” when I try to call Toast.makeText from plugin for Unity Android project.

Toast.makeText works fine when I call it from native Android project, but not from Unity.
It seems, because Unity uses UnityPlayerActivity rather than Activity.

Did anybody meeet this ?
I would greatly appreciate to get some hint how to workaround such a problem.

Kind regards.
Igor

That exception happens when call Toast in another thread. U might just add a handler in your activity like this:

Then call ToastMessage() would be fine.

thank you much
will try it on Monday

Thanks for help
nevertheless it does not work
maybe because I use UnityPlayerActivity as Context for Toast

What value does TextSender.m_pIns have in your sample?

TextSender extends UnityPlayerActivity

TextSender.m_pIns is my activity that extends UnityPlayerActivity. That code works in my Android Text Message plugin.

TextSender is clear to be UnityPlayerActivity.
But what’s m_pIns member of TextSender ?

I was having the same problem and created a plugin to handle any toast I wanted to display. Have a look Android Toast Plugin for Unity by SnickleFritz

I figured out how to do this and made a simple project demonstrating.
Click here if interested.

I want to copy some text (UI.Text, variable or whatever) in Android Clipboard. Simply saying user can copy text from unity app and take it back in his mobile.

I just did that and answered your comment here :slight_smile:
If you want to get see the script (or get the working project) it’s on github.

Cheers!

1 Like

I slightly edited Your code to make it universal and simple toast message shower. Maybe someone will need it:

https://github.com/zakirshikhli/toaster/commit/b18af0d58f605faedaf92de47ad8cb9123d0a5a4

Now You just need do call this function like this:
ToastMessageScript.showToastOnUiThread(“Your Text”);