2

Looking around the website other tutorials found out a nice option applying material design to android app, which is picking a cardView action area color depending on the picture viewed in the card.

In this toturial, they are importing the images into the ciew from drawable, and i am retrieving it from a website, by creating an arrayList of the images addresses, and then using picasso to feed it into the cardView almost as similar as the tutorial, the difference is that i am not using a different class to unwrap the information and converting it into a an ArrayList.

So when trying to apply the same method demonstrated in the tutorial to pick a color for placeNameHolder ( action area under the picture in the cardView) i got confused on how to do so in compatible way with my resources ?

I would appreciate any instruction on how to achieve this and understanding the method ?

in the tutorial Activity_details

   @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_places, parent, false);
return new ViewHolder(view);
 }

  @Override
  public void onBindViewHolder(final ViewHolder holder, final int position) {
final Place place = new PlaceData().placeList().get(position);

holder.placeName.setText(place.name);
Picasso.with(mContext).load(place.getImageResourceId(mContext)).into(holder.placeImage);

Bitmap photo = BitmapFactory.decodeResource(mContext.getResources(), place.getImageResourceId(mContext));

Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
  public void onGenerated(Palette palette) {
    int mutedLight = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
    holder.placeNameHolder.setBackgroundColor(mutedLight);
  }
});
 }

 @Override
  public int getItemCount() {
return new PlaceData().placeList().size();
}

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public LinearLayout placeHolder;
public LinearLayout placeNameHolder;
public TextView placeName;
public ImageView placeImage;

public ViewHolder(View itemView) {
  super(itemView);
  placeHolder = (LinearLayout) itemView.findViewById(R.id.mainHolder);
  placeName = (TextView) itemView.findViewById(R.id.placeName);
  placeNameHolder = (LinearLayout) itemView.findViewById(R.id.placeNameHolder);
  placeImage = (ImageView) itemView.findViewById(R.id.placeImage);
  placeHolder.setOnClickListener(this);
} 

In my app;

 public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView name;
    public ImageView picture;
    public LinearLayout placeNameHolder;

    public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
        super(inflater.inflate(R.layout.item_card, parent, false));

        picture = (ImageView) itemView.findViewById(R.id.placeImage);
        name = (TextView) itemView.findViewById(R.id.card_title);
        placeNameHolder = (LinearLayout) itemView.findViewById(R.id.placeNameHolder);
    }
}
/**
 * Adapter to display recycler view.
 */
public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {

    private Context mContext;

    public ContentAdapter(Context context) {

        this.mContext = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        Picasso.with(mContext).load(mImages[position]).into(holder.picture);

        holder.name.setText(author[position]);

        Bitmap photo = BitmapFactory.decodeResource(mImages[position]);

        Palette.generateAsync(photo, new Palette.PaletteAsyncListener() {
            public void onGenerated(Palette palette) {
                int bgColor = palette.getMutedColor(mContext.getResources().getColor(android.R.color.black));
                holder.placeNameHolder.setBackgroundColor(bgColor);
            }
        });
    }

the difference is that my resource is ;

          Firebase ref = new Firebase("https://wi4x4.firebaseio.com/data/images");
    ref.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            if(snapshot !=null) {
                for (DataSnapshot child : snapshot.getChildren()) {
                    Log.i("MyTag", child.getValue().toString());
                    imagesfeedsList.add(child.child("address").getValue(String.class));
                    authorfeedsList.add(child.child("author").getValue(String.class));
                }
                Log.i("MyTag_imagesDirFinal", imagesfeedsList.toString());

                mImages = imagesfeedsList.toArray(new String[imagesfeedsList.size()]);
                author = authorfeedsList.toArray(new String[authorfeedsList.size()]);
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
madnomad10011
  • 341
  • 2
  • 6
  • 19

1 Answers1

0

All right, with little bit of diving into stackoverflow rich content found this post similar to what i am trying to achieve. and the code i was able to generate;

     try{
            String url1 = mImages[position];
            URL ulrn = new URL(url1);
            HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
            InputStream is = con.getInputStream();
            Bitmap bmp = BitmapFactory.decodeStream(is);
            if (null != bmp)

                Palette.generateAsync(bmp, new Palette.PaletteAsyncListener() {
                    public void onGenerated(Palette palette) {
                        int bgColor = palette.getVibrantColor(mContext.getResources().getColor(android.R.color.black));
                        holder.placeNameHolder.setBackgroundColor(bgColor);
                    }
                });
            else
                Log.e("MyTag_BMP","The Bitmap is NULL");

        }catch (Exception e){

        }

although in my code, .getColor is deprecated ? and i am not sure why and the outcome, almost what i expected. would appreciate any comment on the code to correct what might be wrong ?

Community
  • 1
  • 1
madnomad10011
  • 341
  • 2
  • 6
  • 19