user9709337
user9709337

Reputation:

favorite button in android not working properly

Hii i'm making an app where i have small heart image onclick of that image i've set an onclick listener so when i click on that image it is replaced by another red coloured heart image.

so my problem is when i again click on red heart image it doesnt turn to its normal state which is blank heart vector image as i'm new in android i dont have much knowledge please if someone can guide.

my code for image onclick

  @Override
    public void onBindViewHolder(FeedViewHolder holder, int position) {
    int image_id = images[position];
    //holder.background_image_layout.setImageDrawable(null);
    //holder.background_image_layout.setImageResource(image_id);
    holder.background_image.setBackgroundResource(image_id);

    }

    @Override
    public int getItemCount() {
        return images.length;
    }
    public static class FeedViewHolder extends RecyclerView.ViewHolder {
        CustomTextViewMedium first_text,second_text,third_text,fourth_text,fifth_text,sixth_text,
                seventh_text;
        ImageView favourite_image;
        CardView primary_card;
        LinearLayout background_image;
        ImageView background_image_layout;
        CircleImageView profile_image;
        public FeedViewHolder(View itemView) {
            super(itemView);
            background_image = (LinearLayout)itemView.findViewById(R.id.background_image);
            primary_card = (CardView)itemView.findViewById(R.id.primary_card);
            first_text = (CustomTextViewMedium)itemView.findViewById(R.id.first_text);
            second_text = (CustomTextViewMedium)itemView.findViewById(R.id.second_text);
            third_text = (CustomTextViewMedium)itemView.findViewById(R.id.third_text);
            fourth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fourth_text);
            fifth_text = (CustomTextViewMedium)itemView.findViewById(R.id.fifth_text);
            sixth_text = (CustomTextViewMedium)itemView.findViewById(R.id.sixth_text);
            seventh_text = (CustomTextViewMedium)itemView.findViewById(R.id.seventh_text);
            favourite_image = (ImageView)itemView.findViewById(R.id.favourite_image);
            background_image_layout = (ImageView) itemView.findViewById(R.id.background_image_layout);
            profile_image = (CircleImageView)itemView.findViewById(R.id.profile_image);
            favourite_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(favourite_image.isPressed()){
                        favourite_image.setImageResource(R.drawable.ic_heart);
                        favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    }
                    else {
                        favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                        favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    }
                }
            });


        }
    }

Upvotes: 1

Views: 133

Answers (2)

Abhinav Gupta
Abhinav Gupta

Reputation: 2265

Use if condition like this :-

first globally define like this :-

boolean imageChange = true;

then do this in setOnClickListener like this :-

  favourite_image.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(imageChange ){
                            favourite_image.setImageResource(R.drawable.ic_heart);
                            favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                            imageChange = false;                        
                           }else {
                                favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                                favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                                imageChange = true;
                            }
                        }
                    });

Upvotes: 1

Gotiasits
Gotiasits

Reputation: 1175

Problem is that favourite_image.isPressed() is not persistent state of the View. It will be true only while user holds its finger on the view.

To overcome this you have two similar options:

Option #1

Instead of checking isPressed you can check some other state, for example isSelected. Since this is ImageView you probably need to set the state manually on click.

public void onClick(View v) {
                    if(favourite_image.isSelected()){
                        favourite_image.setImageResource(R.drawable.ic_heart);
                        favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        favourite_image.setSelected(false)
                    }
                    else {
                        favourite_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
                        favourite_image.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        favourite_image.setSelected(true)
                    }
                }

Options #2

Use some simple boolean to keep track of the selected state, and set/chec its values similarity to above code.

Upvotes: 0

Related Questions