Creating a fullscreen custom Toast
Asked Answered
L

3

14

I have recentrly tried a custom Toast following the tutorial:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView

With such a Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA"
              >
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>

But it seems that the fill_parent in the root layout has no effect.

Do you have an idea on how I can fix this in order to get fullscreen Toast?

Loyola answered 20/10, 2012 at 17:52 Comment(0)
B
23

Add this before you show the Toast:

toast.setGravity(Gravity.FILL, 0, 0);
Blub answered 20/10, 2012 at 18:35 Comment(0)
K
3

To completely fill a toast horizontal and vertical in size of its container you need to use

Gravity.FILL as mentioned in Yoah's answer.

I tried following and it worked.

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.FILL, 0, 0);
toast.setView(view); //inflated view
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
Kelleykelli answered 20/10, 2012 at 18:45 Comment(0)
T
0

You can use this approach:

(custom layout: toast_layout.xml)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#80000000" <!-- Semi-transparent black background -->
    android:gravity="center">

    <TextView
        android:id="@+id/textViewToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF" <!-- White text color -->
        android:textSize="18sp"
        android:textStyle="bold"
        android:padding="16dp"/>
</LinearLayout>

(Back end to handle this Toast)

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class FullScreenToast {

    public static void showFullScreenToast(Context context, String message) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.toast_layout, null);

        TextView textViewToast = layout.findViewById(R.id.textViewToast);
        textViewToast.setText(message);

        Toast toast = new Toast(context);
        toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }
}

(Now using the custom Toast)

FullScreenToast.showFullScreenToast(this, "This is a full-screen toast!");
Takeshi answered 22/1 at 21:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.