Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom view ignoring `android:enabled` in XML?

Tags:

android

view

I'm setting android:enabled="false" on a custom view, but it doesn't appear to be having any effect on the isEnabled() property.

Here's a simple test case:

public class TestView extends View {

    public TestView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint p = new Paint();
        p.setTextAlign(Align.CENTER);
        p.setTextSize(ViewUtils.dpToPx(this, 10));
        canvas.drawText("Enabled = " + (isEnabled() ? "true" : "false"),
                getWidth() / 2,
                getHeight() / 2,
                p);
    }
}

And the corresponding XML:

<com.example.TestView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@android:color/darker_gray"
    android:enabled="false"
    android:layout_width="100dp"
    android:layout_height="100dp"/>

The output view looks like this:

output

Can someone please explain why?

like image 760
user3452758 Avatar asked Apr 25 '14 20:04

user3452758


2 Answers

Use this instead:

app:enabled="@{false}"
like image 41
Ahmed AlAskalany Avatar answered Oct 10 '22 15:10

Ahmed AlAskalany


As far as I can tell, android:enabled is not an attribute of View, as it is not in the documentation.

I don't think there is a way to modify the return value of isEnabled() via the XML layout file. (Unless defining custom attributes for a custom view)

like image 115
njzk2 Avatar answered Oct 10 '22 16:10

njzk2