0

I'm creating a wallpaper app and I want to apply getLightMutedColor()of Palette class to the textbox below each wallpaper. But It's creating problems on restarting the app .

So this is how it should be

So this is how it should be

On restarting the app the color changes to white

On restarting the app the color changes to white

Here's my adapter class

public class GridViewAdapter extends ArrayAdapter<GridItem> {

private Context mContext;
private int layoutResourceId;
private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();

public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridItem> mGridData) {
    super(mContext, layoutResourceId, mGridData);
    this.layoutResourceId = layoutResourceId;
    this.mContext = mContext;
    this.mGridData = mGridData;
}


/**
 * Updates grid data and refresh grid items.
 * @param mGridData
 */
public void setGridData(ArrayList<GridItem> mGridData) {
    this.mGridData = mGridData;
    notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final ViewHolder holder;

    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new ViewHolder();
        holder.titleTextView = (TextView) row.findViewById(R.id.grid_item_title);
        holder.imageView = (ImageView) row.findViewById(R.id.grid_item_image);
        row.setTag(holder);
    } else {
        holder = (ViewHolder) row.getTag();
    }

    GridItem item = mGridData.get(position);
    holder.titleTextView.setText(Html.fromHtml(item.getTitle()));
    Picasso.with(mContext)
            .load(item.getImage())
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    Palette p = Palette.from(bitmap).generate();
                    int defaultColor= 0x000000;
                    int vbcol = p.getLightMutedColor(defaultColor);
                    holder.titleTextView.setBackgroundColor(vbcol);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
    Picasso.with(mContext).load(item.getImage()).into(holder.imageView);
    return row;
}

static class ViewHolder {
    TextView titleTextView;
    ImageView imageView;
}
 }
Arjun saini
  • 4,223
  • 3
  • 23
  • 51
r2d2arto
  • 1
  • 1

2 Answers2

0

This is the Asynchronous wayt to do it, You can have a look at this: https://developer.android.com/reference/android/support/v7/graphics/Palette.html

Try this code, maybe it will work

Palette.from(bitmap).generate(new PaletteAsyncListener() {
     public void onGenerated(Palette p) {
         int defaultColor= 0x000000;
         int vbcol = p.getLightMutedColor(defaultColor);
         holder.titleTextView.setBackgroundColor(vbcol);
     }
 });
0

Was getting nullpointerexception with everything so it was certain that the image wasn't loaded so i made it work with this callback

        @Override
        public void onSuccess() {
            BitmapDrawable bd = (BitmapDrawable)holder.imageView.getDrawable();
            Bitmap bm =bd.getBitmap();
            Palette p = Palette.from(bm).generate();
            int defcol=0x000000;
            holder.titleTextView.setBackgroundColor(p.getLightVibrantColor(defcol));
        }

        @Override
        public void onError() {

        }
    }); 
r2d2arto
  • 1
  • 1