Sree
Sree

Reputation: 2787

ColorDrawable placeholder not loading in glide

Am I doing something wrong here? I want to load that gray as a placeholder however, it doesn't load. It seems to only happen if there was already another image set or loaded.

Glide.with(getContext())
    .load(url)
    .placeholder(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.placeholder_gray)))
    .error(new ColorDrawable(ContextCompat.getColor(getContext(), R.color.placeholder_gray)))
    .dontAnimate()
    .into(new SimpleTarget<GlideDrawable>() {
      @Override
      public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
        image.setImageDrawable(resource);
      }

      @Override
      public void onLoadFailed(Exception e, Drawable errorDrawable) {
        resetImageView();
      }
    });

Upvotes: 3

Views: 6506

Answers (1)

remya thekkuvettil
remya thekkuvettil

Reputation: 798

Try the below code:

Glide version:

implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="100dp"
        android:layout_height="100dp"></ImageView>
</RelativeLayout>

Java code:

ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Glide.with(this)
                .load("http://matplotlib.org/_images/color_demo.png")
                .placeholder(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.placeholder_gray)))
                .error(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.placeholder_gray)))
                .dontAnimate()
                .into(imageView);

Color (in colors.xml)

<color name="placeholder_gray">#A9A9A9</color>

Try using a valid URL and an invalid URL.For a valid URL gray color appears before image loading and for an invalid URL gray placeholder appears.

Upvotes: 9

Related Questions