Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Android custom tips

Android comes with Toast, which is very convenient to write, whether it is for development and debugging or for users. However, Toast is really simple for users, and it doesn’t matter if it is simple. The main problem is that sometimes the page color does not match, so it is hard to see clearly. This article explains how to implement a custom Toast.

Toast the queue

Have you ever encountered the phenomenon that when you execute multiple toasts at the same time, the system will display the toasts in sequence? This is because there are actually many new toasts inside the toast. makeText, and the system displays these toasts in sequence in the form of queues. You can see the code proof for run example 1. This is not the case we want, because we want to give prompt immediately, so we have to use a single Toast and then go to setText to update the content, which is the code for example 2.

Example 1 – Circular prompt

for (int i = 0; i < 10; I ++) {toast.maketext (this, "Toast tip :" + I, toast.length_short).show(); }Copy the code

Example 2 – only the most recent

for (int i = 0; i < 10; I ++) {if (toast == null) toast = toast.maketext (this, "toast tip :" + I, toast.length_short); Else {toast.setText("toast prompt :" + I); toast.setDuration(Toast.LENGTH_SHORT); } toast.show(); }Copy the code

Toast custom interface

We usually use Toast, and it only takes one line of code to fix it.

Toast.maketext (this," Toast hint ", toast.length_long).show(); toast.maketext (this," Toast hint ", toast.length_long).show();Copy the code

But if you want to customize the interface, focus on the following Toast methods.

  • SetGravity: Sets the position on the screen where the notification should be displayed.

  • SetView: Sets the view

  • SetDuration: Sets the time for the view to be displayed.

  • Show: display

With these attributes, we first write the Layout file, and then convert the Layout file to View, and then set the View through setView. Let’s try it out.

Start with a layout:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/shape_round_white_border"

android:orientation="vertical">

<TextView

android:id="@+id/toast_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

android:gravity="center"

android:padding="20dp"

android:text=""

android:textColor="#ffffff"

android:textSize="20sp" />

</LinearLayout>

Copy the code

shape_round_white_border

<? The XML version = "1.0" encoding = "utf-8"? > <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp" /> <solid android:color="#dd000000" /> </shape>Copy the code

Toast tool class written, singleton mode instant prompt message.

public class ToastCustom { private static Handler handler = new Handler(Looper.getMainLooper()); private static Toast mToast; Public static void custom(final View View) {final Context Context = AppCache.getContext(); if (context == null) return; handler.post(new Runnable() { @Override public void run() { if (mToast == null) { mToast = new Toast(AppCache.getContext()); } mToast.setGravity(Gravity.CENTER, 0, 0); mToast.setView(view); mToast.setDuration(Toast.LENGTH_LONG); mToast.show(); }}); }}Copy the code

Use it

View layout_view = getLayoutInflater().inflate(R.layout.toast_custom, null); TextView toast_text = layout_view.findViewById(R.id.toast_text); Toast_text.settext (" I am Toast prompt "); ToastCustom.custom(layout_view);Copy the code