When should one use android:clickable?
Asked Answered
W

6

24

When should we use android:clickable in XML? Should we ever?
Is there any difference between such XML declaration and in-code declaration myButton.setOnClickListener? I have read the documentation, but I could not find out when and why should I ever use this attribute.

PS. I was implementing an ad SDK and found that their developers were using android:clickable with WebView and I was intrigued why did they use it.

Wingspread answered 19/10, 2011 at 6:55 Comment(0)
F
8

As the documentation states, and as far as I know :

clickable - Defines whether this view reacts to click events. Must be a boolean value, either "true" or "false".

So for example if you just declare a Webview or View in your layout.xml and try to set an OnClickListener on this views the OnClick event won't be fired unless you specify the attribute :

  android:clickable=true
Fluorosis answered 19/10, 2011 at 7:4 Comment(7)
I am confused. If I have a Button or any other View, I never use this value and I can implement onClickListener thou. I think I did not get what you said.Wingspread
yes the button is by default clickable. but for instance ImageView I think it is not by default clickable.Fluorosis
setOnClickListener makes the View clickable: docsChlorella
@Chlorella Thanks. Now back to my question: Why and when would I use android:clickable if I can just implement onClickListener?Wingspread
@bergnam instead of onClickListener(this); (on an activity that implements OnClickListener) you could put onClick="onClick" and have the same method prototype [public void onClick(View v)] that doesn't require your activity to implement OnClickListener. Why? for shiz & giggles. You can set clickable=false when you don't want to make the view enabled=false, but you don't want to un-assign/reassign the onClickListener either. You might want a view to have a different draw state when clicked, but not do anything. Setting both clickable=true and setOnClickListener is redundant. Both have uses.Chlorella
you should put this in an answer I thinkFluorosis
old QA, but its worth to note that WebView isn't a proper extension of View for an example, because we can't set (easily) `OnClickListener' :) all other works as far as I knowIrritated
B
12

clickable seems to be useful when you need a view to consume clicks so that they do not go to views beneath the top view.

For example, I have a FrameLayout that I display over an underlying RelativeLayout at certain times. When the user would click on an underlying EditText the focus would shift to that EditText. Really annoying when the FrameLayout was still being shown. Now the user doesn't know why a keyboard just popped up or where they are typing.

When I set clickable="true" in the FrameLayout, users could no longer accidentally click underlying EditText fields.

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ...>
    <EditText>
    <EditText>
    <EditText>
    <!-- FrameLayout with grayed-out background. -->
    <FrameLayout
        android:id="@+id/sometimes_visible_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#80808080"
        android:clickable="true"
        android:visibility="gone"
        android:focusable="true"
        ...>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            ...>
            <View>
            <View>
        </LinearLayout>
    </FrameLayout>
</RelativeLayout>
Briarwood answered 7/9, 2017 at 17:7 Comment(0)
F
8

As the documentation states, and as far as I know :

clickable - Defines whether this view reacts to click events. Must be a boolean value, either "true" or "false".

So for example if you just declare a Webview or View in your layout.xml and try to set an OnClickListener on this views the OnClick event won't be fired unless you specify the attribute :

  android:clickable=true
Fluorosis answered 19/10, 2011 at 7:4 Comment(7)
I am confused. If I have a Button or any other View, I never use this value and I can implement onClickListener thou. I think I did not get what you said.Wingspread
yes the button is by default clickable. but for instance ImageView I think it is not by default clickable.Fluorosis
setOnClickListener makes the View clickable: docsChlorella
@Chlorella Thanks. Now back to my question: Why and when would I use android:clickable if I can just implement onClickListener?Wingspread
@bergnam instead of onClickListener(this); (on an activity that implements OnClickListener) you could put onClick="onClick" and have the same method prototype [public void onClick(View v)] that doesn't require your activity to implement OnClickListener. Why? for shiz & giggles. You can set clickable=false when you don't want to make the view enabled=false, but you don't want to un-assign/reassign the onClickListener either. You might want a view to have a different draw state when clicked, but not do anything. Setting both clickable=true and setOnClickListener is redundant. Both have uses.Chlorella
you should put this in an answer I thinkFluorosis
old QA, but its worth to note that WebView isn't a proper extension of View for an example, because we can't set (easily) `OnClickListener' :) all other works as far as I knowIrritated
T
2

When you are setting view.setOnClickListener on any View, eg: myButton.setOnClickListener(new OnClickListener) by default it is considered as clickable="true".

So you would not need to mention that in the XML file like android:clickable="true". The onClick() event will be fired without using android:clickable="true".

Tempt answered 13/6, 2017 at 14:37 Comment(0)
G
1

I've experienced a situation, where I had made an activity swipeable (swipe left/right or right/left to move forward/backwards). In some screens there were places that was only filled out with a LinearLayout. There was no OnClickListener for the layout (it wasn't needed) causing the swipe action not to be registered when performed on the LinearLayout. Setting the android:clickable="true" solved the problem.

To answer your questions and as the above shows, there are situations in which using the clickable-attribute can be usefull.

I don't believe you can say that setting an OnClickListener is the same as setting the clickable-attribute, but setting the OnClickListener certainly makes the View clickable, making the clickable-attribute useless (in that particular situation).

Geometrid answered 19/10, 2011 at 7:5 Comment(2)
If you make LinearLayout clickable and it has child elements, how will Android distinguish if I want to fire element's onClick or layout's onClick? Will it work like this: if I touch element, then its onClick will be fired, if I click on empty space (inside of layour), then LinearLayout's onClick will be fired?Wingspread
Yes, that's what would happen. All though in my above example there was no onClick-event registered for the LinearLayout - I just needed it to register clicks in order for them to be passed to my swipe functionality.Geometrid
A
1

FYI; When you use the android:onClick="" attribute or call setOnClickListener(...) programmatically, the View class sets the clickable flag to true.

/**
 * Register a callback to be invoked when this view is clicked. If this view is not
 * clickable, it becomes clickable.
 *
 * @param l The callback that will run
 *
 * @see #setClickable(boolean)
 */
public void setOnClickListener(@Nullable OnClickListener l) {
    if (!isClickable()) {
        setClickable(true);
    }
    getListenerInfo().mOnClickListener = l;
}

As to when you should set the clickable flag yourself, I think mostly when you want a View to not be cickable, e.g. to stop repeated calls while some data is loading, etc.

Absorbing answered 15/5, 2019 at 3:16 Comment(0)
M
0

I don't know why did they use in that case, but, I had to use it when I created a class, which was extending linearLayout.

I created my own "control", and I wanted it clickable, so I had to use it.

This is one scenario when you will use it

Marketplace answered 19/10, 2011 at 7:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.