MainActivity XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btSuccess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUCCESS"
android:layout_gravity="center"
android:backgroundTint="@color/white"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_marginTop="200dp"/>
<Button
android:id="@+id/btFailed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FAILED"
android:backgroundTint="@color/white"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btWarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WARNING"
android:backgroundTint="@color/white"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INFO"
android:backgroundTint="@color/white"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/btError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ERROR"
android:backgroundTint="@color/white"
android:textColor="@color/black"
android:textStyle="bold"
android:textSize="20sp"
android:layout_gravity="center"
android:layout_marginTop="10dp"/>
</LinearLayout>
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
package com.rbmjltd.customtoast;
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
package com.rbmjltd.customtoast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 102;
Button btSuccess,btFailed,btWarning,btInfo,btError;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btSuccess = findViewById(R.id.btSuccess);
btFailed = findViewById(R.id.btFailed);
btWarning = findViewById(R.id.btWarning);
btInfo = findViewById(R.id.btInfo);
btError = findViewById(R.id.btError);
btSuccessResult();
btFailedResult();
btWarningResult();
btInfoResult();
btErrorResult();
} //=== OnCreate Bundle End hear ======================================
private void btErrorResult() {
btError.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LearnosetToast learnosetToast = new LearnosetToast(MainActivity.this);
learnosetToast.setDuration(Toast.LENGTH_LONG);
learnosetToast.setGravity(Gravity.BOTTOM, 0, 0);
learnosetToast.setType(LearnosetToast.LearnosetToastTypes.ERROR);
learnosetToast.setToastMessage(getString(R.string.ERROR));
learnosetToast.show();
}
});
}
private void btInfoResult() {
btInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LearnosetToast learnosetToast = new LearnosetToast(MainActivity.this);
learnosetToast.setDuration(Toast.LENGTH_LONG);
learnosetToast.setGravity(Gravity.BOTTOM, 0, 0);
learnosetToast.setType(LearnosetToast.LearnosetToastTypes.INFO);
learnosetToast.setToastMessage(getString(R.string.INFO));
learnosetToast.show();
}
});
}
private void btWarningResult() {
btWarning.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LearnosetToast learnosetToast = new LearnosetToast(MainActivity.this);
learnosetToast.setDuration(Toast.LENGTH_LONG);
learnosetToast.setGravity(Gravity.BOTTOM, 0, 0);
learnosetToast.setType(LearnosetToast.LearnosetToastTypes.WARNING);
learnosetToast.setToastMessage(getString(R.string.WARNING));
learnosetToast.show();
}
});
}
private void btFailedResult() {
btFailed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LearnosetToast learnosetToast = new LearnosetToast(MainActivity.this);
learnosetToast.setDuration(Toast.LENGTH_LONG);
learnosetToast.setGravity(Gravity.BOTTOM, 0, 0);
learnosetToast.setType(LearnosetToast.LearnosetToastTypes.FAILED);
learnosetToast.setToastMessage(getString(R.string.FAILED));
learnosetToast.show();
}
});
}
private void btSuccessResult() {
btSuccess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LearnosetToast learnosetToast = new LearnosetToast(MainActivity.this);
learnosetToast.setDuration(Toast.LENGTH_LONG);
learnosetToast.setGravity(Gravity.BOTTOM, 0, 0);
learnosetToast.setType(LearnosetToast.LearnosetToastTypes.SUCCESS);
learnosetToast.setToastMessage(getString(R.string.SUCCESS));
learnosetToast.show();
}
});
}
}