Muhammad Luqman
Muhammad Luqman

Reputation: 55

ANDROID Progress Dialog when Loading Image on Dialog/Popup

I have this working function that load image from url. The only thing is that it shows no progress while loading the image just the screen become dimmer and after some time only the picture is shown. This makes the user think like it is not working. So how do I add progress dialog while the image is loading or any way that shows the image is loading?

public void showImage() {
    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(this);
    Picasso.with(this).load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png").into(imageView);
    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();
}

Upvotes: 1

Views: 3235

Answers (2)

Janki Gadhiya
Janki Gadhiya

Reputation: 4510

public void showImage() {
    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    final ProgressDialog progressDialog = new ProgressDialog(this);
    ImageView imageView = new ImageView(this);
    Picasso.with(this)
            .load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png")
            .into(imageView, new com.squareup.picasso.Callback() {
                @Override
                public void onSuccess() {

                    // The image has loaded you can make the progress bar invisible
                    if (progressDialog.isShowing())
                        progressDialog.dismiss();
                }

                @Override
                public void onError() {
                    // Show some error message
                    if (progressDialog.isShowing())
                        progressDialog.dismiss();
                }
            });


    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();

    progressDialog.setMessage("please wait");
    progressDialog.setCancelable(false);
    progressDialog.show();
}

Upvotes: 2

Jayanth
Jayanth

Reputation: 6297

try adding PlaceHolder add this to layout folder

<RelativeLayout
android:id="@+id/hoteldetails_myContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<ProgressBar
    android:id="@+id/hoteldetails_progressBar"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:visibility="gone"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp" />

<ImageView
    android:id="@+id/hoteldetails_imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"/>
</RelativeLayout>

and this to loading code

Picasso.with(this)
        .load(hoteImageStr)
        .error(R.drawable.loading)
        .into(img,  new com.squareup.picasso.Callback() {
            @Override
            public void onSuccess() {
                if (progress != null) {
                    progress .setVisibility(View.GONE);
                }
            }

            @Override
            public void onError() {

            }
        });

Upvotes: 4

Related Questions