How to display Toast at center of screen
AndroidAndroid ToastAndroid Problem Overview
In Android I want to display a toast message at the bottom of the screen, I tried this:
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).show();
It doesn't work, how do I do it correctly?
Android Solutions
Solution 1 - Android
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();
Solution 2 - Android
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.
Solution 3 - Android
Before Android 11, to center a Toast, use:
Kotlin:
val toast = Toast.makeText(context, "Test", Toast.LENGTH_LONG)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()
Java:
Toast toast = Toast.makeText(context, "Test", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Or a custom view.
Warning: starting on Android 11 (R, API 30), you have no non-legacy way to display a centered toast on Android 11 and above:
- the gravity is not working anymore. See https://developer.android.com/reference/android/widget/Toast#setGravity(int,%20int,%20int).
- the custom view is not working, when called from the background (and deprecated anyway). See https://developer.android.com/reference/android/widget/Toast#setView(android.view.View)
Solution 4 - Android
In Xamarin.Android, this displays toast at center of screen:
Toast toast = Toast.MakeText(ApplicationContext, "bbb", ToastLength.Long);
toast.SetGravity(GravityFlags.Center, 0, 0);
toast.Show();
Solution 5 - Android
Layout file for custom toast
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000" />
.java file for custom toast on button's click event
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonToast);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// get your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_layout_id));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Button is clicked!");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
}
Solution 6 - Android
Toast toast = new Toast(context);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
toast.show();
Solution 7 - Android
I have tried almost all the ways to make my Toast in center of screen, but it didn't worked for me anyhow (I am having Android 11 Device). So, I and Ruturaj Rathod found a solution to make a Custom Toast.
Warning: I don't know whether this way is a good practice or not.
Let's see how its made in Android Studio, though its Custom Toast its totally modifiable.
Make a new Layout file for example: toast_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toast_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="10dp"
app:cardCornerRadius="10dp"
app:cardUseCompatPadding="true"
app:cardBackgroundColor="#2d2d2d"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is the custom toast"
android:textSize="20dp"
android:textColor="@color/white"
android:layout_margin="10dp"
/>
</androidx.cardview.widget.CardView>
</LinearLayout>
In your .java file (wherever you want to show your Custom Toast):
View v = View.inflate(this, R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout));
Toast toast = new Toast(MainActivity.this);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(v);
toast.show();
And, your Custom Toast will look like:
Solution 8 - Android
Showing/ Setting text gravity at center (Horizontally) in koltin
fun Context.longToast(msg: String) {
Toast.makeText(this, msg, Toast.LENGTH_LONG)
.apply {
view.findViewById<TextView>(android.R.id.message)?.gravity = Gravity.CENTER
}
.show()
}
Solution 9 - Android
The following code can be used to display Toast message
Toast tt = Toast.makeText(MainActivity.this,"Your text displayed here", Toast.LENGTH_LONG);
tt.setGravity(Gravity.CENTER, 0, 0);
tt.show();
Solution 10 - Android
For kotlin;
val toast = Toast.makeText(this,"Yes clicked...",Toast.LENGTH_LONG)
toast.setGravity(Gravity.CENTER,0,0)
toast.show()
Solution 11 - Android
Toast.makeText(test.this, "bbb", Toast.LENGTH_LONG).apply{
setGravity(Gravity.CENTER, 0, 0)}.show()
This way you can do it in one line.
Solution 12 - Android
Please use this line:
setGravity(Gravity.CENTER, 0, 0)}.show()
this can not work on Android 11
Solution 13 - Android
The below code worked for me.
Toast.makeText(this, "Toast in center", Toast.LENGTH_SHORT).setGravity(Gravity.CENTER,0,0).show();