Hide Toastmessage?

JonPM

Well-Known Member
Licensed User
Longtime User
This sounds weird, but is there a way to hide a toastmessage? For example, I have a screen that when opened will display a toastmessage with long duration. However, if the user backs out of the screen or clicks to another screen, I would like to immediately hide that toastmessage. Basically something like ProgressDialogHide but for ToastMessages.
I saw in the CustomToast library that it allows you to set custom duration, but this won't really work for me.
 

BarrySumpter

Active Member
Licensed User
Longtime User
Last edited:
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
Thanks. The problem I have wouldn't really be fixed with a timer either. I would like to be able to manually remove the toast from view before the set duration expires. Oh well, this isn't that imperative to my app, would just be nice.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Could you create a small panel with a label on it & a timer

To show it set it's visible property to true & start the timer.

If you exit the activity then test if it's visible. If so kill timer & hide the panel

B4X:
pnlToast. Visible = True
tmrToast. Enabled = True
. 
. 
Sub Activity_Pause(UserClosed As Boolean) 
    If pnlToast. Visible Then
        tmrToast. Enabled = False
        pnlToast. Visible = False
    End If
End Sub

Regards, Ricky
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Hi there is a function in the standard toast class which allows for canceling a toast if it is showing, Erel must not have included it in the standard library I presume because it only last for 3.5sec at most. I have been playing with a small toast library and it does in fact close the toast when called. Not sure if Erel wants to include this option in a future update or if XverhelstX would put the feature in his customtoast library. It would be a simple job to create another toast library with this feature but not sure if we need another toast library. The standard toast object also allows for positioning on the screen and text padding.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Ok, that sounds interesting.

When I get home from work in 6 hours I'm going to start on a custom toast using a class.

It'll have a Show & a Hide methods. It will accept 3 arguments at Initialize - activity, duration, and a boolean that if true will enable clicking the toast to hide it.
The Show method will accept a string to display and a duration in milliseconds - if it's zero then it stays until it's hidden or if clickable when user clicks it.

Because it's in a class anyone can tweak it to customize it however you like.

B4X:
Dim mt As Toast
mt. Initialize (Activity, 2000, True) 
mt. Show("this is a custom toast",  1000) 
. 
. 
. 
mt. Hide

Regards, Ricky
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Will be interested how you implement the duration method as android setduration method only accepts the two constants LENGTH_SHORT & LENGTH_LONG (although the documentation does say "This time could be user-definable."), any methods I have seen only repeat the toast a number of time to simulate a longer duration. You might also include the setGravity method so that we can move the toast around the screen.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It will be a good use of class, here's one in a code module for anyone not using the beta yet. It just has long and short durations. If you want to add methods they are documented here
 

Attachments

  • tt.zip
    5.9 KB · Views: 300
Last edited:
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
:sign0142:re the gravity it would be simple like this

mt.setGravity(Gravity.CENTER_HORIZONTAL)

You could add more properties like height, font sets like textsize & typeface & even background color.

It will build on a panel with label to be added to the activity in Show and destroyed in Hide
The duration will be by a timer. I'm thinking of ticking it 10ms. It will subtract 10 from the initial time you set so mt.Show("toast text", 1200) would show it to 1.2 seconds

You will call Hide if you want to kill it. This will make the panel invisible then take it off the activity. I'll test to see if it naturally goes away when the activity is paused. If it doesn't then you write code in Activity_Pause to hide it.

I see the built in toastmessage when you click it goes away before it's timer expires so I'll make do the same by default.

Regards, Ricky
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
stevel05 that is a great example of using the reflector library, I really must put some time into the reflector library.
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Ok I'm home and starting to code.

I realised whilst at work that my way of using a panel & label should work with an activity but would fail in a service.

Stevel05 I have tested your code on an activity in the class and it works just fine.

Can I use it in my version if we're in a service?

So it's Initialize sub would look like

B4X:
Private context As String
Private duration As Int

