简体   繁体   中英

TextColor of button is not changing onclick of parent LinearLayout

I want to change the textcolor of button and modify image when I click on its parent linear layout. Image is changing but textcolor is not getting changed on click of linear layout.

Here is my code in xml -

<LinearLayout
        android:id="@+id/btn_layout"
        android:layout_width="0.0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.0"
        android:gravity="center"
        android:clickable="true"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/done"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/done_with_accounts_action"
            android:textColor="@drawable/done_button_text_selector"
            android:focusable="false"
            android:textSize="@dimen/done_button_text_size" />

        <ImageView
            android:id="@+id/done_btn_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:paddingLeft="4dp"
            android:focusable="false"
            android:src="@drawable/done_btn_arrow_bg_selector" />
    </LinearLayout>

**selector xml for button -**

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:color="@color/done_btn_pressed_color" />
    <item android:state_pressed="true" android:color="@color/done_btn_pressed_color" />
    <item android:color="@color/button_text_white_color" />
</selector>

If I replace button with TextView, everything works fine.

In this lines:

 <item android:state_focused="true" android:color="@color/done_btn_pressed_color" />
    <item android:state_pressed="true" android:color="@color/done_btn_pressed_color" />

You put the same color done_btn_pressed_color . Maybe thats the problem?

I found the solution - I have to set clickable as false for the done button.Now it also recognizes its parent onClick event.

Why the above code worked with textView because its onClick event is disabled byDefault, so it recognizes its parent onClick event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM