4

Recently Android Studio shows me a warning in the onClick element of my xml views.

Use databinding or explicit wiring of click listener in code

Suppress Add tools:ignore="UsingOnClickInXml" attribute

This is my View:

<ImageView
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_gravity="center"
    android:scaleType="fitCenter"
    android:src="@drawable/ic_web_white_24dp"
    android:clickable="true"
    android:focusable="true"
    android:onClick="openSite" />

What is the meaning of the warning? What's wrong in using android:onClick?

Edit: I know the difference between setting the onClick on xml and declaring the onClickListener in the Activity but I want to know why the last one is preferable.

MattButtMatt
  • 461
  • 2
  • 11
  • 26
  • 3
    Does this answer your question? [How exactly does the android:onClick XML attribute differ from setOnClickListener?](https://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) – Kostas Andrianos Jun 02 '21 at 22:39
  • For me the warning started after updating dependency 'androidx.appcompat:appcompat:1.2.0' to 'androidx.appcompat:appcompat:1.3.0'. Prior to the dependency update, I never saw this warning. So it is unclear why studio is asking us to use databinding or an explicit onClickListener() instead of xml onClick. – Java42 Jun 05 '21 at 00:36
  • 1
    @KostasAndrianos - No, that question does not answer the reason for the warning. – Java42 Jun 05 '21 at 00:39
  • It's a warning about a good practice it's not an error. The answers in the question I linked and the answer I gave explain exactly why you get the warning. @Java42 – Kostas Andrianos Jun 06 '21 at 07:58
  • 1
    It's a warning but I want to know what are the downsides of using the xml – MattButtMatt Jun 06 '21 at 08:35
  • @KostasAndrianos - You are missing the point of the OP's question. Warning vs Error is not the issue. How to implement an onClickListener is not the issue. How to implement onClick via XML is not the issue. Difference between the two techniques is not the issue. This is a new warning so why are we now being asked to switch from XML onClick to either 1) explicit onClickListener() or 2) databinding? – Java42 Jun 06 '21 at 17:57
  • I'm not a Google employee to tell you that but they are pushing Jetpack navigation a lot, meaning single-activity/multi-fragment applications. For these kinds of applications, for reasons I already explained, it's a bad practice (and it's not scaleable) to have the listeners in xml because you'll have to implement all of them in MainActivity. Listeners belong in the fragment they're used not in the MainActivity of your application. @Java42 Also OP edited the question 9h ago, the point wasn't clear from the original question, which I answered. OP hasn't accepted my answer yet... – Kostas Andrianos Jun 06 '21 at 18:25

1 Answers1

0

Android will set the onClick Listener as if you did it yourself. The difference is that you have to implement the method you put in android:onClick in the current Activity, which might be a problem if you're using fragments.

If you have many buttons, you can litter the Activity with methods that do not belong there. The most common use case where that would be a problem is a single-activity application. If you use 40-50 fragments and only set listeners on the XML (which isn't always possible, I don't think you can do it on a RecyclerView for example), then you have to implement all the listeners in the only Activity your app has. I don't want to imagine how messy that would be.

It has been extensively discussed in this answer's comments

It's not an error, it is a warning so you can ignore it if you like but it's best if you don't.

Kostas Andrianos
  • 1,551
  • 2
  • 16
  • 21
  • 1
    Thanks for the answer but I still don't understand why using onClick in xml is a bad practice. Like you said, the Activities are not full of onClickListeners so setting them in the xml I think it's a more elegant solution. What is the downside? – MattButtMatt Jun 06 '21 at 08:34
  • ??? You need to implement the `onClickListener` *in* the activity, even though you reference it in the xml. You need to define what to do on the click event of something (like a button) and you have to write a method to do so. If you use the xml attribute, you need to do it in the Activity, if you don't, you can set it in exactly where you like. – Kostas Andrianos Jun 06 '21 at 15:14