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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" android:state_pressed="true"
android:drawable="@drawable/jbbtn_pressed"/>
<item android:state_enabled="true"
android:drawable="@drawable/jbstyle_transparent"/>
<item android:state_enabled="false" android:drawable="@drawable/jbbtn_disabled"/>
</selector>
And This is My LinearLayout
<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" >
~~~~~~
</LinearLayout>
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:
<LinearLayout
style="@style/btn_stand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClickLLButton"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button Label" />
<TextView
android:id="@+id/tvLLButton"
android:layout_height="0px"
android:layout_weight="1"
android:gravity="center"
android:text="Button Info" />
</LinearLayout>
My style definition for the button:
<style name="btn_stand" parent="AppBaseTheme">
<item name="android:background">@drawable/btn_stand_sel</item>
<item name="android:textColor">@drawable/btn_stand_text_color</item>
<item name="android:minHeight">48dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">5dp</item>
</style>
My @drawable/btn_stan_sel file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- disabled state -->
<item android:drawable="@drawable/btn_stand_disabled" android:state_enabled="false"/>
<!-- enabled and pressed state -->
<item android:drawable="@drawable/btn_stand_pressed" android:state_enabled="true" android:state_pressed="true"/>
<!-- enabled and focused state -->
<item android:drawable="@drawable/btn_stand_focused" android:state_enabled="true" android:state_focused="true"/>
<!-- enabled state -->
<item android:drawable="@drawable/btn_stand_enabled" android:state_enabled="true"/>
</selector>
My drawable file repeated for each state just with different colors for each state:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="@color/stroke" />
<solid android:color="@color/blue" />
<corners android:radius="6dp" />
</shape>
You can add:
android:clickable="true"
to LinearLayout
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With