Joe Joe - 1 year ago 105
Android Question

Toast with custom message

I start to learn Android programming, and now I try to display a toast with a custom string.

Random r = new Random();
int i = r.nextInt(100 - 90 + 1) + 90;
String message = String.format(r);

Toast.makeText(getApplicationContext(), "@".replace(message), Toast.LENGTH_LONG).show();

Any ideas what I'm doing wrong?

Answer Source

Even though you've found the answer yourself, I still want to provide some examples to make sure that you understand how String#format(String, Object...) works:

Random r = new Random();
String message = null;

int i = r.nextInt(100 - 90 + 1) + 90;
message = String.format("%d", i);

float f = 0.1;
message = String.format("%f", f);

String s = "Hello world";
message = String.format("%s", s);

// "Hello world, f=0.1"
message = String.format("%s, f=%f", s, f);

More explanation about java formatting can be reached at:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download