Adam
Adam

Reputation: 862

LinearLayout onClick works, but the state does not change

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?

Upvotes: 1

Views: 1235

Answers (4)

Adam
Adam

Reputation: 862

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

Upvotes: 0

Emre Akt&#252;rk
Emre Akt&#252;rk

Reputation: 3346

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

Upvotes: 3

Ravi Makvana
Ravi Makvana

Reputation: 2912

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>

Upvotes: -1

Seraphis
Seraphis

Reputation: 1036

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

Upvotes: 0

Related Questions