Android toast not suitable for text

I am developing an application where I have to use numerous toasts.

I show these toasts using:

Toast.makeText(context, "Some medium-sized text", Toast.LENGTH_SHORT).show(); 

The display toaster, however, has a height of one line, and the text is displayed on several lines. As a result, I can’t see all the text in the toast.

How can i fix this?

+6
source share
2 answers

Try inserting a carriage return and a line where you want to split the text.

These characters refer to old typewriter models. The carriage return was that the cylinder moved back to the start, and the feed line is rolling the cylinder (feed) in one line.

In the calculation, they are represented by two escaped characters (special codes that allow non-printable codes inside a string by prefixing them with a backslash \ ).

  • Carriage return is presented \r
  • Line feed is represented \ \n (you can remember this as a new line).

For some non-Unix systems (for example, for Windows) for others (for example, for Linux, on which Android is installed) only a new line is required, but it is usually safe to do this everywhere. The only thing that matters is the order in which they are located. It should be \r\n

Put this in your example:

 Toast.makeText(context, "First line of text\r\nSecond line of text", Toast.LENGTH_SHORT).show(); 

On Android, you can only reduce this to the new line character \n , since unix-based systems are not so fussy:

 Toast.makeText(context, "First line of text\nSecond line of text", Toast.LENGTH_SHORT).show(); 
+28
source

Using the basic idea of ​​this Custom toast in android: a simple example and Standard colors for Android Toast and alpha I developed a simple custom toast that looks like the default, but it wraps the text in multi-lines.

I am creating a simple class with the static method makeText(context,text,duration) , so I had to replace Toast.makeText with CustomToast.makeText everywhere in my projects.

Below code

CustomToast.java

 public class CustomToast extends Toast{ /** * Construct an empty Toast object. You must call {@link #setView} before you * can call {@link #show}. * * @param context The context to use. Usually your {@link Application} * or {@link Activity} object. */ public CustomToast(Context context) { super(context); } public static Toast makeText(Context context, CharSequence text, int duration) { Toast t = Toast.makeText(context,text,duration); LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); View layout = inflater.inflate(R.layout.custom_toast,null); TextView textView = (TextView) layout.findViewById(R.id.text); textView.setText(text); t.setView(layout); return t; } } 

Layout layout/custom_toast.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_layout_id" android:background="@android:drawable/toast_frame" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_horizontal|center_vertical" android:padding="5dp" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_gravity="center_horizontal|center_vertical" android:layout_height="wrap_content" android:singleLine="false" android:layout_weight="1" android:textAppearance="@android:style/TextAppearance.Small" android:textColor="@android:color/background_light" android:shadowColor="#BB000000" android:shadowRadius="2.75"/> </LinearLayout> 
0
source

Source: https://habr.com/ru/post/893973/


All Articles