Different Ways to Add Image to Toast in Android

A Toast is a feedback message. It takes very little space for displaying while the overall activity is interactive and visible to the user. It disappears after a few seconds. It disappears automatically. If the user wants a permanently visible message, a Notification can be used. Another type of Toast is custom Toast, in which images can be used instead of a simple message. So in this article, we are going to discuss three different ways to Add Images to Toast on Android. Note that we are going to implement this project using the Java language.
Method 1
Step 1: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. We will create a simple TextView inside the activity_main.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical"    tools:context=".MainActivity">          <TextView        android:id="@+id/show"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="10dp"        android:text="Show image in Toast"        android:textSize="22sp"        android:textStyle="bold" />  </LinearLayout> | 
Step 2: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;import android.view.View;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;  import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      TextView show;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        show = findViewById(R.id.show);                  // on click on show text images toast will be shown        show.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                                  // Initialising Toast                Toast toast = new Toast(getApplicationContext());                ImageView view = new ImageView(getApplicationContext());                                  // set image resource to be shown                view.setImageResource(R.drawable.screenshot);                                  // setting view to toast                toast.setView(view);                                  // showing toast                toast.show();            }        });    }} | 
Output:
Method 2
Step 1: Working with the activity_main.xml file
The activity_main.xml file will be the same.
Step 2: Create a new toast_image_layout.xml file
Go to the app > res > layout > right-click > New > Layout Resource File and name the file as toast_image_layout. Below is the code for the toast_image_layout.xml file.
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     android:id="@+id/relativeLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@android:color/white">      <TextView        android:id="@+id/textView1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="Toast Notification Type"        android:textAppearance="?android:attr/textAppearanceLarge"        android:textColor="@android:color/black"></TextView>      <ImageView        android:id="@+id/imageView1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/textView1"        android:layout_margin="5dip"        android:src="@drawable/gfgimage"></ImageView>  </RelativeLayout> | 
Step 3: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.widget.Toast;  import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      TextView show;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        show = findViewById(R.id.show);        show.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                LayoutInflater inflater = getLayoutInflater();                                  // inflate layout file in Layout Inflater                View view = inflater.inflate(R.layout.toast_image_layout,                         (ViewGroup) findViewById(R.id.relativeLayout1));                Toast toast = new Toast(getApplicationContext());                                  // add view of toast to                // toast_image_layout file                toast.setView(view);                                  // show toast                toast.show();            }        });    }} | 
Output:
Method 3
Step 1: Working with the activity_main.xml file
The activity_main.xml file will be the same.
Step 2: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import android.widget.Toast;  import androidx.appcompat.app.AppCompatActivity;  public class MainActivity extends AppCompatActivity {      EditText msg;    TextView show;    ImageView image;      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                  show = findViewById(R.id.show);                  show.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                LinearLayout linearLayout = new LinearLayout(getApplicationContext());                                  // populate layout with your image and text                // or whatever you want to put in here                ImageView imageView = new ImageView(getApplicationContext());                                  // adding image to be shown                 imageView.setImageResource(R.drawable.geeks);                                  // adding image to linearlayout                linearLayout.addView(imageView);                Toast toast = new Toast(getApplicationContext());                                  // showing toast on bottom                toast.setGravity(Gravity.BOTTOM, 0, 0);                toast.setDuration(Toast.LENGTH_LONG);                                  // setting view of toast to linear layout                toast.setView(linearLayout);                toast.show();            }        });    }} | 
Output:
				
					


