How to Display Toast in Android

How to display Toast in Android?

In order to display Toast in your application, try this:

Toast.makeText(getActivity(), (String)data.result, 
Toast.LENGTH_LONG).show();

Another example:

Toast.makeText(getActivity(), "This is my Toast message!",
Toast.LENGTH_LONG).show();

We can define two constants for duration:

int LENGTH_LONG Show the view or text notification for a long period
of time.

int LENGTH_SHORT Show the view or text notification for a short period
of time.

Customizing your toast

LayoutInflater myInflater = LayoutInflater.from(this);
View view = myInflater.inflate(R.layout.your_custom_layout, null);
Toast mytoast = new Toast(this);
mytoast.setView(view);
mytoast.setDuration(Toast.LENGTH_LONG);
mytoast.show();

How do you display a Toast using Kotlin on Android?

It can be an extension function for Context:

fun Context.toast(message: CharSequence) = 
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()

You can place this anywhere in your project, where exactly is up to you. For example, you can define a file mypackage.util.ContextExtensions.kt and put it there as a top level function.

Whenever you have access to a Context instance, you can import this function and use it:

import mypackage.util.ContextExtensions.toast

fun myFun(context: Context) {
context.toast("Hello world!")
}

Display string variables in a Toast

If you want to show all content of EditText in one Toast, then just use concatenation

String concatenatedText = str1 + str2 + str3 + ... 

or StringBuilder class:

StringBuilder sb = new StringBuilder();
sb.append(str1);
sb.append(str2);
sb.append(str3);
String concatenatedText = sb.toString();

and simply pass the result as second argument, as below:

Toast toast = Toast.makeText(context, concatenatedText, duration);

How to display a Toast message in the top of the page in my android application

You need add this as programmatically like this in your code:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

How to display Toast at center of screen

To display the Toast in center of the screen.

Toast toast = Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

How to change position of Toast in Android?

From the documentation,

Positioning your Toast

A standard toast notification appears near the bottom of the screen,
centered horizontally. You can change this position with the
setGravity(int, int, int) method. This accepts three parameters: a
Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the
top-left corner, you can set the gravity like this:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

If you want to nudge the position to the right, increase the value of
the second parameter. To nudge it down, increase the value of the last
parameter.

how to show one time toast messages in android studio

You can do it with SharedPreferences that you store in another class like Utility.java:

public class Utility {

public static SharedPreferences preferences(Context context) {
return PreferenceManager.getDefaultSharedPreferences(context);
}

public static Boolean hasSendToast(Context context) {
return preferences(context).getBoolean("Toast", false);
}

public static void setSendToast(Context context, Boolean bool) {
preferences(context).edit()
.putBoolean("Toast", bool).apply();
}
}

And use it with your Toast inside the onClickListener in your MainActivity.java like this:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = findViewById(R.id.button);
button.setOnClickListener(v -> {
if (!Utility.hasSendToast(getApplicationContext())) {
Toast.makeText(getApplicationContext(), "My Toast", Toast.LENGTH_SHORT)
.show();
Utility.setSendToast(getApplicationContext(), true);
}
});
}
}

Show toast widget underneath a view

From what I learned, this is the best I came up with for the program. Doing this puts the Toast message in the center of the phone.

if(input > 100) {   
Toast toast = Toast.makeText(MainActivity.this,
R.string.over_guess,
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return;
}
else if(input < comp) {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.guess_low, input),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else if(input > comp) {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.guess_high, input),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
else {
Toast toast = Toast.makeText(MainActivity.this,
getString(R.string.correct, comp, guesses),
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}

Android Toast message doesn't show

Perhaps you have accidentially disabled notifications for your app in the settings? This causes no toasts too.

How to display Toast from a Service after main Activity finishes?

OnHandleIntent will run in a differant Thread
so you are showing Toast in a thread which is not allowed in android

so change your code like this

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {

@Override
public void run() {
Toast.makeText(getApplicationContext(),
getString(R.string.car_opened),
Toast.LENGTH_SHORT).show();
}
});

From this dead thread in service

IntentService will create a thread to handle the new intent, and terminated it immediately once the task has done. So, the Toast will be out of controlled by a dead thread.

You should see some exceptions in the console when the toast showing on the screen.



Related Topics



Leave a reply



Submit