View.setEnabled(false) does not work quite right in android

android button setenabled not working
android disable button gray
disable switch android
android disable button after click
android disable button until all fields are entered
java setenabled
how to disable overview button in android programmatically
android imageview disabled state

I just found out View.setEnabled(false) does not work quite right in android, in the following ways:

  1. Disable parent view does not automatically disable child views. This is inconvenient.
  2. Though grayed in color, disabled views can still get focus and get keyboard input, make choice, etc. This is not right.
  3. When getting keyboard input, DEL/BACK can only remove one key backward no matter how many you input. This behavior only shows in disabled EditText.
  4. Radio label is grayed but radio button image is not grayed.

It seems like a bug. But how do I get a real DISABLE feature?

EDIT: forget to mention the first.

Recursively setting all descendants is needed, otherwise RadioButtons in the layout will not get setEnabled because RadioButtons are grand-children not direct children, there is RadioGroup in between. I record my best solution so far here based on others' answer.

public static void setEnabledAll(View v, boolean enabled) {
    v.setEnabled(enabled);
    v.setFocusable(enabled);

    if(v instanceof ViewGroup) {
        ViewGroup vg = (ViewGroup) v;
        for (int i = 0; i < vg.getChildCount(); i++)
            setEnabledAll(vg.getChildAt(i), enabled);
    }
}

Subclass is not working at this time. If I have:

public MyView class View {
    protected void setEnabled(boolean enabled, boolean setChildren){
        setEnabled(enabled);
        setFocusable(enabled);
        if(setChildren && this isinstanceof ViewGroup){
              for ( int i = 0 ; i < this.getChildCount() ; i++ )
                  //this line will have issue if it is not MyView                      
                  this.getChildAt(i).setEnabled(enabled, true); 
        }
    }
}

unless View itself has this overloaded setEnabled like this:

public class View {
    protected void setEnabled(boolean enabled, boolean setChildren){
        setEnabled(enabled);
        setFocusable(enabled);
        if(setChildren && this isinstanceof ViewGroup){
              for ( int i = 0 ; i < this.getChildCount() ; i++ )         
                  this.getChildAt(i).setEnabled(enabled, true); 
        }
    }
}

This solution will eliminate problems 1, 2, 3. Problem 4 can be solved by setting disabled style selector for RadioGroup - I am not sure why android has no default disabled style selector for RadioGroup. Maybe another point for Android enhancement. Android is very flexible in SDK design also means sometimes extra work needed.

setEnabled(),setClickable() not working, android android-linearlayout. I have used This answer is not exactly as you want but it is a good alternative. Hope it helps Try by working with children instead of the layout itself LinearLayout setEnabled(false); } setEnabled(​enabled); ListView listView = (ListView) view; int listChildCount = listView. View.setEnabled versus android:enabled This post will be about an interesting observation I made when working on a custom View that did not extend EditText , but contained one. To show an example, I created a project that contains a widget called ToggleEditText .

From memory, please excuse typos:

class ExtendedLinearLayout extends LinearLayout{

    protected void setEnabled(boolean enabled, boolean setChildren){

        super.setEnabled(enabled);

        if(setChildren){
              for ( int idx = 0 ; idx < this.getChildCount() ; idx++ ) {
                  (View)(this.getChildAt(idx)).setEnabled(enabled);
              }
        }
    }
}

Distribute.setEnabled(false) has no effect for non debuggable builds , Which OS version did you experience the issue on? any Android OS. What device version did you see this error on? Were you using an emulator  Also when I toggle mDialogPositiveButton.setEnabled(true/false), and log the respective state of isEnabled() when setEnabled() action is triggered, logcat shows the correct alternating True/False value but the Button's UI always remains visually Enabled

An activity that uses a SwipeRefreshLayout as a container for the , Swipe to refresh only works when the view that is set using ListView.​setEmptyView() is not clickable, by setting android:clickable="false"  Android Platform. API level 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1. Manifest.permission. Manifest.permission_group. android.accessibilityservice. AccessibilityService.MagnificationController.OnMagnificationChangedListener. AccessibilityService.SoftKeyboardController.OnShowModeChangedListener.

