Maximus
Maximus

Reputation: 1299

Button state_pressed="true" never triggered, why?

I have the following button defined in an activity layout:

<Button
    android:id="@+id/button_Collect"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_marginBottom="16dp"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/selector_CollectButton" />

With it I have this selector applied to the button:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:state_pressed="true">
        <!-- PRESSED STATE (Opacity change to 75) -->
        <!-- ==================================== -->
        <layer-list>
          <!-- Shape for the outer circle -->
          <item opacity="75">
            <shape android:shape="oval">
              <solid android:color="#FFBDC3C7" />
            </shape>
          </item>

          <!-- Shape for the circle -->
          <item android:top="18dp" android:left="18dp" android:right="18dp" android:bottom="18dp">
            <shape android:shape="oval">
              <!--<solid android:color="#FF3498DB" />-->
              <solid android:color="#FF22DD44" />
            </shape>
          </item>
        </layer-list>
      </item>

      <item>
        <!-- NORMAL STATE -->
        <!-- ============ -->
        <layer-list>
          <!-- Shape for the outer circle -->
          <item>
            <shape android:shape="oval">
              <solid android:color="#FFBDC3C7" />
            </shape>
          </item>

          <!-- Shape for the circle -->
          <item android:top="18dp" android:left="18dp" android:right="18dp" android:bottom="18dp">
            <shape android:shape="oval">
              <solid android:color="#FF3498DB" />
            </shape>
          </item>
        </layer-list>
      </item>
    </selector>

Why is it that the pressed state never tiggers whenever I touch the button in my app?

I also tried splitting my "states" into separate xml drawable files, but the problem still remained.

As for the event, I have the following in my activity:

Button btnCollect = FindViewById<Button>(Resource.Id.button_Collect);
btnCollect.Touch += btnCollect_Touch;

This is C# code since I'm working with Xamarin. This code executes without a problem, so I know the touch event is executing.

UPDATE:

I figured something out, it works ONLY if I remove the above event handler:

I have the following code:

private void btnCollect_Touch(object sender, View.TouchEventArgs e)
    {
        if (e.Event.Action == MotionEventActions.Up)
            ScoreManager.Instance.CalculatePoints();
    }

If I don't add a handler to this method the selector works fine, anyone know why ???

Upvotes: 0

Views: 1680

Answers (2)

Maximus
Maximus

Reputation: 1299

Fixed!!

I changed the event from Touch to Click for the button and everything works fine now.

Also to note, many people have said (in other posts) that a Selector with Layer-Lists should be split into separate files. This crowds the project with a lot of xml files. IT IS NOT NECESSARY, you can have the states within the same selector file and it works perfectly fine.

Upvotes: 1

Related Questions