Java Question Toast Message

Johan Schoeman

Expert
Licensed User
Longtime User
This compiles without a problem but when I call myToast from within the B4A project the toast message does not show. What am I missing?

B4X:
import android.widget.Toast;
import anywheresoftware.b4a.BA;
...
...
public class Toasting {

            public void myToast(final BA ba) {
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(ba.context.getApplicationContext(), "Hello toast!", duration);
            toast.show();
...
...
...
 

DonManfred

Expert
Licensed User
Longtime User
i´m not sure about but somewhere in my brain there comes a question

I´m missing the creation of a new toastobject
B4X:
Toast toast = new Toast(getApplicationContext());

BUT as my knowledge about java is limited: don´t give too much attention on it. I may be totally wrong ;)

I´m refering to http://developer.android.com/guide/topics/ui/notifiers/toasts.html
B4X:
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                               (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

I think Mr Notification (@barx ) can give a more professional answer! :)
 
Last edited:

Johan Schoeman

Expert
Licensed User
Longtime User
This bit of code is working. Seems that the "no show" was related to where in the java method it was positioned. Moved it to a different position in the method and now it is working.

B4X:
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(ba.context.getApplicationContext(), text, duration).show();
 
Last edited:
Top