Reputation: 137
I have been recently thinking about, how can I make my code more efficient? I have a Floating Action Button which does a common task when clicked on. I want this button to be present in all activities. When pressed this FAB button it opens a circular menu (https://github.com/Ramotion/circle-menu-android) wherein I can open any activity I click on. I do not want to pass any reference to another activity I just want to start that activity. Now in order to achieve this what I currently do is declare the FAB button and write same piece of code over and over again in all the activities I want to use this button in. My question is how can avoid repetition of the same piece of code and get my work done more easily. Is there a way I can globally declare a function and call it in onclicklistener method in all the activities? Thank You
Upvotes: 1
Views: 44
Reputation: 93668
OnClickListener is a class. Just create a subclass that does what you want, and pass an instance of that to setOnClickListener.
Or if you really never want to do anything else, subclass FAB and override its constructor to set the onClickListener with a listener that does what you want. Then use that subclass instead of the base FAB class in your xml.
Upvotes: 1