-2

I'm trying to build an app that displays images from a Google Drive folder. I am using Glide to retrieve and display those images.

This is what i've tried:

public class HomeFragment extends Fragment {

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        LinearLayout linearLayout = root.findViewById(R.id.image_layout);
        linearLayout.addView();
        String url = "";
        Glide.with(new HomeFragment()).load(url).placeholder(R.drawable.ic_dashboard_black_24dp)
                .into();
        return root;
    }
}

2 Answers2

1

You can do it with the help of LayoutParams as below.

        ImageView iv = new ImageView(this); //dynamically added ImageView
        LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(300, 300);
        imgView.setLayoutParams(layoutParam);
        Glide.with(this)
             .load(url)
             .override(300,300)
             .into(iv);
        linearLayout.addView(iv); //adding the ImageView to the Layout.
Jacks
  • 803
  • 1
  • 6
  • 16
  • Using "this" as the context for new ImageView() gives me an error. Whst should I replace it with? Or is there any documentation that describes the context parameter? –  Nov 27 '19 at 04:08
  • 1
    You can get the context for a Fragment - using getActivity() for more details. https://stackoverflow.com/questions/8215308/using-context-in-a-fragment – Jacks Nov 27 '19 at 04:38
0

You can add listener to Glide load command to get Drawable resource and do what ever you want with that Drawable:

Glide.with(mImageView.getContext())
            .load(url)
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    //Do what ever you want with the resource
                }
            })
            .into(mImageView);
Duy Khanh Nguyen
  • 454
  • 3
  • 11