As with many things, the correct approach depends on what you are trying to do for the specific button and what else you are doing with the activity.
Activity class implements interface:
This is a good option when you only have one type of task to execute when this listener is called. An example of this would be a simple form with a number of fields and a save button. I prefer to not have my event listener check the source of the event in order to decide what actually needs to be done. I know that some may say this is a style thing, but I believe that by not requiring the listener to do this check makes the code easier to follow as you will know exactly what is being called for each event.
A different class implements interface:
As I said above, I prefer this option for when I have multiple items that can fire the same event. Extending the above example, lets add a clear button that also requires a click listener. Create one listener that that preforms the save actions and one that preforms the clear actions. Each listener is only added to the components that will produce that action.
This implementation has an additional benefit that you can utilize if you care. The benefit is that it prevents other classes from triggering the event inside of your activity class. Since the interface method must be public, anyone with a reference to the class can fire the event. If you want fine grained control over who can do what in the application, a separate class prevents anyone with a reference to the activity to trigger your form to be cleared or saved (or potentially breaking the code if the listener utilizing the source, but does not handle bad input).
An anonymous inner class implements interface:
This is really just a specific way to construct the second option of using a different class as the implementation. This option can even further restrict who has access to trigger the event as no one else can create an instance of the class. However, I think the more important factor between the two options is how much work is being done. Clearing a few text fields is a simple and straight forward operation. However, the process of saving the for can involve a number of task is you are validating the input (which you should be doing), writing to a database to store the values and triggering some post save action. In this case, making a separate class with its own file will provide a clearer divide between the input form and the data processing. This in turn keeps the form code instead of a larger file with multiple inner classes nested inside.
You should think about Inversion of Control
You basically define an interface for your Layout
which is located somewhere near your Layout class in an own package so you would have an implementation package and a public interface package - for instance call it Layoutable
(I don't know if that's proper English). Now - Layout won't implement that interface but the Figure
class.
Likewise you would create an interface for Figure that's Drawable
for instance.
So
my.public.package.Layoutable
my.implementation.package.Layout
my.public.package.Drawable
my.implementation.package.Figure
Now - Figure implements Layoutable and thus can be used by Layout and (I'm not sure yet if that is what you wanted) - Layout implements Drawable and can be drawn in a Figure. The point is, that the class that exposes some service makes it available by an interface (here: Layout and Layoutable) - the class that wants to use that service has to implement the interface.
Then you would have something like a creator object that binds both together. So the creator would have a dependency to Layout
as well as to Figure
, but Layout
and Figure
themselves would be independent.
That's the rough idea.
An excellent source for solutions to this problems is the book Java Application Architecture by Kirk Knoernschild.
Best Answer
Here are the best and better programming way for to work with button /any other widget
Declare Button/ any other widgets Object outside from the Activity life cycle method; mostly after declaration of the public class like this
public class MainActivity extends Activity {
Button button;
Always define inside the
onCreate(Bundle savedInstanceState)
method; because it's guaranteed thatonCreate()
always call so therefore it's better to define inside onCreate() method.Here is the complete Code example
On the above I have used anonymous class
onClickLListener
that is the nested class of View class; so you can also use separate class Avoid to make switch statement because it's make your like spaghetti like.