rozina
rozina

Reputation: 4232

Disable button click animation withouth setEnable(false)

I have a LienarLayout with a Button in it. I have a OnClickListener on both the Button and the LinearLayout. At some point I would like to disable the Button and pass the onClick event to the parent LinearLayout. I found out that you achieve this by setting Button.setClickable(flase). Which works and the LinearLayout gets the click, however the Button's click animation is still played. Even worse, if I click on the LinearLayout where the Button is not drawn, the Buttons click animation still plays!

If anyone knows how I can achieve what I want, I would greatly appreciate it.

P.S.: The reason I don't want to use Button.setEnabled(false) is because I don't want the button to look disabled. I would also like to be able to enable / disable button's clickable state on demand. So basically I would like the button to be active sometimes and then other times for the click to pass through to the LinearLayout.

The code - xml:

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground">

    <Button
        android:id="@+id/button"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="button"/>

</LinearLayout>

The code - java:

Button btn = (Button) view.findViewById(R.id.button);
btn.setClickable(false);

Before click: Before click During click on LinearLayout: During click on LinearLayout

Upvotes: 1

Views: 8832

Answers (1)

Wilson Christian
Wilson Christian

Reputation: 650

You can simply give background color so as the animation won't happen.

android:background="@android:color/white"

When you want clickable animation back, use this

TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, outValue, true);
yourButton.setBackgroundResource(outValue.resourceId);

This will solve this issue.

Upvotes: 4

Related Questions