Home>
The default position of toast in android is near the bottom of the screen.This default location is sometimes inappropriate.For example, when there is less content on the page,The content is generally focused on the top half of the screen,The user ’s attention is also focused on the top half of the screen,Toast users in the default location may not notice.It is also possible that the toast in the default position is blocked by the user's hand.In practice, it feels better to display toast in the middle or upper part of the screen.How to modify the default location of toast?Let's do a simple example to demonstrate it.
First screenshot:
The layout file activity_toast.xml code is as follows:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onclick="onclickdefaulttoast"
android:text="click toast to show default position" />
<button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onclick="onclickcentertoast"
android:text="click toast to show centered position" />
<button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onclick="onclicktoptoast"
android:text="click toast to the middle and upper position" />
</linearlayout>
The background toastactivity.java code is as follows:
package chengyujia.demo.aty;
import android.os.bundle;
import android.view.display;
import android.view.gravity;
import android.view.view;
import android.widget.toast;
import chengyujia.demo.r;
public class toastactivity extends baseactivity {
@override
protected void oncreate (bundle savedinstancestate) {
super.oncreate (savedinstancestate);
setcontentview (r.layout.activity_toast);
}
public void onclickdefaulttoast (view v) {
toast.maketext (this, "toast in default position", toast.length_long) .show ();
}
public void onclickcentertoast (view v) {
toast toast=toast.maketext (this, "centered toast", toast.length_long);
toast.setgravity (gravity.center, 0, 0);
toast.show ();
}
public void onclicktoptoast (view v) {
display display=getwindowmanager (). getdefaultdisplay ();
//Get screen height
int height=display.getheight ();
toast toast=toast.maketext (this, "toast in the middle position", toast.length_long);
//This gives a y-axis offset of 1/4 screen height
toast.setgravity (gravity.top, 0, height/4);
toast.show ();
}
}
Related articles
- Super simple implementation of Android custom Toast example (with source code)
- Toast does not display on Android 50 and above
- Solution to the problem that Toast cannot be displayed normally in Android Service
- The Android AndBase framework internally encapsulates the progress box, Toast box, pop-up box, and confirmation box (two)
- Android implements Toast prompt box text coexistence method
- Android programming classic code collection (copy, paste, browser call, Toast display, custom Dialog, etc)
- Android Toast prompt package example code
Trends