When do i need to use android: clickable?

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

PS. I implemented an advertising SDK and found that their developers used android:clickable with WebView , and I was intrigued why they used it.

+12
source share
6 answers

As stated in the documentation, and as far as I know:

clickable - Determines if this view responds to click events. There must be a boolean value, either "true" or "false".

So, for example, if you simply declare Webview or View in your layout.xml and try to set OnClickListener in these views, the OnClick event OnClick not be fired if you do not specify an attribute:

  android:clickable=true 
+7
source

clickable seems useful when you want your view to consume clicks so that they don’t go to the top views.

For example, I have a FrameLayout that I show above the base RelativeLayout at a specific time. When the user clicks on the main EditText , the focus moves to EditText . Really annoying when FrameLayout is still displayed. Now the user does not know why the keyboard just popped up or where they type.

When I set clickable="true" in FrameLayout , users could no longer accidentally name the 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> 
+3
source

When you set view.setOnClickListener to any View , for example: myButton.setOnClickListener(new OnClickListener) defaults to clickable="true" .

Therefore, you do not need to mention this in the XML file, for example android:clickable="true" . The onClick() event will onClick() without using android:clickable="true" .

+2
source

I had a situation when I did a swipeable operation (swipe left / right or left / right to move forward / backward). On some screens there were places that were filled only with LinearLayout. There was no OnClickListener for the layout (this was not necessary), as a result of which the swipe action was not registered when LinearLayout executed. Setting android:clickable="true" solved the problem.

To answer your questions and as shown above, there are situations where using the clickable attribute may be useful.

I do not believe that you can say that setting OnClickListener is the same as setting the clickable attribute, but setting OnClickListener certainly makes the View clickable creating the clickable -attribute attribute is useless (in this particular situation).

+1
source

I do not know why they used in this case, but I had to use it when I created a class that extended linearLayout.

I created my own “control” and I wanted it to be clickable, so I had to use it.

This is one of the scenarios when you will use it.

0
source

FYI; When you use the android:onClick="" software 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 for when you should set the self-locking flag yourself, I think mainly when you want the view not to be hacked, for example, to stop repeated calls while loading some data, etc.

0
source

Source: https://habr.com/ru/post/899588/


All Articles