Android Tutorial On Custom Toast, Dynamic Toast

 

Android Custom Toast :

Android Custom Toast has a simple way of displaying a piece of message in form of a Toast, i.e., a message displayed to user regarding the status of work sometimes values depending upon requirement.

If you want to get full tutorial on simple toast visit Simple Tutorial On Toast

In this tutorial i will show you a custom toast by which we can display a toast on different positions on screen.We can customize the position, color and also can add small icons / emoji in to the toast.

Also you can customize the time for which a toast can be displayed on to the screen based on the message importance can be classified.

You may find few libraries in google but this approach will be safer for you because the libraries may turn incompatible any time.

 

Android Custom Toast Video Tutorial :

Providing the video tutorial also below to make sure you understand the concept much easier.

MainActivity :

Adding 5 buttons to display toast on 5 different sides you may use them according to your requirement.

You can also create a single component and can change the dimensions dynamically to get this approach too.

 

public class MainActivity extends Activity {
   
   Button toptoast,btmtoast,cnttoast,lfttoast,rgttoast;
   View view;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      toptoast = (Button) findViewById(R.id.toptoast);
      btmtoast = (Button) findViewById(R.id.btmtoast);
      cnttoast = (Button) findViewById(R.id.cnttoast);
      lfttoast = (Button) findViewById(R.id.lfttoast);
      rgttoast = (Button) findViewById(R.id.rgttoast);
      
      toptoast.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

            Context context = getApplicationContext();
            LayoutInflater inflater = getLayoutInflater();
             
            view = inflater.inflate(R.layout.toast, null);
             
            Toast toast = new Toast(context);
             
            toast.setView(view);
            toast.setGravity(Gravity.TOP , 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();  
         }
      });
      
      btmtoast.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

            Context context = getApplicationContext();
            LayoutInflater inflater = getLayoutInflater();
             
            view = inflater.inflate(R.layout.toast, null);
             
            Toast toast = new Toast(context);
             
            toast.setView(view);
            toast.setGravity(Gravity.BOTTOM , 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();  
         }
      });
      
      cnttoast.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

            Context context = getApplicationContext();
            LayoutInflater inflater = getLayoutInflater();
             
            view = inflater.inflate(R.layout.toast, null);
             
            Toast toast = new Toast(context);
             
            toast.setView(view);
            toast.setGravity(Gravity.CENTER , 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();  
         }
      });
      
      lfttoast.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

            Context context = getApplicationContext();
            LayoutInflater inflater = getLayoutInflater();
             
            view = inflater.inflate(R.layout.toast, null);
             
            Toast toast = new Toast(context);
             
            toast.setView(view);
            toast.setGravity(Gravity.LEFT , 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();  
         }
      });
      
      rgttoast.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {

            Context context = getApplicationContext();
            LayoutInflater inflater = getLayoutInflater();
             
            view = inflater.inflate(R.layout.toast, null);
             
            Toast toast = new Toast(context);
             
            toast.setView(view);
            toast.setGravity(Gravity.RIGHT , 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.show();  
         }
      });
   }

}

activity_main.xml :

As stated this is a tutorial example we have five buttons to display Android Custom Toast.

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.androidcoding.abhi.customtoast.MainActivity" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="70dp"
        android:layout_marginBottom="10dp"
        android:text="Custom Toast"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    
        
     <Button
        android:id="@+id/toptoast"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Top Toast" />
     
     <Button
        android:id="@+id/btmtoast"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Bottom Toast" />
     
    <Button
        android:id="@+id/cnttoast"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Center Toast"
        android:layout_marginTop="40dp" />
    
    <Button
        android:id="@+id/lfttoast"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Left Toast" />
    
    <Button
        android:id="@+id/rgttoast"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Right Toast" />
    

</LinearLayout>

 

Now we will be creating a custom toast layout

toast.xml :

So this is the interesting thing in this tutorial you can design the toast in this file i.e., you can add the text font, size, color background color and customize accordingly.

You can add icons also to the toast and display.

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
      
   <TextView 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"      
       android:textColor="#000000"        
       android:text="Custom Toast"
       android:layout_gravity="center" />

</LinearLayout>

Android Custom ToastĀ  Output :

The screen depicts the implementation of Android Custom Toast in your app.

Android Custom Toast

If you have any query’s on Android Custom Toast do let us know in comment section below.If you like this tutorial do like and share us for more interesting updates.

 

Show Buttons
Hide Buttons
Read previous post:
Android Tutorial on Shared Preferences

  Android Shared Preferences : Android Shared Preferences are used to store and retrieve data in between android activity'sĀ  it...

Close