nicolausYes
nicolausYes

Reputation: 653

Selector for layout inside DrawerLayout doesn't work

I have a view aligned to the bottom of a navigation drawer that is not in list.

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/readscreen_bg">

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/loading_layout"
            android:visibility="invisible">
                <ProgressBar
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/progressBar"
                    android:layout_centerInParent="true" />

        </RelativeLayout>
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/navigation_drawer_content"
        android:layout_width="@dimen/navigation_width"
        android:layout_height="match_parent"
        android:background="@color/navdrw_bg"
        android:clickable="true"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:layout_gravity="start" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/navigation_support_item_height"
            android:id="@+id/support_project"
            android:focusableInTouchMode="true"
            android:clickable="true"
            android:focusable="true"
            android:background="@drawable/support_project_selector"
            android:layout_alignParentBottom="true">

            <ImageView
                android:layout_width="@dimen/navigation_item_icon_size"
                android:layout_height="@dimen/navigation_item_icon_size"
                android:id="@+id/navigation_icon"
                android:layout_marginLeft="@dimen/navigation_item_icon_left_margin"
                android:layout_marginRight="@dimen/navigation_item_icon_right_margin"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_menu_support"
                android:layout_alignParentLeft="true"/>

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/navdrw_bg"
                android:text="@string/navigation_support_project"
                android:textSize="@dimen/navigation_item_text_size"
                android:id="@+id/navigation_name"
                android:layout_centerVertical="true"
                android:gravity="center_vertical"
                android:layout_toRightOf="@+id/navigation_icon"/>

        </RelativeLayout>

        <ListView android:id="@+id/left_drawer"
            android:layout_width="@dimen/navigation_width"
            android:layout_height="match_parent"
            android:layout_above="@+id/support_project"
            android:layout_gravity="left"
            android:dividerHeight="0dp"
            android:divider="@null"/>

    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

It is, with id support_project. The problem is selector for this layout doesn't work.

support_project_selector.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector
        xmlns:android="http://schemas.android.com/apk/res/android">

        <!-- Selected -->
        <item
            android:drawable="@color/navdrw_support_block_bg_pressed"
            android:state_focused="true"
            android:state_selected="true"/>

        <!-- Pressed -->
        <item
            android:drawable="@color/navdrw_support_block_bg_pressed"
            android:state_focused="false"
            android:state_selected="true"/>

        <!-- When not selected -->
        <item
            android:drawable="@color/navdrw_support_block_bg"/>

    </selector>

I was trying many variants and nothing helped. Selector does install color when view is not clicked but when it is clicked it doesn't change it's color.

But layout is clickable, I do receive click event and can process it. The problem is only with click/activate color.

Colors in selector for active and normal state are 100% completely different.

Upvotes: 2

Views: 541

Answers (1)

joao2fast4u
joao2fast4u

Reputation: 6892

There are multiple aspects you are handling not so good, here.

First, your selector possible states:

You should have an item tag per state you want to customize and one different android:state value per item tag. So, remove your android:state_focused attributes from both states (selected and pressed).

On your pressed state, you are using android:state_selected="true" when you should be using android:state_pressed="true". This will make your selector work when the item is actually pressed, not only when it is selected.

Second, in order to affect the selector to your RelativeLayout, you have to set it clickable by adding android:clickable=true attribute in your xml layout. RelativeLayout is not clickable by default, unlike a Button, for example.

For last, add android:duplicateParentState="true" to your RelativeLayout children Views, ImageView and TextView. This attribute allows the children to have the same state as the parent. When your RelativeLayout is selected or pressed, that state will be replicated by its children.

And you are done. Your selector is working.

Upvotes: 1

Related Questions