How to call a Method in View using a Class that extends from Fragment

Asked

Viewed 310 times

0

I have several buttons in a view that call the addNumero method in Onclick, something like this:

<Button
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:gravity="center"
  android:onClick="addNumero"
  android:text="8" />

And since there are many buttons, it is impossible to use the setOnClickListener in each. I have a class that inherits from Fragment: public class Exemplo extends Fragment and est class inflates the view that contains the buttons and in that class is also the method addNumero but it’s not working, when I click it doesn’t call the addNumero.

  • 1

    The use of android:onClick only works when the view is used in an Activity.

  • So, but my view is used in a fragment, that’s the problem

  • Like the android:onClick does not work in a Fragment must use setOnClickListener().

1 answer

1

The attribute onClick only works when the view is being used in an Activity, you will have to implement an View.OnClickListener.

If you have many on-screen buttons with similar functions, you can create a single class to use as a listener.

private class MyClickListener implements View.OnClickListener

If the buttons have different functions, create several classes in separate files, so even with a lot of code, everything will be organized.

Sources:

https://github.com/chrisjenx/Calligraphy/issues/305

https://hackernoon.com/4-ways-to-implement-onclicklistener-on-android-9b956cbd2928

EDIT: enlightened response with the help of /users/2541/ramaral

  • 1

    "It is not recommended to use onClick in xml, this file should represent only the visual, not call functions." Why? That’s the equivalent of setOnClickListener(), only limits that the method exists in the Activity. On the other hand if "this file should represent only the visual, not call functions.", then any kind of "Binding" would be too "Not recommended".

  • The issue of not calling functions in xml is a conceptual MVC issue, where View and Controller should be separated.

  • 1

    Yes, that is true, but what is at stake here has nothing to do with it. The method assigned in android:onClick just have to call another one in the Controller. What should be separated is the implementation and if it involves the Model. Actions that do not involve the Model, which only have an effect on the UI, should not be implemented in the Controller.

  • You’re right, but, as you put in the question comment yourself, onClick only works when the view is used in an Activity, so my answer is still valid, I’ll edit to be clear.

  • 1

    Maybe you didn’t explain it well. What I mean is that you won’t use/implement the View.Onclicklistener interface in the Controller. I mean, in terms of MVC, it doesn’t matter android:onClick or setOnClickListener(), the method onClick() will not be implemented/used in the Controller.

  • I understood, I hadn’t thought of it that way.

  • Behold here what I think between using android:onClick or setOnClickListener()

  • @ramaral, A class Myclicklistener I do on the outside, or I implement the View.Onclicklistener in the Class itself where I am working with the buttons? And if I call on the outside, how do I use it within the Class I’m working on? Thank you for your attention

  • If you make a Myclicklistener class, it implements View.OnClickListener. To use in the class you are working in(Fragment) aSuaView.setOnClickListener(new MyClickListene()).

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.