Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setEnabled() vs setClickable(), what is the difference?

Until now, when I wanted to stop the user from pressing the button, I would set the button.setClickable(false); and usually change the text to some kind of grey colour (to let the user know that the button is disabled). Today I stumbled upon the setEnabled() property.

So I went to the documentation to see the method's explanation below:

setEnabled(boolean enabled)    Set the enabled state of this view. 

What does this even mean? What is the difference between enable state/clickable state and disabled state/not clickable state? Could someone please explain what is the difference between doing what I was doing previously, using the clickable property and using the setEnabled() property? What should be used when? I searched Stack Overflow but could not find anything related.

like image 229
Emil Adz Avatar asked Mar 25 '13 13:03

Emil Adz


People also ask

What is setClickable?

setClickable public void setClickable (boolean clickable) It enables or disables click events for the particular view. When a view is clickable it will change its state to "pressed" on every click. if this property of view is disabled then it will not change its state.

What does setEnabled do?

setEnabled(false) disables the button and bttn. setEnabled(true) enables the button. Here is my applet: In the original applet, the button variables are declared in the init() method of the applet, where the buttons are created.


2 Answers

What the hell is that mean?

Quoting the Wikipedia page for "GUI widget":

In the context of an application, a widget may be enabled or disabled at a given point in time. An enabled widget has the capacity to respond to events, such as keystrokes or mouse actions. A widget that cannot respond to such events is considered disabled. The appearance of disabled widget is typically different from an enabled widget; the disabled widget may be drawn in a lighter color, or may be visually obscured in some way. See the image to the right for an example.

This concept has been around for a couple of decades and can be found in most GUI frameworks.

what is the difference between enable state/clickable state and disabled state/ not clickable state?

In Android, a widget that is not clickable will not respond to click events. A disabled widget not only is not clickable, but it also visually indicates that it is disabled.

what do you mean by: "..since it makes the Button visually "disabled"? how does it changes it visually?

What makes a Button look and respond like a Button is its background, which is a StateListDrawable. There is a specific image used for the disabled state.

like image 100
CommonsWare Avatar answered Sep 25 '22 00:09

CommonsWare


A big difference I don't see mentioned elsewhere is with overlapping Views. A View with clickable=true and enabled=false won't allow you to press a View behind it. But a View with clickable=false will allow you to press a View behind it.

like image 30
Trevor Avatar answered Sep 27 '22 00:09

Trevor