I seek to create a toast whose value should reflect a number and should dynamically change while the toast is still displayed. I don't want to create new toasts for every change in the value. The changes to the value should reflect in the existing displayed toast itself. Is this possible, if so, how should I go about it?
Asked
Active
Viewed 7,409 times
5
-
Have you tried by using some variable or anything? – Mehul Joisar Nov 24 '14 at 05:06
-
http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView – John Nov 24 '14 at 05:08
-
@MehulJoisar, I tried that but once a toast is shown, the text in it is not updated there even when the variable's value gets changed. – Debojeet Chatterjee Nov 24 '14 at 05:12
-
@DebojeetChatterjee : Ok. It seems that you have to create some custom layout or custom toast itself. – Mehul Joisar Nov 24 '14 at 05:13
2 Answers
11
You can save your instance of Toast which you get from makeText and update it with setText.
UPDATED
Code:
public class MainActivity extends ActionBarActivity {
private Toast mToast;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
if (mToast == null) {
mToast = Toast.makeText(MainActivity.this, "Count " + 0, Toast.LENGTH_LONG);
}
mToast.setText("Count " + count++);
mToast.show();
}
});
}
}

Bracadabra
- 3,609
- 3
- 26
- 46
-
Seems it's not working. The Toast is redrawn each time I use setText. Can you suggest any other way? – Debojeet Chatterjee Nov 24 '14 at 06:56
-
But what's not working? To display new text in Toast it should redraw. – Bracadabra Nov 24 '14 at 14:46
-
By redrawn, I mean the existing toast exits and a new one pops in. I want the already existing toast that I see on screen to be updated. – Debojeet Chatterjee Nov 24 '14 at 17:24
-
Then you did something wrong, I provided sample code that works and existing toast is updated. – Bracadabra Nov 25 '14 at 05:52
-
1Apparently the problem was that I was using XUIMod on my phone to tweak toast entry and exit animations. So it would redraw the toast with the exit and entry animation each time show() was called. Disabling the tweaks fixed it. Thanks. – Debojeet Chatterjee Nov 27 '14 at 04:41
0
These other answers weren't working for me. Maybe its Oreo. This is what people are looking for. Should work in all cases.
EDIT: It should be clarified that original toast will only show as long as the duration allows it. Once the toast length is finished, you cannot call show() on it without re-instantiation.
private Toast mToast;
protected void showToast(int value) {
if (mToast == null) {
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
} else {
mToast.cancel();
mToast = Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
mToast.setText("Shutdown in "+value+" seconds");
mToast.show();
}

DeaMon1
- 572
- 5
- 10