Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide 4.7.1 placeholder not loading

I am trying to load a placeholder image using GlideLibrary and I am using SimpleTarget. The placeholder image is not loading but the image from the url is loading. I have searched a lot and used RequestOptions as well. Still, it doesn't seem to work.

Below is the code :

Glide.with(mContext).load("https://i.imgur.com/0k7ZFWr.png").apply(RequestOptions.placeholderOf(R.drawable.pubgguide_icon)).into(new SimpleTarget<Drawable>() {
        @Override
        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                holder.cardImageLayout.setBackground(resource);
                Drawable cardViewBackground = holder.cardImageLayout.getBackground();
                cardViewBackground.setColorFilter(0x5F000000, PorterDuff.Mode.SRC_ATOP);
            }
        }
    });

I am using Glide library 4.7.1. I have tried using setDefaultRequestOptions() as well - not working.

My cardview XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/category_cardview"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="@dimen/leftrightmargin"
        android:layout_marginRight="@dimen/leftrightmargin"
        android:layout_marginTop="10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="4dp"
                android:layout_height="match_parent"
                android:background="#FFBF41"></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/cardImageLayout"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="2">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginBottom="3dp"
                        android:layout_marginLeft="@dimen/leftrightmargin"
                        android:layout_marginRight="@dimen/leftrightmargin"
                        android:layout_marginTop="60dp"
                        android:orientation="vertical">

                        <TextView
                            android:id="@+id/category_name"
                            android:layout_width="match_parent"
                            android:layout_height="0dp"
                            android:layout_gravity="bottom"
                            android:layout_weight="1"
                            android:gravity="bottom"
                            android:paddingLeft="5dp"
                            android:shadowColor="#f3848383"
                            android:shadowDx="4"
                            android:shadowDy="4"
                            android:shadowRadius="2"
                            android:text="WEAPONS"
                            android:textColor="@color/colorWhite"
                            android:textSize="30sp" />

                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="0.3"
                    android:background="#000"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="1">

                        <TextView
                            android:id="@+id/category_desc"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:paddingLeft="11dp"
                            android:text="Know your weapons before you shoot"
                            android:textColor="@color/colorLightWhite"
                            android:textScaleX="1" />
                    </LinearLayout>

                    <com.romainpiel.shimmer.ShimmerTextView
                        android:id="@+id/arrow_text"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginRight="5dp"
                        android:layout_weight="0.2"
                        android:gravity="right"
                        android:text=">>>>>"
                        android:textColor="#000"
                        android:textScaleX="1.5"
                        android:textStyle="bold"
                        app:reflectionColor="#FFBF41" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>
</FrameLayout>
like image 359
Charan Shetty Avatar asked Oct 17 '22 14:10

Charan Shetty


2 Answers

You can like this with Glide (4.7.1)

Glide.with(getActivity()).load(avatar)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.defaultusericon))
                .into(mUserIcon);
like image 144
豆软亮 Avatar answered Oct 31 '22 14:10

豆软亮


placeHolderOf sets place Holder image only if you are loading it in an ImageView. If you want to set place holder to a ViewGroup, SimpleTarget class provides a method which you can override to set background when image starts loading.
In your case try loading like below :

Glide.with(mContext)
            .load("https://i.imgur.com/0k7ZFWr.png")
            .apply(RequestOptions.placeholderOf(R.drawable.pubgguide_icon))
            .into(new SimpleTarget<Drawable>() {

                @Override
                public void onLoadStarted(@Nullable Drawable resource) {
                    holder.cardImageLayout.setBackground(resource);
                }

                @Override
                public void onResourceReady(@NonNull Drawable resource, Transition<? super Drawable> transition) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        holder.cardImageLayout.setBackground(resource);
                        Drawable cardViewBackground = holder.cardImageLayout.getBackground();
                        cardViewBackground.setColorFilter(0x5F000000, PorterDuff.Mode.SRC_ATOP);
                    }
                }
            });
like image 27
Akshay Bhat 'AB' Avatar answered Oct 31 '22 14:10

Akshay Bhat 'AB'