0

How to add custom image added to custom Toast...

Rohit
  • 3,401
  • 4
  • 33
  • 60
  • So? What is your problem? – Barney Mar 01 '13 at 10:06
  • pls specify the problem you are facing with this code !! – Rachita Nanda Mar 01 '13 at 10:07
  • i want to add my image dynamically , thats it – Rohit Mar 01 '13 at 10:07
  • i have a solution , i made new custom toast , then use that, but i want to do this by only one custom toast , this is my problem – Rohit Mar 01 '13 at 10:08
  • 2
    What do you mean exactly by "I want to do this by only one custom toast"? So do you have a custom Toast working or not? You might want to follow this link: http://stackoverflow.com/questions/11288475/custom-toast-in-android-a-simple-example – Barney Mar 01 '13 at 10:12

3 Answers3

1

SImply add an ImageView into your toast_layout and then:

ImageView iv=  (ImageView) layout.findViewById(R.id.image);
iv.setBackgroundResource(R.drawable.img1);//Or wathever you want!

This will work.

Waza_Be
  • 39,407
  • 49
  • 186
  • 260
0
03-01 15:55:41.040: E/AndroidRuntime(6512): java.lang.NullPointerException
03-01 15:55:41.040: E/AndroidRuntime(6512):    at com.anthem.mathsmagic.maxone$1.onClick(maxone.java:98)

That's a fairly common NPE on your onClick method. What are you doing on line 98?

Charlie-Blake
  • 10,832
  • 13
  • 55
  • 90
-2

This is how I did it:

LayoutInflater inflater = LayoutInflater.from(getActivity());
    View layout = inflater.inflate(R.layout.toast, null); 
     ImageView image = (ImageView) layout.findViewById(R.id.image); 
     image.setImageBitmap(bm);

    TextView text = (TextView) layout.findViewById(R.id.toast_text);
    String toastText = "Hi"; 
    text.setText(toastText);

    Toast toast = new Toast(getActivity().getApplicationContext());
    toast.setGravity(Gravity.CENTER, 0, 0);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/covu_sdk_background_toast"
android:orientation="horizontal"
android:padding="10dp" >

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/toast_text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:gravity="center_vertical"
    android:textColor="#FFF" />

</LinearLayout>
M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103