0

This is my Popup class,

public class Popup {

    public void showDialog(Activity activity, String url){
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.popup_playerstats);

        ImageView imageFirst= (ImageView) dialog.findViewById(R.id.img_First);

        ImageView dialogButton = (ImageView) dialog.findViewById(R.id.close);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }
}

And my onClick method of Fragment is,

public void onClick(final View v) {
    Popup alert = new Popup();
    switch (v.getId()) {
        case R.id.button_1:
            alert.showDialog(getActivity(),url);
            break;
        case R.id.button_2:
            alert.showDialog(getActivity(),url1);
            break;
        default:
            // some code here
            break;
    }
 }

I need to use the String variable url or url1 and setimage on ImageView imageFirst.

Help how could I do this?

4 Answers4

1

First add the Glide dependency to your build.gradle Section.

dependencies {
    // glide
    compile 'com.github.bumptech.glide:glide:3.7.0'
}

Then

ImageView imageFirst= (ImageView) dialog.findViewById(R.id.img_First);
Glide.with(activity).load(url)
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageFirst);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

add following in dependencies

compile 'com.github.bumptech.glide:glide:3.7.0'

and use to set in ImageView

Glide.with(mContext)
                        .load(url)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .dontAnimate()
                        .placeholder(ContextCompat.getDrawable(mContext, R.mipmap.ocassion_placeholder))
                        .into(imageFirst);
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50
0

add dependency in build.gradle file

compile 'com.github.bumptech.glide:glide:3.7.0'

Extend your popup class with Dialog

public class Popup extends Dialog{

private Context mContext;
private  ImageView imageFirst;
private String url;

public Popup(Context context, String url) {
    super(context);
    mContext = context;
    this.url = url;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.popup_playerstats);

    imageFirst= (ImageView) findViewById(R.id.img_First);
    Glide.with(mContext)
            .load(url)
            .error(R.drawable.error)
            .into(imageFirst);


    ImageView dialogButton = (ImageView) findViewById(R.id.close);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
}
}

In your activity, onClickEvent

public void onClick(final View v) {
    Popup popup;
    switch (v.getId()) {
        case R.id.button_1:
            popup = new Popup(mContext, url);
            break;
        case R.id.button_2:
            popup = new Popup(mContext, url1);
            break;
        default:
            // some code here
            break;
        popup.setCancelable(false);
        popup.show();
    }
}
Mohammad nabil
  • 1,010
  • 2
  • 12
  • 23
0

Add another variable to pass into showDialog method to pass the image's location id from the onClick method. Then in the showDialog method, use this id to set it as an image resource.

onClick:

public void onClick(final View v) {
    Popup alert = new Popup();

    switch (v.getId()) {
        case R.id.button_1:
            alert.showDialog(getActivity(),url,R.drawable.img1);
            break;
        case R.id.button_2:
            alert.showDialog(getActivity(),url1,R.drawable.img2);
            break;
        default:
            // some code here
            break;
    }
 }

showDialog:

public class Popup {

    public void showDialog(Activity activity, String url, int id){
        final Dialog dialog = new Dialog(activity);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.popup_playerstats);

        ImageView imageFirst= (ImageView) dialog.findViewById(R.id.img_First);
        imageFirst.setImageResource(id);

        ImageView dialogButton = (ImageView) dialog.findViewById(R.id.close);
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();

    }
}