2

I have a LinearLayout in my xml. When I click on the LinearLayout, I need color appearances for a specific time of onclick event. After that, the color will disappear.

holder.ll2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) {
                
                    v.setBackgroundColor(0xFF00FF00);
                    v.invalidate();
                }
            }
        });
peterh
  • 11,875
  • 18
  • 85
  • 108
Manikanta Reddy
  • 631
  • 9
  • 15

4 Answers4

2

Try this Make drawable folder in resource file create this XML You can change it according to your requirements

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_focused="true"
         android:state_pressed="false"
         android:drawable="@drawable/big_box_hover"/>
<item android:state_focused="true"
         android:state_pressed="true"
         android:drawable="@drawable/big_box_hover" /> 
<item android:state_focused="false" 
         android:state_pressed="true" 
         android:drawable="@drawable/big_box_hover" /> 
<item android:drawable="@drawable/big_box" /> 
</selector>

Then in your layout you can do this

<Button
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="6dp"
            android:layout_marginTop="6dp"
            android:descendantFocusability="blocksDescendants"
            android:background="@drawable/dashboardbuttonclick"
            android:gravity="center_vertical" >
Hanish Sharma
  • 869
  • 8
  • 23
0

You can use Handler for that

holder.ll2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(final View v) { 
                   handler=new Handler();
                   handler.postDelayed(myRunnable, TIME_MILI_TO_SHOW_COLOR_INT);
                  btn.setBackgroundColor(0xFF00FF00);
                }
            }
        });

Runnable for That

 myRunnable=new Runnable() {

            @Override
            public void run() {

                btn.setBackgroundColor(YOUR_DEFAULT_COLOR);
            }
        }
N J
  • 27,217
  • 13
  • 76
  • 96
0

this link can help you out

How to create custom button in Android using XML Styles

Basically you want clicked stated for a button..when clicked it should show a different color and when released show original color.It can be done by defining button stated as xml and then set that xml to button background.

Community
  • 1
  • 1
Meenal
  • 2,879
  • 5
  • 19
  • 43
0

Try this code

Button btn = (Button)this.findViewById(R.id.btn1);
//Let's change background's color from blue to red.
ColorDrawable[] color = {new ColorDrawable(Color.BLUE), new ColorDrawable(Color.RED)};
TransitionDrawable trans = new TransitionDrawable(color);
//This will work also on old devices. The latest API says you have to use setBackground instead.
btn.setBackgroundDrawable(trans);
trans.startTransition(5000);
sasikumar
  • 12,540
  • 3
  • 28
  • 48