Hi I'm trying to make my linear layout work like button.
I mean I'm trying to change its background color when the state is changed.
I used selector to solve it, but it didn't work.
I looked for solutions and all they say was add clickable attribute.
I've already done that.
My LinearLayout contains two LinearLayout which contains 9 TextViews each.
They fill my LinearLayout fully.
What I thought of was that its child is absorbing the click event and my LinearLayout doesn't change its state to pressed.
So I put clickable and focusalbe attribute to false on every child of my LinearLayout.
However, it's still same.
Here's my code.
This is the selector jbbtn.xml
android:drawable="@drawable/jbbtn_pressed"/>
android:drawable="@drawable/jbstyle_transparent"/>
And This is My LinearLayout
android:id="@+id/llCurrents"
android:background="@drawable/jbbtn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/llTimer"
android:layout_below="@id/btnMenu"
android:layout_marginRight="3sp"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="3sp" >
~~~~~~
# Answer 1
I am using a linear layout as a button however, I do not have anything assigned as clickable or not and it seems to work just fine. I have set up a style for my standard button and I just assign that style to the linear layout like I would any other button.
The linearlayout as a button:
style="@style/btn_stand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickLLButton"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button Label" />
android:id="@+id/tvLLButton"
android:layout_height="0px"
android:layout_weight="1"
android:gravity="center"
android:text="Button Info" />
My style definition for the button:
@drawable/btn_stand_sel
@drawable/btn_stand_text_color
48dp
5dp
5dp
My @drawable/btn_stan_sel file:
My drawable file repeated for each state just with different colors for each state:
android:shape="rectangle" >
android:width="1dp"
android:color="@color/stroke" />
# Answer 2
You can add:
android:clickable="true"
to LinearLayout.
# Answer 3
Although this question has answer I am writing this to clarify and give my observation.
answer by @minelight does not work on views that are not clickable, for example LinearLayout and TextView. The statePressed state is available only for clickable views.
The second answer actually completes the first one.