0

In my app I have a button which when cancelled should display a dialog which contains only an image and nothing else. The image is retrieved from a URL. But when button is clicked nothing is displayed.

My code

public void onClick(View v){

    final Dialog dialog = new Dialog(SingleItemView.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    ImageView imgvw = new ImageView(SingleItemView.this);
    String imgurl =descImgOne;
    Uri imguri = Uri.parse(imgurl);
    imgvw.setImageURI(imguri);
    dialog.getWindow().getAttributes().windowAnimations = R.style.SmileWindow;
    dialog.addContentView(imgvw, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
               ViewGroup.LayoutParams.MATCH_PARENT));

     dialog.show();
}

Where descImgOne is assigned a URL link

dstudeba
  • 8,878
  • 3
  • 32
  • 41
user5524159
  • 553
  • 5
  • 16

4 Answers4

1
    public void onClick(View v){

    final Dialog dialog = new Dialog(SingleItemView.this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.customdialog);

    ImageView imageView= (ImageView).dialog.findViewById(R.id.dialogimageview);
    URL url = new URL("http://image10.bizrate-images.com/resize?sq=60&uid=2216744464");
    Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    imageView.setImageBitmap(bmp);

    dialog.show();
}

customdialog.xml

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

        <ImageView
            android:id="@+id/dialogimageview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />


</LinearLayout>
praveen s
  • 209
  • 4
  • 11
0

Use need use setContentView : Dialog custom layout

dialog.setContentView(R.layout.dialog_layout);
Quang Doan
  • 632
  • 4
  • 12
  • Yeah I can do that but instead of defining a new layout file just wanted a image in the dailog. Thanks for your help – user5524159 Dec 23 '15 at 07:36
  • Oh! Try this. http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html or checkout this http://stackoverflow.com/questions/4351204/android-progressdialog-with-setcontentview . – Quang Doan Dec 23 '15 at 07:43
0
imgvw.setBackgroundResource(R.drawable.app_launcher);

try this if it renders then there is some problem in the uri

nakul
  • 85
  • 5
0

One of the working code (using web view inside the dialog)

AlertDialog.Builder alert = new AlertDialog.Builder(this);

WebView wv = new WebView(this);
wv.loadUrl("http://www.jjyproductions.com/wp-content/uploads/2014/02/IMG_3454-001.jpg");
wv.setWebViewClient(new WebViewClient() {

   @Override
   public boolean shouldOverrideUrlLoading(WebView view, String url) {

     view.loadUrl(url);
     return true;

   }    
});

   wv.setLayoutParams(new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));

   alert.setView(wv);

   alert.setNegativeButton("Close", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                dialog.dismiss();
            }
  });
  alert.show();
Zumry Mohamed
  • 9,318
  • 5
  • 46
  • 51