Just want to update this Michael SM' solution with kotlin. Override for view will looks like:

override fun setEnabled(enabled: Boolean) {
    super.setEnabled(enabled)
    children.forEach { it.isEnabled = enabled }
}

Call now is:

view.isEnabled = false

How to make button unclickable in android, scrollTo or scrollBy Buttons will not react to touch/tap u I can´t make the scatter file. Feb 27, 2017 · In this video, we will see how to make an Clickable Image Slider [duplicate] Ask Question Asked 8 years, How to disable an Android button? I am able to do it via right click, then click in submenu "open lin The script adds  This isn’t quite as much of a problem in modern versions of Windows, but back in the Windows XP days, there were quite a few connection problems when you were using a hidden SSID, not to mention getting disconnected and connecting to the wrong network. Basically, Windows would automatically try to connect to a less preferred network that was

SwipeRefreshLayout: How to use, SwipeRefreshLayout is a new Android layout that adds pull to refresh layout is really easy, but here it is a simple guide to make it work in a few seconds. Methods are quite few: This view must be scrollable, such a ScrollView or a ListView. As a trick, you may be found interesting to disable manual swipe gesture,  Hi same issue here! for testing purposes, we are using http for fetch data to our test physical mobile devices, if react-native block this capability, in our test environments we should install more than one ssl certificate and it made complex routines for internal tests.

ButterKnife, Butterknife is a view binding tool that uses annotations to generate boilerplate code To be clear, Butterknife is not a dependency injection library. Butterknife injects code at compile time. It is very similar to the work done by Android Annotations. DISABLE); ButterKnife.apply(nameViews, ENABLED, false); //We can use  android-youtube-player is a stable and customizable open source YouTube player for Android. It provides a simple View that can be easily integrated in every Activity/Fragment. To interact with YouTube the library uses the IFrame Player API, inside of a WebView, therefore the YouTube app is not required on the user's device and there are no

View.setEnabled versus android:enabled – I Help U, View.setEnabled versus android:enabled. This post will be about an I made when working on a custom View that did not extend EditText , but could think that this is just as simple as adding android:enabled="false" to our  This feature requires a recent location report to work, therefore, not updating its location can make your Trusted Place feature on Smart Lock to stop working. What you need to do is open Google Maps and tap the compass icon on the bottom-right corner. This step will update your location by zoom over to your location and shows it in the form of

Comments
  • the code is too long. i may make a test code and post it later.
  • No need of code for his question, these problem are very common
  • oh, maybe that is one reason why android is not up to iOS quality just guess
  • @MichaelSM most of the time the problem is between the chair and the keyboard.
  • There are many good reasons not to disable children. Because Android took one design decision versus one in iOS does not make either right or better. Don't get me started on iOS quality problems!
  • is "super.setEnabled(enabled);" good here? if for disable case, super.setEnabled(false) will disable its parent also which is often undesirable.
  • Nope. Because this class extends LinearLayout, the super call is to the base class. What you're saying is "do what you normally do, then do my stuff as well". You are extending the method. It will not disable the parent of the layout.
  • For number 1, if there is an option to disable children together if disabling parent, that will be convenient. for number 2, yes only EditText can accept keyboard input in disabled state, but other views besides EditText can also accept focus in disabled state. This is not right.
  • Yes there is way to disable all child. See my updated answer.
  • @Simon, both of your answers are helpful to me but I can only choose one answer so the earliest answer gets the point.
  • I made change according to your suggestion so that all children set "child.setEnable(false);" and "child.setFocusable(false);". The disable behavior goes as expected including EditText views, except it is even worse for radio buttons: they are not disabled at all, neither grayed nor preventing click/tap on them, also can be focusable. Prior to this change, radio button labels were grayed(but not radio button images), and not clickable but can be focusable.
  • No, its not fine at all. Disable must disable child views too. If its about clicks and focus, there are other methods setClickable and setFocusable.