Sub Initialize(obj As Object, durationin As Int)
    duration = durationin
    If obj Is Activity Then    'do my custom code for an activity
        context = "activity"
    Else If obj Is String Then   
        Dim s As String
        s = obj
        If s="service" Then    'do your code
            context = "service"
        End If
    End If
End Sub

Sub Show(showtext As String, duration As Int)
    If context="activity" Then    'do my custom show
    Else If context="service" Then    'do your code to show
    End if
End Sub

Sub Hide
    If context="activity" Then    'do my hide code
    Else If context="service" Then   'do your hide code
    End if
End Sub

'code to instantiate a toast
Dim t As Toast
'we're in an activity
t.Initialize(Activity, 2000)
t.Show("we're making toast!", 3000)  'overwrite the duration
.
.
t.Hide

'and if we're in a service
t.Initialize("service", 1)   '1=True, 0 = False for duration
t.Show("toast is cooked!", 0)  'overwrite the default duration if we want
.
.
.
t.Hide
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Ricky, if you're asking if you can use my code then, yes of course. It's all good to share. If you're asking me if it'll work, then I have done very little with services so it'll be down to trial and error I'm afraid.

I look forward to seeing the results.

Steve
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
Hit a snag

compiler complains about this code

B4X:
Sub Initialize(obj As Object, message As String, durationin As Int)
   duration = durationin
   
   If obj Is Activity Then
      context = "activity"
      pnl.Initialize("pnl")
      lbl.Initialize("lbl")
      lbl.Text = message
      lbl.Height = height
      pnl.Visible = False
      pnl.Height = height
      pnl.Width = lbl.Width 
      pnl.AddView(lbl, 0, 0, lbl.Width, height)
      Dim act As Activity 
      act = obj
      
      act.AddView(pnl, left, top, lbl.Width, height)
   Else If obj Is String Then
      Dim s As String
      s = obj
      If s="service" Then
         context = "service"
         Dim C As Object
         duration = durationin
   
         C = R.GetContext
   
         T = R.RunStaticMethod("android.widget.Toast", "makeText", Array As Object(C, message, duration), Array As String("android.content.Context", "java.lang.CharSequence", "java.lang.int"))
      End If
   End If
End Sub

at the Dim act As Activity line. Here is the compile text

Compiling code. 0.02
Compiling layouts code. 0.00
Generating R file. 0.00
Compiling generated Java code. Error
B4A line: 30
Dim act As Activity
javac 1.6.0_30
src\ricky\customtoast\toast.java:150: cannot find symbol
symbol : constructor ActivityWrapper()
location: class anywheresoftware.b4a.objects.ActivityWrapper
_act = new anywheresoftware.b4a.objects.ActivityWrapper();Debug.locals.put("act", _act);
^
1 error

I guess this won't work like I hoped

:sign0085:

regards, Ricky
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have a look at this thread. May just have to wait a little while.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
I had written code using reflection lib to do a custom toast I have been using for months. It works great but I also wanted it in a module but could only get it to work in an Activity. I hope with the new version (which I do not have yet), it can be made to work in a code module, etc. Seems like we may be able in the next update!!
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
It will be a good use of class, here's one in a code module for anyone not using the beta yet. It just has long and short durations. If you want to add methods they are documented here

Hi stevel05,

Hope you still read this thread.

I was trying to call the setDuration method, using your example but failed to call it. I attached here the test project. Can you please advise me where is the mistake thank you very much.

Best

bsnqt
 

Attachments

  • ToastTest.zip
    326.8 KB · Views: 260
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
Hi Erel,

Sorry I will user File - Export as zip next time, I did not know it.

My code is as follows:

B4X:
Sub setDuration(duration As Int)
   R.Target=T
   R.RunMethod2("setDuration", Array As Object(duration), Array As String("java.lang.int"))
End Sub

It gave me error Not Found Class Exception
I tried to change a little bit but again Not Found Method Exception.

My purpose is trying to set the duration of this toast using this example.

Thanks!

Best
 
Upvote 0
Top