I’m creating my custom toast mainly because I place it on a grid view and need some control on it’s width. But I really do like the lightblue halo on the standard toast and would like to get that back (or at least have my custom toast look somewhat similar to the default toast).
What’s an easy / elegant way to achieve that ?
I created a custom toast from this layout:
<LinearLayout
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:padding="10dp">
<ImageView android:id="@+id/toast_layout_image"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:src="@drawable/icon" android:layout_marginRight="10dp">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/toast_layout_text"
android:textColor="#FFF" android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</LinearLayout>
and invoke it this way (common approach as outlined in the android dev. documentation as well as a couple of tutorials on the web, only difference is that I work with a grid view and each toast is placed according to its grid tile position):
int location[] = new int[2];
view.getLocationOnScreen(location);
LayoutInflater inflater = getLayoutInflater();
View toastLayout = inflater.inflate(R.layout.toast_layout,(ViewGroup)findViewById(R.id.toast_layout_root));
TextView textView = (TextView) toastLayout.findViewById(R.id.toast_layout_text);
textView.setText("This tile does not contain any valid data.");
int toastWidth = cellWidth * 3 / 4;
textView.setWidth(toastWidth - 64); // TODO picture size needs to be correct
Toast toast = new Toast(getApplicationContext());
toast.setView(toastLayout);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.LEFT, location[0]+((cellWidth-toastWidth)/2),location[1]+cellHeight-100);
toast.show();
This looks very clean but totally different from the default toast..
apply this layout for your toast message:
I don’t found what are cellWidth and cellHeight, so try to experiment with padding if it will not fit
Edit: Oh, I applied dark theme, try to use light instead.