- First of all add this code in
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.DrawableRes
import com.example.dorbbytask.R
object FancyToast {
const val SUCCESS = 1
const val WARNING = 2
const val ERROR = 3
const val INFO = 4
const val DEFAULT = 5
const val CONFUSING = 6
const val LENGTH_SHORT = Toast.LENGTH_SHORT
// const val LENGTH_LONG = Toast.LENGTH_LONG
fun makeText(
context: Context,
message: CharSequence,
type: Int,
@DrawableRes imageResource: Int? = null,
): Toast {
val toast = Toast(context)
val layout = LayoutInflater.from(context).inflate(R.layout.fancytoast_layout, null)
val textView: TextView = layout.findViewById(R.id.toast_text)
val linearLayout: LinearLayout = layout.findViewById(R.id.toast_type)
val imgIcon: ImageView = layout.findViewById(R.id.toast_icon)
// Set message
textView.text = message
// Set background and icon based on type
setLayoutAndIcon(type, linearLayout, imgIcon)
// Set custom drawable if provided
imageResource?.let {
imgIcon.setImageResource(it)
}
// Set the custom view
toast.view = layout
toast.duration = LENGTH_SHORT
toast.show() // Show the toast immediately after creation
return toast
}
private fun setLayoutAndIcon(
type: Int,
linearLayout: LinearLayout,
imgIcon: ImageView
) {
val drawableRes = when (type) {
SUCCESS -> {
linearLayout.setBackgroundResource(R.drawable.success_shape)
R.drawable.ic_check_black_24dp
}
WARNING -> {
linearLayout.setBackgroundResource(R.drawable.warning_shape)
R.drawable.ic_pan_tool_black_24dp
}
ERROR -> {
linearLayout.setBackgroundResource(R.drawable.error_shape)
R.drawable.ic_clear_black_24dp
}
INFO -> {
linearLayout.setBackgroundResource(R.drawable.info_shape)
R.drawable.ic_info_outline_black_24dp
}
DEFAULT -> {
linearLayout.setBackgroundResource(R.drawable.default_shape)
imgIcon.visibility = View.GONE
return
}
CONFUSING -> {
linearLayout.setBackgroundResource(R.drawable.confusing_shape)
R.drawable.ic_refresh_black_24dp
}
else -> {
linearLayout.setBackgroundResource(R.drawable.default_shape)
R.drawable.ic_refresh_black_24dp
}
}
imgIcon.setImageResource(drawableRes)
}
}
- In Activity you have to enter only
FancyToast.makeText(requireContext(), "Success!", FancyToast.SUCCESS)
- Now , you have to paste this code after creating fancytoast_layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:id="@+id/toast_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/success_shape"
android:paddingLeft="16dp"
android:paddingTop="8dp"
android:paddingRight="24dp"
android:paddingBottom="8dp"
android:paddingStart="16dp"
android:paddingEnd="24dp">
<ImageView
android:id="@+id/toast_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:src="@drawable/ic_info_outline_black_24dp" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
tools:text="Hi! This is the sample toast example"
android:textColor="#FFFFFF" />
</LinearLayout>
<ImageView
android:id="@+id/imageView4"
android:layout_width="28dp"
android:layout_height="28dp"
android:visibility="gone"
android:layout_alignEnd="@+id/toast_type"
android:layout_alignParentTop="true"
android:src="@drawable/androidicon"
android:layout_alignRight="@+id/toast_type" />
</RelativeLayout>
- Download these drawable files
here is the link
https://drive.google.com/drive/folders/1afrSagYNII9kVMKqwkF3VYSJ5f7e79gM?usp=sharing
- For java use this
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import com.example.dorbbytask.R;
public class FancyToast {
public static final int SUCCESS = 1;
public static final int WARNING = 2;
public static final int ERROR = 3;
public static final int INFO = 4;
public static final int DEFAULT = 5;
public static final int CONFUSING = 6;
public static final int LENGTH_SHORT = Toast.LENGTH_SHORT;
public static Toast makeText(
Context context,
CharSequence message,
int type,
@Nullable @DrawableRes Integer imageResource
) {
Toast toast = new Toast(context);
View layout = LayoutInflater.from(context).inflate(R.layout.fancytoast_layout, null);
TextView textView = layout.findViewById(R.id.toast_text);
LinearLayout linearLayout = layout.findViewById(R.id.toast_type);
ImageView imgIcon = layout.findViewById(R.id.toast_icon);
// Set message
textView.setText(message);
// Set background and icon based on type
setLayoutAndIcon(type, linearLayout, imgIcon);
// Set custom drawable if provided
if (imageResource != null) {
imgIcon.setImageResource(imageResource);
}
// Set the custom view
toast.setView(layout);
toast.setDuration(LENGTH_SHORT);
toast.show(); // Show the toast immediately after creation
return toast;
}
// Overload without the imageResource parameter
public static Toast makeText(
Context context,
CharSequence message,
int type
) {
return makeText(context, message, type, null);
}
private static void setLayoutAndIcon(
int type,
LinearLayout linearLayout,
ImageView imgIcon
) {
int drawableRes;
switch (type) {
case SUCCESS:
linearLayout.setBackgroundResource(R.drawable.success_shape);
drawableRes = R.drawable.ic_check_black_24dp;
break;
case WARNING:
linearLayout.setBackgroundResource(R.drawable.warning_shape);
drawableRes = R.drawable.ic_pan_tool_black_24dp;
break;
case ERROR:
linearLayout.setBackgroundResource(R.drawable.error_shape);
drawableRes = R.drawable.ic_clear_black_24dp;
break;
case INFO:
linearLayout.setBackgroundResource(R.drawable.info_shape);
drawableRes = R.drawable.ic_info_outline_black_24dp;
break;
case DEFAULT:
linearLayout.setBackgroundResource(R.drawable.default_shape);
imgIcon.setVisibility(View.GONE);
return;
case CONFUSING:
linearLayout.setBackgroundResource(R.drawable.confusing_shape);
drawableRes = R.drawable.ic_refresh_black_24dp;
break;
default:
linearLayout.setBackgroundResource(R.drawable.default_shape);
drawableRes = R.drawable.ic_refresh_black_24dp;
break;
}
imgIcon.setImageResource(drawableRes);
}
}
0 Comments