Reputation: 1698
I am trying to show a toast prior to a for loop, however, the toast does not show. I also tried adding a snackbar, but that didn't work either. I would like to know why the toast does not fire first. I even tried the following, but it kept looping:
Toast toast = Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);
while (!toast.getView().isShown()) {
toast.show();
}
How can I show a toast prior to a for loop?
ArrayList<String> pizzaBases = new ArrayList<> ();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
main.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);
Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();
}
});
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
for (int i = 0; i < 100; i++) {
try {
String pizza = preferences.getString("TypeOfBase" + i, "");
if (!pizza.equals("")) {
pizzaBases.add(pizza);
}
} catch (NullPointerException e) {
//Do nothing here
}
}
}
});
Button button1 = (Button) findViewById(R.id.buttonOne);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (String myString : pizzaBases) {
System.out.println(myString);
}
}
});
}
I tried solutions like runOnUiThread, asynctask and while loops, but they all didn't show a toast nor a snackbar.
EDIT:
I found out that the UI only updates AFTER the for loop is done. I am wondering why it does that, although it is activated first.
Upvotes: 1
Views: 1278
Reputation: 2839
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);
When you are show toast you have to call .show()
method.
If you are showing Toast
in Activity
it will be better to use Activity
's context
Correction
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();
If you want to show SnackBar
than you have to provide the view of current activity not android.R.id.content
Correction
Snackbar.make(findViewById(R.id.parent_of_your_activity), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();
You don't need to use runOnUiThread
cause you're already on the main thread
You can get Context
via YourActivity.this
or this
in Activity
itself
via getContext()
in Fragment
class
Upvotes: 0
Reputation: 26547
The problem is that Toast.show()
is asynchronous. The toast will not be displayed until the current method returns, and even then, Android may decide to show it at a later time (for example if there is already another toast displayed on the screen) or even not at all.
Do you really need to use the native toasts? Maybe the best would be to use another system that is more reliable. What about a custom view that appears on top of everything that looks like a toast and that you can show immediately and reliably?
Upvotes: 2
Reputation: 9047
You don't need to use runOnUiThread
cause you're already on the main thread and you're not calling show()
on your toast in the second code.
It's also better to pass in the nearest Context
object to your toast.
public void onClick(View view) {
Toast.makeText(YourActivity.this, "Hello", Toast.LENGTH_SHORT).show(); // show must be called
Snackbar.make(findViewById(android.R.id.content), "Had a snack at Snackbar", Snackbar.LENGTH_LONG).show();
SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE);
...
}
Upvotes: 0
Reputation: 9388
You are not calling show() method on this line:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT);
It should be:
Toast.makeText(getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
Upvotes: 0