Reputation: 641
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();
}
}
});
Upvotes: 2
Views: 284
Reputation: 869
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" >
Upvotes: 2
Reputation: 13358
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);
Upvotes: 0
Reputation: 2877
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.
Upvotes: 0
Reputation: 27535
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);
}
}
Upvotes: 0