Skip to content Skip to sidebar Skip to footer

How To Change Color Of Button When Being Click,and Revert Back To Default Color In Next Click?

I have a like button in my RecyclerView,what I want is when user hit the like button for the 1st time,the button background color will change to red color,and when the same user hi

Solution 1:

You should create a selector file. Create a file in drawable folder like color_change.xml

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@color/button_pressed"/><!-- pressed --><itemandroid:state_focused="true"android:drawable="@color/button_focused"/><!-- focused --><itemandroid:drawable="@color/button_default"/><!-- default --></selector>

and declare it in the button like this

 <Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/color_change"
    android:text="Click Me" />

Solution 2:

Hi try to this hope this can help you...

In XML

  <Button
    android:id="@+id/btnClick"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:text="click"/>

In Adapter Class

boolean click = true;


        holder.btnClick.setTag(position);
        holder.btnClick.setId(position);
        holder.btnClick.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {

                if (click) {
                    holder.btnClick.setBackgroundColor(Color.RED);
                    click = false;
                } else {
                    holder.btnClick.setBackgroundColor(Color.WHITE);
                    click = true;
                }
                notifyDataSetChanged();
            }
        });

Solution 3:

Instead of clicked or not condition, make and use condition from you update database for like and dislike operation. So, In click-listener get data previously user like or not then change background and update database as per new click.

Solution 4:

Try adding this line to your row.xml file in the main layout :

android:descendantFocusability="blocksDescendants"

Solution 5:

Have a look at this. Here, I changed the button text color on click. First time, all buttons appear white and after click it toggles between red and white as you expected. --

//LikeAdapter.java

publicclassLikeAdapterextendsRecyclerView.Adapter<LikeAdapter.LikeHolder> {

    publicLikeAdapter() {

    }

    @Overridepublic LikeAdapter.LikeHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Viewview= LayoutInflater.from(parent.getContext()).inflate(R.layout.like_item,parent,false);
        returnnewLikeHolder(view);
    }

    @OverridepublicvoidonBindViewHolder(final LikeAdapter.LikeHolder holder, int position) {

        holder.red_btn.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {

                if (holder.red_btn.getVisibility() == View.VISIBLE) {
                    holder.red_btn.setVisibility(View.GONE);
                    holder.white_btn.setVisibility(View.VISIBLE);
                } else {
                    holder.red_btn.setVisibility(View.VISIBLE);
                    holder.white_btn.setVisibility(View.GONE);
                }
            }
        });

        holder.white_btn.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View view) {
                if (holder.white_btn.getVisibility() == View.VISIBLE) {
                    holder.red_btn.setVisibility(View.VISIBLE);
                    holder.white_btn.setVisibility(View.GONE);
                } else {
                    holder.red_btn.setVisibility(View.GONE);
                    holder.white_btn.setVisibility(View.VISIBLE);
                }

            }
        });
    }

    @OverridepublicintgetItemCount() {
        return5;
    }

    publicclassLikeHolderextendsRecyclerView.ViewHolder {
        private Button red_btn, white_btn;
        publicLikeHolder(View itemView) {
            super(itemView);
            red_btn = (Button) itemView.findViewById(R.id.red_btn);
            white_btn = (Button) itemView.findViewById(R.id.white_btn);
            red_btn.setBackgroundColor(Color.RED);
            white_btn.setBackgroundColor(Color.WHITE);

        }
    }
}

//like_item.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="5dp"><Buttonandroid:id="@+id/red_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="Red"/><Buttonandroid:id="@+id/white_btn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="White"/></RelativeLayout>

Post a Comment for "How To Change Color Of Button When Being Click,and Revert Back To Default Color In Next Click?"