1

A Layout has the following structure:

 <LinearLayout
        android:id="@+id/card_pack_clickable_area"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/card_pack_bg_selector">

            <FrameLayout
                android:id="@+id/card_pack_body_frame_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/bg_card_pack_body">

                <...>

            </FrameLayout>

            <TextView
            android:id="@+id/card_pack_percentage"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/bg_card_pack_body"/>

  </LinearLayout>

Content of @drawable/card_pack_bg_selector":

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_pressed="true">
      <shape>
          <solid android:color="#536dfe" />
      </shape>
  </item>

  <item android:state_focused="true">
      <shape>
          <solid android:color="#536dfe" />
      </shape>
  </item>

  <item android:drawable="@android:color/transparent"/>

</selector>

Problem statement

So the LinearLayout card_pack_clickable_area has two elements: a FrameLayout card_pack_body_frame_layout and a TextView card_pack_percentage.

What I'm trying to achieve is to have the FrameLayout and the TextView as a single clickable area, that's why I've wrapped them into outer LinearLayout.

In Java code I put onClickListener on this LinearLayout. Actually I'm obtaining onClick event when I click either on the FrameLayout or on TextView.

The problem is that the LinearLayout does not change its state as it should do following its background (@drawable/card_pack_bg_selector").

The question

How can I have the FrameLayout and the TextView as the united clickable area with the single background, which changes its state pressed/unpressed?

Adam
  • 842
  • 2
  • 14
  • 22

4 Answers4

3

Can you please try to add android:clickable="true" to your outter linear layout.

If is does not work you can also try to add android:descendantFocusability="beforeDescendants" to your outter layout again..

Good luck there

Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • Actually, your method does not work, but it suggested an idea to me: I should've remove background attributes from inner elements (in my case the FrameLayout and the TextView). Thank you! – Adam Apr 15 '15 at 06:48
  • I am glad that it have enlighted you! – Emre Aktürk Apr 15 '15 at 06:52
0

Maybe, based on this question, you have to make your LinearLayout clickable? Remember to google before asking ;-)

Community
  • 1
  • 1
Seraphis
  • 1,026
  • 2
  • 14
  • 30
0

I should've remove background attributes (android:background="@color/bg_card_pack_body") from inner elements (in my case the FrameLayout and the TextView).

Adam
  • 842
  • 2
  • 14
  • 22
-1

replace this code with your old code in @drawable/card_pack_bg_selector and try i hope it's working....

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="#536dfe" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="#536dfe" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="#536dfe" android:state_focused="true"/>
    <item android:drawable="@android:color/transparent" android:state_focused="false" android:state_pressed="false"/>

</selector>
Ravi Makvana
  • 2,872
  • 2
  • 24
  • 38