1

I'm able to fetch every (default) toast message, but I want to add the app icon to it. How can I get the position from the toast view/widget?

My toast catcher is an AccessibilityService and I was hoping that the AccessibilityEvent has something useful in it, but I didn't find a property with the needed data.

Edit: CyanogenMod added it and I want to implement it into stock android with my app.

Example toast

mars3142
  • 2,501
  • 4
  • 28
  • 58

3 Answers3

0

You can change the position of Toast Message

setGravity(int gravity, int xOffset, int yOffset)

As you know your Toast message position you can simply set your icon position accordingly doc

Zubair Akber
  • 2,760
  • 13
  • 30
0

Can you please try Custom toast ?

Screen shot attached for the same.

enter image description here

custom_toast_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toastParent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#F08080"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/app_name" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />

</LinearLayout>

In Java class:

private void displayToast(){
   LayoutInflater inflater = getLayoutInflater();
   View layout = inflater.inflate(R.layout.custom_toast_view, (ViewGroup) findViewById(R.id.toastParent));
   ImageView image = (ImageView) layout.findViewById(R.id.image);
   image.setImageResource(R.drawable.ic_launcher);
   TextView text = (TextView) layout.findViewById(R.id.text);
   text.setText("Hello! This is a custom toast!");
   Toast toast = new Toast(getApplicationContext());
   toast.setDuration(Toast.LENGTH_LONG);
   toast.setView(layout);
   toast.show();
}

Hope this will help you.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
  • My app (https://play.google.com/store/apps/details?id=org.mars3142.android.toaster) will catch every toast. So, I can't use custom layouts. I don't want to disable the original toast and show a custom one – mars3142 Sep 15 '15 at 11:31
0

With the following code you can get default Toast Y offset from bottom of the screen:

int i = Resources.getSystem().getIdentifier("toast_y_offset", "dimen", "android");
int bottomOffset = getResources().getDimensionPixelSize(i);

Then just place your image at the bottom of the screen with bottom padding == bottomOffset.

Answer on "where do I get the left position?":

Toast uses android:layout_gravity="center_horizontal" to align text and default text size. So you can calculate how many space this text would take (if you know what the exact text would be) : Calculating width of a string on Android . Then left offset can be calculated as (screenWidth - textWidth) / 2.

Community
  • 1
  • 1
amukhachov
  • 5,822
  • 1
  • 41
  • 60