home top ad

How to create custom and normal toast in andriod

Hello Guys, today we are learn about toast . Toast is a small poppup notifications that is used to display information about the Operation which we performed in our app. The Toast will show the message for a small period of time and it will disappear automatically after a timeout.
Generallly, we can customize the toast that we see on the display very easily. To implement it, you have to create a layout that way you can design the toast the way you want to design it.

Whenever you print a toast on some error then you use a normal toast in which you can print the toast by duration like LENGTH_SHORT and LENGTH-LONG but if you want to print in a special way, then you have to make a custom toast Should be used.

Create New Custom Toast Project

Choose "File", "New", select "New Project" and then click "Next". In the New Android Application window, enter your chosen Application, Project, and Package names and then choose "Next" and last finish. After creating the project, you will see two files, first activity_main and second MainActivity.java, then you have to paste the code given below activity_main and MainActivity.java.

activity_main .xml

 <?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="match_parent"  
   android:orientation="vertical"  
   android:padding="20dp">  
   <TextView  
     android:id="@+id/textView"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:text="Enter Toast Message"  
     android:textAppearance="?android:attr/textAppearanceLarge"  
     android:textColor="#293DBD"  
     android:textStyle="bold" />  
   <EditText  
     android:id="@+id/toast_editText"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_above="@+id/toast_button"  
     android:layout_alignStart="@+id/textView"  
     android:layout_alignLeft="@+id/textView"  
     android:layout_alignEnd="@+id/textView"  
     android:layout_alignRight="@+id/textView"  
     android:hint="Enter Text Here"  
     android:layout_marginBottom="46dp" />  
   <Button  
     android:id="@+id/toast_button"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_centerVertical="true"  
     android:text="Create Normal Toast" />  
   <Button  
     android:id="@+id/custom_button"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_centerVertical="true"  
     android:layout_below="@id/toast_button"  
     android:layout_marginTop="10dp"  
     android:text="Create Custom Toast" />  
 </RelativeLayout>  
Here we have two buttons, EditText and one textView, in which we will enter a message from the user in edittext Which we will print as normal toast and custom toast, ie the user who enters the message, we will print it as normal and custom toast.

MainActivity.java

 import androidx.appcompat.app.AppCompatActivity;  
 import android.content.Intent;  
 import android.os.Bundle;  
 import android.view.Gravity;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.TextView;  
 import android.widget.Toast;  
 public class MainActivity extends AppCompatActivity {  
    private EditText editText;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     // We define our button and define to the button in the layout.  
     // Now I can access and use the button in Layout  
      editText = findViewById(R.id.toast_editText);  
     Button button = (Button) findViewById(R.id.toast_button);  
     button.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         // We define our EditText and define to the EditText in the layout.  
         // Now I can access and use the EditText in Layout  
           editText = findViewById(R.id.toast_editText);  
          //I get the data in EditText. I'm transferring to variable  
          String inputText= editText.getText().toString();  
          //If the data in EditText is empty or null  
         // Let's print something to Toast as a warning  
          if(inputText.isEmpty())  
          {  
           inputText="You did not write a message";  
           // Toast.makeText(MainActivity.this, "Please Enter Message For Toast", Toast.LENGTH_SHORT).show();  
          }  
          else  
            { inputText= editText.getText().toString().trim(); }  
          // Create our Toast Message  
          Toast.makeText(getApplicationContext(), inputText, Toast.LENGTH_SHORT).show();  
       }  
     });  
     Button custom=findViewById(R.id.custom_button);  
     custom.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         //Create an object from the Toast class  
         TextView textView=findViewById(R.id.textRec);  
         Toast toast = new Toast(getApplicationContext());  
         //Make the place on the screen //It takes three values, type of int  
         // first position, The other two values is the right or left and up or down  
         // Negatives use for to up and to left  
         toast.setGravity(Gravity.CENTER, 0, -250);  
         //wait time //Two values are available, long 2 sec short 1 sec  
         toast.setDuration(Toast.LENGTH_SHORT);  
         //add layout to inside //We first need to create an Inflater //select our layout for our toast in Layout folder  
         LayoutInflater inf = getLayoutInflater(); //Now let's define our layout //Let's give it an id for access to our toast layout,            //From R.java, I have the layout and id values in the layout and id sections  
         View layout = inf.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.showid)); // Now set as View  
         TextView text=layout.findViewById(R.id.textRec);  
         String recieved=editText.getText().toString().trim();  
         if (recieved.isEmpty())  
         {  
           recieved="You did not write a message";  
         }  
         else  
         {  
           text.setText(recieved);  
         }  
         Toast.makeText(MainActivity.this, recieved, Toast.LENGTH_SHORT).show();  
         toast.setView(layout); //I show now  
         toast.show();  
       }  
     });  
   }  
 }  
Now we will use activity_main in the MainActivity class First of all, we will create edittext and variable of both buttons and run the setOnClickListener method on it.
First of all we will run the OnClickListener method on the first button, in that we will take editText.getText.toString () in a StringType variable. After that we will check the value that is taken in the StringType variable in the if condition that this value is If there is no empty or if there is a null, then we will print a toast to say "please write a message" then we will print the message which the user has entered by edittext in the else condition.
To apply the onClickListener method to the Custom button, first of all we have to create a layout. To create this layout, first we need to right click on the layout folder and then click on the layout resource file and create an xml file like this picture. And paste the following code in it, remember one thing, the layout name given in MainActivity is the same name as xml or else the error will come.

custom_toast.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/showid"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent">  
   <LinearLayout  
     android:layout_width="310dp"  
     android:layout_height="100dp"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="271dp"  
     android:weightSum="2"  
     android:background="@drawable/shape"  
     android:elevation="5dp"  
     android:orientation="horizontal">  
     <ImageView  
       android:layout_width="50dp"  
       android:layout_height="45dp"  
       android:src="@drawable/ic_error"  
       android:layout_marginLeft="15dp"  
       android:layout_marginTop="28dp"  
       android:layout_marginStart="15dp" />  
     <TextView  
       android:id="@+id/textRec"  
       android:layout_width="wrap_content"  
       android:layout_height="40dp"  
       android:fontFamily="serif-monospace"  
       android:layout_marginTop="32dp"  
        android:text="No any Message"  
       android:textColor="@color/white"  
       android:textSize="26sp"  
       android:textStyle="normal|bold"/>  
   </LinearLayout>  
 </RelativeLayout>  
In this xml you will see two errors, first to fix the error of the shape and src image, you will have to right-click on the drawable and click on the drawable resource file and create the shape and in that you will have to paste this code in it.

shape.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <selector xmlns:android="http://schemas.android.com/apk/res/android">  
   <item android:state_pressed="false" android:state_selected="false">  
     <shape android:shape="rectangle">  
       <corners android:radius="10dp" />  
       <solid android:color="#26EF03" />  
       <stroke android:width="8dp"  
         android:color="@color/purple_700"/>  
     </shape>  
   </item>  
 </selector>  
I hope this article works for you
Thanks ...

Post a Comment

0 Comments