Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement simple like button in listview of each item

enter image description here

I have certain entries in my list view item. There I have a simple "like button" (not facebook like button). You can see the above mentioned SCREENSHOT; for the reference. The moment I click on like button; i want the like button color to be changed and the like button color should remain same(changed on like) when I'll login again.

Also, all the entries must get filled in Database with cust_id, bus_id, Offer_id using json; that I know very well.

When I again click on the same button(like button), whose color has been changed. It must be changed back to the default color and data must get removed from database.

How can I do this...? 1. How to get value of click button. 2. How to bring back the changed color to default; once the button has been re-clicked.

Plz suggest me...

this is button code

holder.b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (clicked) {
                    holder.b1.setBackgroundResource(R.drawable.like_icon_hover);
                } else {
                    holder.b1.setBackgroundResource(R.drawable.like_icon);
                }
                clicked = true;
            }
        });
like image 950
Amardeepvijay Avatar asked Apr 09 '14 11:04

Amardeepvijay


1 Answers

You need to add a listener to the button and using ValueAnimator you can change the button color and reverse it back when you click again.

Here is a simple and best approach to achieve your scenario. Add the onClick listener for the button in your list item like this.. I have explained each line ..

    // set a default background color to the button
    placeHolder.likeButton.setBackgroundColor(Color.RED);
    placeHolder.likeButton.setOnClickListener(new View.OnClickListener() {
        ValueAnimator buttonColorAnim = null; // to hold the button animator

        @Override
        public void onClick(View v) {
            // first time this will be null
            if(buttonColorAnim != null){
                // reverse the color
                buttonColorAnim.reverse();
                // reset for next time click
                buttonColorAnim = null;
                // add your code here to remove from database
            }
            else {
                final Button button = (Button) v;
                // create a color value animator
                buttonColorAnim = ValueAnimator.ofObject(new ArgbEvaluator(), Color.RED, Color.BLUE);
                // add a update listener for the animator.
                buttonColorAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animator) {
                        // set the background color
                        button.setBackgroundColor((Integer) animator.getAnimatedValue());
                    }
                });
                // you can also set a delay before start
                //buttonColorAnim.setStartDelay(2000); // 2 seconds
                // start the animator..
                buttonColorAnim.start();
                // add your code here to add to database
            }
        }
    });

This will change the button color on your first click and then revert the color back on the next click. You can also set a delay to change the color.

Note: You have to set the default button color based on your logic.

like image 178
Libin Avatar answered Nov 15 '22 06:11

Libin