VladMorzhanov
9/23/2017 - 2:53 PM

Android custom Toast class.

Android custom Toast class.

package com.main.helpers;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.mevics.main.R;

public class MyToast {

    public static Toast makeText(Activity context, String text, int duration) {

        LayoutInflater inflater = context.getLayoutInflater();
        // Inflate the Layout
        View layout = inflater.inflate(R.layout.mevics_toast,
                (ViewGroup) context.findViewById(R.id.custom_toast_layout));

        TextView textView = (TextView) layout.findViewById(R.id.textToShow);
        // Set the Text to show in TextView
        textView.setText(text);

        Toast toast = new Toast(context);
        toast.setDuration(duration);
        toast.setView(layout);

        return toast;
    }
}