how to create custom toast in android studio
custom_toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
app:cardCornerRadius="0dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical">
<View
android:id="@+id/toastColor"
android:layout_width="8dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:layout_toEndOf="@+id/toastColor"
android:orientation="vertical">
<TextView
android:id="@+id/toastTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Success"
android:textColor="#000000"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/toastMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:maxLines="1"
android:text="Custom Toast Message" />
</LinearLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
LearnosetToast.java
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class LearnosetToast extends Toast {
private final Context context;
private LearnosetToastTypes learnosetToastTypes;
private TextView toastTitle;
private TextView toastMessage;
private View toastColor;
public LearnosetToast(Context context) {
super(context);
this.learnosetToastTypes = LearnosetToastTypes.SUCCESS;
this.context = context;
init();
}
private void init() {
// creating layout inflater to get custom Toast view from res/layout folder
final LayoutInflater layoutInflater = LayoutInflater.from(context);
// getting custom Toast view from res/layout folder
final View customView = layoutInflater.inflate(R.layout.custom_toast_layout, null);
// getting title TextView from layout
toastTitle = customView.findViewById(R.id.toastTitle);
// getting message TextView from layout
toastMessage = customView.findViewById(R.id.toastMessage);
toastColor = customView.findViewById(R.id.toastColor);
// setting custom view to Toast
setView(customView);
}
public void setType(LearnosetToastTypes learnosetToastTypes) {
this.learnosetToastTypes = learnosetToastTypes;
}
public void setToastMessage(String message) {
toastMessage.setText(message);
}
public void show() {
if (learnosetToastTypes == LearnosetToastTypes.SUCCESS) {
toastTitle.setText("Success");
toastColor.setBackgroundColor(context.getResources().getColor(android.R.color.holo_green_light));
} else if (learnosetToastTypes == LearnosetToastTypes.FAILED) {
toastTitle.setText("Failed!");
toastColor.setBackgroundColor(context.getResources().getColor(android.R.color.holo_red_light));
} else if (learnosetToastTypes == LearnosetToastTypes.WARNING) {
toastTitle.setText("Warning!");
toastColor.setBackgroundColor(Color.YELLOW);
} else if (learnosetToastTypes == LearnosetToastTypes.INFO) {
toastTitle.setText("Info");
toastColor.setBackgroundColor(context.getResources().getColor(android.R.color.holo_blue_light));
} else if (learnosetToastTypes == LearnosetToastTypes.ERROR) {
toastTitle.setText("Error!");
toastColor.setBackgroundColor(context.getResources().getColor(android.R.color.holo_red_light));
}
super.show();
}
public enum LearnosetToastTypes {
SUCCESS,
FAILED,
WARNING,
INFO,
ERROR,
}
}
MainActivity.java
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 102;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// creating LearnosetToast object
LearnosetToast learnosetToast = new LearnosetToast(MainActivity.this);
learnosetToast.setDuration(Toast.LENGTH_LONG); // set Toast duration
learnosetToast.setGravity(Gravity.BOTTOM, 0, 0); // set gravity
learnosetToast.setType(LearnosetToast.LearnosetToastTypes.ERROR); // set Toast type. must be success, error, failed, etc.
learnosetToast.setToastMessage("Your custom Message goes here"); // Custom message for toast
learnosetToast.show(); // show toast
}
}
Output
Projects with Source Code + Video Tutorials
You can download our Java and Android Studio Projects with Source Code and Video Tutorials.
Simple Music Player App (Version 2) for Android (Offline Version)
Login and Register Activity Material UI design in Android Studio
Bottom Navigation Bar with Fragments + Animation
If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com
Follow us to learn Coding and get in touch with new Technologies.