By adding @AndroidKiller to the answer, you can also set gravity and custom TextView among other things:
Toast toast = Toast.makeText(context, context.getResources().getString(resID), Toast.LENGTH_LONG); LayoutInflater li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE ); View toastView = li.inflate(R.layout.toast_hint_layout, null); TextView text = (TextView) toastView.findViewById(R.id.hint_text_tv); text.setText(resID); toast.setView(toastView); toast.setDuration(Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toastView.setBackgroundResource(R.drawable.toast_9_patch); toast.show();
Please note that your background resource must have nine PNG patches
You can even put an ImageView and multiple TextView in XML as follows:
<LinearLayout android:id="@+id/layout_root" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:layout_width="32dp" android:layout_height="43dp" android:src="@drawable/lightbulb" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:orientation="vertical" > <TextView android:id="@+id/hint_text_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ccc" android:textSize="14dp" /> <TextView android:id="@+id/hint_text_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(disable hints in preferences)" android:textColor="#555" android:textSize="11dp" /> </LinearLayout> </LinearLayout>
source share