5

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.

Hesam
  • 52,260
  • 74
  • 224
  • 365
  • 1
    If you set the visibility as GONE, the view is completely removed from the layout. It doesn't even take any space. I'd first confirm if that actually happens ? (I don't think its possible to click something that doesnt even show up on the screen) – Shivam Verma Jun 15 '15 at 09:29
  • `mPayments.setVisibility(View.GONE);` is more than enough. And it will definately work. – Neal Ahluvalia Jun 15 '15 at 09:31
  • 1
    Thanks guys but as you can see in following link, others have same problem as well, http://stackoverflow.com/questions/4728908/android-view-with-view-gone-still-receives-ontouch-and-onclick. I am still able to click the item even when the view is gone :( – Hesam Jun 15 '15 at 09:34

3 Answers3

0

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();
        }
N J
  • 27,217
  • 13
  • 76
  • 96
  • 2
    Nice guess Nilesh, thanks but unfortunately didn't work :( based on what I know invalidate functionality tries to redraw the view rather than changing attributes of the view. – Hesam Jun 15 '15 at 09:52
0

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.

Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
0

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.

JJ86
  • 5,055
  • 2
  • 35
  • 64