Bunny Rabbit
Bunny Rabbit

Reputation: 8411

How to make RelativeLayout in state disabled?

I have a relative layout, which uses a statelist to display different drawables in the states pressed, focused and disabled. Now the states pressed, normal are working fine, but when i do a relativeLayoutObject.setEnabled(false), the correct drawable is not displayed. I have also relativeLayoutObject.setClickable(false) and setting android:state_enabled="false" from XML but none of them is working.

This is my layout :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:clickable="true"
    android:layout_height="match_parent">

        <!--SOME ITMES INSIDE THE LAYOUT-->
<RelativeLayout>

and the selector is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/drawable_d" android:state_enabled="false"/>
    <item android:drawable="@drawable/drawable_n" android:state_focused="true"/>
    <item android:drawable="@drawable/drawable_p" android:state_pressed="true"/>
    <item android:drawable="@drawable/drawable_n"/>
</selector>

I am applying the selector as : relativeLayoutObject.setBackgroundDrawable(R.drawabled.button_state_list);

Upvotes: 0

Views: 1055

Answers (1)

dafNou
dafNou

Reputation: 339

Just came across your question, even it's been 3 yrs since you asked :)

RelativeLayouts do not have a disabled/enabled state, so what I did was to originally set a background drawable style to my RelativeLayout like this:

        <RelativeLayout
            android:id="@+id/activity_register_btn"
            ...
            android:background="@drawable/style_button_yellow_disabled_rounded">

            <!-- Register button -->
            <TextView
                ... />
        </RelativeLayout>

So my style's code (within drawbles folder):

style_button_yellow_disabled_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- view background color -->
    <solid
        android:color="#ffdca5" >
    </solid>
    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="#ffdca5" >
    </stroke>
    <!-- If you want to add some padding -->
    <padding
        android:left="12dp"
        android:top="12dp"
        android:right="12dp"
        android:bottom="12dp">
    </padding>
    <!-- Here is the corner radius -->
    <corners
        android:radius="30dp">
    </corners>
</shape>

Then, in my java file, when needed, I changed the background to another xml which was the enabled view:

style_button_yellow_rounded.xml

<?xml version="1.0" encoding="utf-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- view background color -->
    <solid
        android:color="#fcc002" >
    </solid>
    <!-- view border color and width -->
    <stroke
        android:width="1dp"
        android:color="#fcc002" >
    </stroke>
    <!-- If you want to add some padding -->
    <padding
        android:left="12dp"
        android:top="12dp"
        android:right="12dp"
        android:bottom="12dp">
    </padding>
    <!-- Here is the corner radius -->
    <corners
        android:radius="30dp" >
    </corners>
</shape>

Upvotes: 2

Related Questions