Toast In Android Studio

Toast in android studio

Toast in the android studio provides you a simple interface of any feedback or operation done. Assume you developed a social media app and want to show welcome to the user whenever the user logs in, So you can use Toast to show this.

Toast in the android studio provides simple feedback in the form of popup. Read about List View Here

One of the best things of toast is it covers only the amount of space required to show the message.

But the bad part about the toast is you can not implement click listener on toast.

Toast automatically disappears after some time after showing a message on the screen.

In this post we are going to see each and every function of toast in android studio

Creating Toast in Android Studio

Creating Toast in the android studio is very simple, All you need to do is just use Toast.make text() function like this,

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Toast toast =  Toast.makeText(getApplicationContext(),"GeeksToCode",Toast.L ENGTH_SHORT);
       toast.show();
    }
}
Toast In Android Studio

We instantiate the Toast object with makeText() method and pass three argument’s

Context: It allows access to application-specific resources and classes, as well as up-calls for application-level operations, such as launching activities, broadcasting and receiving intents

String: This is the message you want to show in the toast

Duration: Duration up to which you want to show the Toast. Basically there is two duration present one is LENGTH_SHORT and the other is LENGTH_LONG.

Changing Position of Toast in Android studio

By Default, the toast will be shown always in the center of the bottom but the best thing is that you can change your toast position.

Changing the toast position is very simple by using the setGravity() method.

You can change toast position like this

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       Toast toast =  Toast.makeText(getApplicationContext(),"GeeksToCode",Toast.LENGTH_SHORT);
       toast.setGravity(Gravity.TOP,10,10);
       toast.show();
    }
}

Now, when you run this program you will see something like this,

Toast in Android Studio
Toast in Android Studio

Toast is shown in the top position at the center.

Toast.setGravity() takes three parameter which is explained below

Gravity: Gravity of toast

xOffSet: X co-ordinate of toast

yOffSet: Y co-ordinate of toast

Custom Toast in Android Studio

Android studio provides you a way to design your custom toast and use it as you want.

Designing a custom toast is very easy, all you need to create a new layout resource file and use it as toast.

Don’t worry we explain it to you step by step. Firstly Right-click on layout and then new and then Layout Resource File

Name Resource file as custom_toast.xml, now, Add following code to the .xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GeeksToCode"
        android:layout_margin="20dp"
        android:textSize="30dp"
        android:layout_gravity="center"
        android:textColor="@color/colorPrimary"/>

</LinearLayout>

So, what we do is simply add a text view which we will be used to show the message. We set text as GeeksToCode and also give text size and text color.

Now, we need to use this layout as custom toast, for this, we need to Create a Toast object and set this layout as a view for Toast like this.

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);

        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_toast, null);
        toast.setView(view);
        toast.show();
    }
}
Custom Toast in Android Studio

First we create object of Toast by this line of code

 Toast toast = new Toast(getApplicationContext());

Then we set the duration to Long using this

toast.setDuration(Toast.LENGTH_LONG);

After this, we inflate our custom_toast to view using LayoutInflater

LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_toast, null);

Now, we simply add the view to Toast by using this method and show it to the user.

toast.setView(view);
        toast.show();

Adding Image in Toast

We know how to create a Toast and now we are going to know How to insert Image in Toast. Inserting Image in Toast is quite easy because we learn previously creating custom Toast.

What I need to do is simply add an Image View in the custom_toast and then attach it to the Toast.

First right click on drawable and click new and then vector assets. Choose any of the icons and add them. I use the ac unit icon.

The following code is for the adding image in custom_toast

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"

    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_ac_unit_black_24dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_marginLeft="5dp"
        android:text="GeeksToCode"
        android:textColor="@color/colorPrimary"
        android:textSize="30dp" />

</LinearLayout>

I set LinearLayout orientation to horizontal so Our toast looks beautiful. Now we only need to add this in our Toast like we add above

import android.content.Context;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast toast = new Toast(getApplicationContext());
        toast.setDuration(Toast.LENGTH_LONG);

        LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_toast, null);
        toast.setView(view);
        toast.show();
    }
}

After running this you will see screen like this

Image in Toast in Android Studio

Conclusion:

So, after reading whole of this content you know what is Toast, How to make custom Toast and also How to add image in Toast. So Run this code in your own android studio and let me know If it helps you.

You can refer this docs also.

Tagged : /

Leave a Reply

Your email address will not be published. Required fields are marked *