Add image icon inside Toast message in android

Display images through drawable folder into toast message.

Toast message box can be edited so developer can add images, icons, custom text, add views on it. By this tutorial you will able to add images inside toast at left, right any side with custom textview text included. So here is the complete step by step tutorial for Add image icon inside Toast message in android .

android-project-download-code-button

How to Add image icon inside Toast message in android.

Code for MainActivity.java file.

 package com.android_examples.com.toastimageadd;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 Button ToastshowBUtton;
 // CREATING LINEAR LAYOUT.
 final LinearLayout layout=new LinearLayout(this); 
 // CREATING TEXTVIEW THAT SHOWS TOAST MESSAGE STRING.
 final TextView ToastText = new TextView(this);
 //CREATING TOAST.
 final Toast toast = new Toast(getApplicationContext());
 //CREATING IMAGEVIEW TO HOLD TOAST INSIDE IMAGE.
 final ImageView ToastImageAdd = new ImageView(this);
 
 setContentView(R.layout.activity_main);
 
 ToastshowBUtton = (Button)findViewById(R.id.button1);
 
 ToastshowBUtton.setOnClickListener(new View.OnClickListener() {
 
 @Override
 public void onClick(View v) {
 
 //ADDING CUSTOM TEXT IN TEXTVIEW TO DISPLAY TEXT IN TOAST.
 ToastText.setText(" Toast message including image ");
 ToastText.setTypeface(ToastText.getTypeface(),Typeface.BOLD);
 ToastText.setTextSize(15);
 
 //SETTING UP TOAST TIME DURATION.
 toast.setDuration(Toast.LENGTH_LONG);
 
 //ADDING IMAGE THROUGH DRAWABLE FOLDER INSIDE IMAGEVIEW.
 ToastImageAdd.setImageResource(R.drawable.ic_launcher);
 
 // ADDING IMAGE INSIDE LAYOUT VIEW.
 layout.addView(ToastImageAdd);
 
 //ADDING TOAST MESSAGE TEXT INSIDE LAYOUT VIEW.
 layout.addView(ToastText);
 
 //ADDING COMPLETE LAYOUT INCLUDING TOAST TEXT & TOAST IMAGE INSIDE TOAST VIEW.
 toast.setView(layout);
 
 //SHOW TOAST MESSAGE
 toast.show();
 
 }
 });
 }
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.toastimageadd.MainActivity" >

 <Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="CLICK HERE TO SHOW IMAGE INSIDE TOAST MESSAGE" />

</RelativeLayout>

Screenshot:

Add image icon inside Toast message in android

Click Here To Download Add image icon inside Toast message in android project.

Leave a Reply

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