Is there any way to disable a view without leaving it gray?

Asked

Viewed 44 times

0

I would like to standardize the views, some screens to view and edit and others just to view. In view mode I leave the views disabled.

I’m trying to keep the appearance similar, using the following code:

private void enableControls(boolean enable, ViewGroup vg){
    for (int i = 0; i < vg.getChildCount(); i++){
        View child = vg.getChildAt(i);
        child.setEnabled(enable);
        if(enable)
            child.setAlpha(1);
        else
            child.setAlpha(.9f);

        if (child instanceof ViewGroup){
            enableControls(enable, (ViewGroup) child);
        }
    }
}
  • setFocusable(false)

  • Almost! But it has the toggle-button that keeps working.

1 answer

1

Hello, Create a custom xml for this view and change the color when it is disabled and then call on background="@drawable/seu_xml

Create an xml in the drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/disable" />
    <item android:color="@color/enable"/>
</selector>
  • Interesting, but I would have to do it for dozens of views ! And each one with a different type, so I’m trying to do it with code.

  • 1

    @Rodrigosantiago try to add so in your loop: child.setBackground(R.drawable.seuDrawable) where seuDrawable is the name of xml based on the answer.

  • I can’t do this

  • 1

    @Rodrigosantiago thought of a better way to do what you want, give a view.setClickable(false); instead of disabling it or deal with a Boolean in setOnClickListener

  • 1

    I just did that, it worked !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.