Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>My solution for custom Toast :</p> <pre><code>public class MyToast { public static void show(Context context, String text, boolean isLong) { LayoutInflater inflater = LayoutInflater.from(context); View layout = inflater.inflate(R.layout.toast_layout, null); ImageView image = (ImageView) layout.findViewById(R.id.toast_image); image.setImageResource(R.drawable.ic_launcher); TextView textV = (TextView) layout.findViewById(R.id.toast_text); textV.setText(text); Toast toast = new Toast(context); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration((isLong) ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); } } </code></pre> <p>And toast_layout.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/toast_border" android:orientation="horizontal" android:padding="10dp" &gt; &lt;ImageView android:id="@+id/toast_image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:contentDescription="@string/app_name" android:layout_marginRight="10dp" /&gt; &lt;TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:textColor="#FFF" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>And toast_border.xml :</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;shape xmlns:android="http://schemas.android.com/apk/res/android" &gt; &lt;stroke android:width="2dp" android:color="#ee777777" /&gt; &lt;solid android:color="#ee444444" /&gt; &lt;padding android:bottom="2dp" android:left="20dp" android:right="20dp" android:top="2dp" /&gt; &lt;corners android:radius="5dp" /&gt; &lt;/shape&gt; </code></pre> <p>Result in image :</p> <p><img src="https://i.stack.imgur.com/6Hj32.png" alt="enter image description here"></p> <p>I hope I have helped you!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload