Android View is still getting OnClick() even by set its visibility to GONE

2k views Asked by At

I know there are few questions regard this issue but all of them are using animation. I have no animation at all in my activity. I have a TextView that by default is visible and is set to Gone based on a Feature-Flag that I get in my Splash screen.

This is my xml file:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/profile_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/profile_bg"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:paddingTop="?android:actionBarSize">

    <TextView
            android:id="@+id/payments_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="6dp"
            android:background="@drawable/profile_payment_bg"
            android:clickable="true"
            android:drawableLeft="@drawable/ic_profile_payment"
            android:drawablePadding="8dp"
            android:gravity="center_vertical"
            android:onClick="onClick"
            android:padding="8dp"
            android:text="Payment"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="@android:color/white" />

...

    </LinearLayout>

</FrameLayout>

Although I'm setting Clickable and OnClickListener functionality to false and null respectively, OnClick() functionality is called even when my Payment button is not visible :(

@Override
    protected void onCreate(final Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_profile);

        this.mPayments = (TextView) findViewById(R.id.payments_btn);

        // Check if payment functionality available for the Passenger
        final PassengerFeatureResponse cachedFeature = FeatureResponse.fromJsonString(PreferenceUtils.getFeatureResponse(this));
        if (cachedFeature == null || !cachedFeature.isMade())
        {
            this.mPayments.setVisibility(View.GONE);
            this.mPayments.setClickable(false);
            this.mPayments.setOnClickListener(null);
        }
    }

I even tried to set visibility to Gone from xml file and set it to visible from code however the functionality was same :(

It's probably because I have defined onClick functionality from xml file and my problem would be fixed if I set click listener from code, however, I'm looking for fix the issue in this way.

Any suggestion would be appreciated. Thanks.

3

There are 3 answers

1
N J On

Try this on FrameLayout like this

  this.frameMain= (FrameLayout) findViewById(R.id.profile_layout);
       if (cachedFeature == null || !cachedFeature.isMade())
        {
            this.mPayments.setVisibility(View.GONE);
            this.mPayments.setClickable(false);
            this.mPayments.setOnClickListener(null);
            this.frameMain.invalidate();
        }
3
Sharp Edge On

After these line:

        this.mPayments.setVisibility(View.GONE);
        this.mPayments.setClickable(false);

Add these lines:

        this.mPayments.setFocusable(false);
        this.mPayments.setFocusableInTouchMode(false);

** EDIT **

Ignore the focusable snippet and try to disable the view like this:

         this.mPayments.setEnabled(false);

You can check if a view can receive click events to make sure everything is rite:

         this.mPayments.isClickable(); // Indicates whether this view reacts to click events or not.

If the above shows false, then it shouldn't react to click events.

0
JJ86 On

When you use onClick pattern, you must define a View as parameter on the method implemented in your class.

Maybe you can use a switch with the ID of the view, then check if payments_btn is still visible and fire your action only if is visible.