Advertisement

Responsive Advertisement

CustomToast In Java And Kotlin

  1.  Xml Layout For Toast
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
android:layout_marginVertical="10dp"
android:backgroundTint="#ACCEEA"
android:orientation="vertical"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">

<TextView
android:id="@+id/txt_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:textAlignment="center"
android:layout_gravity="center"
android:textColor="#373535"
android:textSize="16sp"
android:textStyle="bold"
tools:text="Toast Message" />

</androidx.cardview.widget.CardView>

  1. Using Java
package com.example.firebaseproject;

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

public class CustomToast extends Toast {
private Context context;
private String msg;

public CustomToast(Context context,String msg) {
super(context);
this.context=context;
this.msg=msg;
View view= LayoutInflater.from(context).inflate(R.layout.customtoastlayout_row,null);
TextView txtMsg = view.findViewById(R.id.txt_message);
txtMsg.setText(msg);
setView(view);
setDuration(Toast.LENGTH_SHORT);
}
}

// How'll Use
new CustomToast(JavaFirebaseActivity.this,"Toast Hu Bhai Dikhunga Hi").show();


  1. Using Kotlin

public class CustomToast1(private val context: Context, private val msg: String) :
Toast(context) {
init {
val view = LayoutInflater.from(context).inflate(R.layout.customtoastlayout_row, null)
val txtMsg = view.findViewById<TextView>(R.id.txt_message)
txtMsg.text = msg
setView(view)
duration = LENGTH_SHORT
}
}
// How'll Use
val myToast = CustomToast1(applicationContext,"Nahi Chal Gya Toast Bhai")
myToast.show()


Post a Comment

0 Comments