Why toast message are not show in android 4.1 operating system containing mobile
Asked Answered
V

5

8

I cant see the toast message in android 4.1 mobile. Upto yesterday I was able to see the toast message. From today only I can not see the message. Please help me.

Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_SHORT).show(); 

I have tried custom toast message also instead of toast message. But still not working.

Custom toast:

LayoutInflater inflater=getLayoutInflater(); 
View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root)); 
TextView text = (TextView) layout.findViewById(R.id.text); 
text.setText("Please fill Name"); 
Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG); 
toast.setView(layout); 
toast.show();
Vienna answered 28/8, 2012 at 12:46 Comment(5)
Please share your code or log traces.Gleeson
if(first_name.length()<1 ||first_name==null) { Toast.makeText(getApplicationContext(), "Please fill Name", Toast.LENGTH_SHORT).show(); Utilities.writeIntoLog("Please fill Name"); }Vienna
I just tested this on a 4.1 device and it worked.. Are you sure that your first_name string is actually empty?Gleeson
probably your context is not right !Yeasty
Not explicitly related to your problem but you really should perform your null check first, because if it actually is null, you'll crash on the first condition.Erbe
B
8

Change to this and check again.

if(first_name.length() == 0) 
{ 
    Toast.makeText(NameOfYourActivity.this, "Please fill Name", Toast.LENGTH_SHORT).show(); 
    Utilities.writeIntoLog("Please fill Name"); 
}
Basrhin answered 28/8, 2012 at 12:53 Comment(4)
see i can view the toast message in galaxy,emulator.But i cant see in nexus(os 4.1)Vienna
@Vienna I just want to check is this code working in nexus (4.1) . So please try my code and let me know weather it is working or not .Basrhin
You should still run the NULL check as well.Stab
i have tried custom toast message also instead of toast message.but still not working.Custom toast: LayoutInflater inflater=getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Please fill Name"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();Vienna
N
49

Toast was not showing with me in Android 4.1 because Show Notifications was checked off in my app's settings. I just went to Settings->Manage Applications->[My App] and toggled on Show Notifications and Toasts started to appear.

Neediness answered 5/1, 2013 at 8:30 Comment(2)
Thanks @herman. I tried all ways to display toast message and got stuck, but your post helps me to find the reason.Snocat
Clearly the dumbest change I've seen yet. This is what happens when Google hires amateurs to write code.Wormhole
B
8

Change to this and check again.

if(first_name.length() == 0) 
{ 
    Toast.makeText(NameOfYourActivity.this, "Please fill Name", Toast.LENGTH_SHORT).show(); 
    Utilities.writeIntoLog("Please fill Name"); 
}
Basrhin answered 28/8, 2012 at 12:53 Comment(4)
see i can view the toast message in galaxy,emulator.But i cant see in nexus(os 4.1)Vienna
@Vienna I just want to check is this code working in nexus (4.1) . So please try my code and let me know weather it is working or not .Basrhin
You should still run the NULL check as well.Stab
i have tried custom toast message also instead of toast message.but still not working.Custom toast: LayoutInflater inflater=getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout,(ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Please fill Name"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();Vienna
P
7

just post the google code link here.

Since Jelly Bean, users can disable notifications for a given app via "app details" settings.

A very bad and unwanted side effect is that once notifs are disabled, Toast messages are also disabled, even when the user is running the app!

You encourage to use Toast in your design guidelines, but who will want to use it if the users have the possibility to remove them, especially if the toasted message is an important feedback to display to the user...

I understand at least that Toasts could be disabled when the app is in background, but not if it is the foreground Activity.

Poddy answered 24/12, 2012 at 8:30 Comment(0)
S
2

Toast is working fine in all the version of Android. There can be couple of issues in your code like

  1. Your context is not wrong
  2. You are trying to display toast in the background thread instead of worker thread.

Edit In your custom toast don't set the parent in your layout inflater for example use like below

View layout = inflater.inflate(R.layout.toast_layout,null);
Soissons answered 29/8, 2012 at 4:10 Comment(3)
Plz tel me what is background thread and worker thread.Can u give me the example code for both.Vienna
Worker thread is the main thread in which your all the drawing and animation runs and also it is responsible for update the UI of your application.You can read the documentation about the Main(UI) thread. In every application there is a Main thread and the background thread are those which you create for background processSoissons
Android's documentation indicates you should pass the id of the root element in your toast as the second argument to inflater.inflate.Habitant
A
0

I had the same problem. When I invoked the code on UI thread the issue resolved for me

public void showToastMessage(final String msg) {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_LONG).show();
        }
    });
}
Amulet answered 4/9, 2017 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.