0

How can i load an external image (from url) into a toast? Currently i have this code working that displays a simple text:

cordova.getActivity().runOnUiThread(new Runnable() {
    public void run() {
      android.widget.Toast toast = android.widget.Toast.makeText(webView.getContext(), 'Hello, i'm a toast!', 0);
      toast.setDuration(android.widget.Toast.LENGTH_LONG);
      toast.show();
      callbackContext.success();
    }
});

what i want is to put a little icon to the left, that it's downloaded from a url, so i think it should be loaded from async task in background..

Dawson Loudon
  • 6,029
  • 2
  • 27
  • 31
FidoBoy
  • 392
  • 6
  • 18

2 Answers2

1

custom_toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/toast_layout"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#000"
 android:orientation="horizontal"
 android:padding="5dp" >
 <ImageView
 android:id="@+id/image"
 android:src="@drawable/toastimg"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:layout_marginRight="5dp" />
 <TextView
android:id="@+id/text"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:textColor="#FFF"/>
 </LinearLayout>

Activity.java

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.toast_layout));
TextView text = (TextView) layout.findViewById(R.id.text);
ImageView imageView = (ImageView) layout.findViewById(R.id.image);
Picasso.with(context)
.load(url)
.into(imageView);
text.setText("Custom Toast with Twitter Icon"); //Message shown in Custom Toast
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);
toast.show();
TooCool
  • 10,598
  • 15
  • 60
  • 85
  • Thanks abdellah, but i don't think that i can create a custom_toast.xml in a cordova plugin, also the issue is how to download the image, it's not a local resource – FidoBoy Dec 21 '14 at 00:05
  • Ok, i see that you have added Picasso library to download the image, but it may be a bit complicated for this simple task; there is not a simpler way to load the image without using external libraries like Picasso? – FidoBoy Dec 21 '14 at 00:07
  • Also, if you have experience using cordova, where should i put the custom_toast.xml file inside the plugin folder? – FidoBoy Dec 21 '14 at 00:08
  • @FidoBoy check out http://stackoverflow.com/questions/21012964/cordova-3-android-plugin-how-to-call-a-function-in-main-activity – TooCool Dec 21 '14 at 00:17
  • Brilliant! But when i replaced the R with this code: ```View layout = inflater.inflate(resources.getIdentifier("custom_toast","layout",packageName),(ViewGroup)findViewById(resources.getIdentifier("toast_layout","id",packageName)));``` it gives me error "error: cannot find symbol" on (ViewGroup) findViewById :( why? – FidoBoy Dec 22 '14 at 02:57
  • I suppose that it can't locate the resource, but the question is, where should i put it? remember that it's a cordova plugin,so may be i should add something to my plugin.xml to copy the layout to certain folder ?¿ – FidoBoy Dec 22 '14 at 03:00
  • ok, solved, the right code is now: ```View layout = inflater.inflate(resources.getIdentifier("custom_toast","layout",packageName),null);‌​``` and it works – FidoBoy Dec 23 '14 at 12:59
0

Just create a custom layout with ImageView and add it to your custom Toast:

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(yourCustomView); // <- you custom View
toast.show();

Look at this: Custom toast in android : a simple example

EDIT:

ok try this (WARNING ugly pseudocode!!):

 public void yourMethod(){
     new Thread(new Runnable(){
         public void run(){

             final Bitmap bmap = getBitmapFromURL("your URL");     

             runOnUiThread(new Runnable() {
                 public void run() {
                     ImageView img = new ImageView();
                     img.setImageBitmap(bmap );

                     Toast toast = new Toast(getApplicationContext());
                     toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                     toast.setDuration(Toast.LENGTH_SHORT);
                     toast.setView(img); // <- you custom View
                     toast.show();
                 }
             });
         }
     }).start();
 }


 public Bitmap getBitmapFromURL(String link) {

    try {
        URL url = new URL(link);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap bmap = BitmapFactory.decodeStream(input);

        return bmap ;

    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
Community
  • 1
  • 1
alex
  • 8,904
  • 6
  • 49
  • 75
  • Thanks dit, but i'm a noob with android native code, and don't know how to create a custom view with image downloader, can you elaborate a bit more? – FidoBoy Dec 21 '14 at 00:03
  • yes, but this doesn't download the image into the toast – FidoBoy Dec 21 '14 at 00:10
  • ok i updated my answer. i could not try it, so it is just pseudocode – alex Dec 21 '14 at 00:24
  • Hmm... i like it cause it doesn't use external libraries like Abdellah's answer. I need to try it, but looks good. Just a question, where you put `toast.setView(img); // <- you custom View` what does it mean? also, why it is 'ugly pseudocode'? – FidoBoy Dec 21 '14 at 00:27
  • Your code doesn't set the toast text... where should i put it? – FidoBoy Dec 21 '14 at 00:40
  • and what about the image dimensions? :D – FidoBoy Dec 21 '14 at 00:42