1

I am wondering how do we create a Custom toast in Android that shows on the top of screen like this?

enter image description here

I came from iOS background and now I have to create custom controls like this.

Any pointers please?

Thank you

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
JayVDiyk
  • 4,277
  • 22
  • 70
  • 135

5 Answers5

3

Check this link: https://github.com/gfranks/GFMinimalNotifications, this is what you want i think and it is working fine for me.

output:

enter image description here

Or you can prepare custom toast like this:

View layout = getLayoutInflater().inflate(R.layout.customtoast,
            (ViewGroup) findViewById(R.id.custom_toast_layout));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 0);
toast.setView(layout);
toast.show();
Srikanth
  • 1,555
  • 12
  • 20
2

you can use crouton for this purpose.

Description :

A Crouton will be displayed at the position the developer decides. Standard will be the top of an application window. You can line up multiple Croutons for display, that will be shown one after another

Create a Crouton for any CharSequence:

Crouton.makeText(Activity, CharSequence, Style).show();

more details on Crouton, a Context sensitive notifications for Android

there are many you can use for toast describe here

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
1

try this ::

Toast toast= Toast.makeText(getApplicationContext(), 
"Your string here", Toast.LENGTH_SHORT);  
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();

add this line for position

 toast.setGravity(Gravity.TOP, 0, 0);
curiousMind
  • 2,812
  • 1
  • 17
  • 38
0

creat customize toast layout, toast.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:background="@color/yellow"
      android:layout_width="match_parent"
      android:padding="8dp"
      android:gravity="center"
      android:textSize="32sp"
      android:layout_height="256dp"/>

create toast using the layout above:

    static Toast t;

public static void show(Context c, String s) {
    if (t == null) {
        t = new Toast(c.getApplicationContext());
        t.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);
        LayoutInflater inflater = LayoutInflater.from(c);
        TextView v = (TextView) inflater.inflate(R.layout.toast, null);
        t.setView(v);
        t.setDuration(Toast.LENGTH_LONG);
    }
    TextView v = (TextView) t.getView();
    v.setText(s);
    t.show();
}
-1

As you can see, there are many solutions. But the easiest way is create a layout in your XML layout file, and make it invisible. When this view needs to be shown make it visible and start an animation.

APC
  • 144,005
  • 19
  • 170
  • 281