Not only in Android but in every platform buttons are commonly used to execute some tasks/functions for one time only. But if you need some repetitive work, you can use RxJava or any other methodologies. However, this article is all about dropping some light on Android Buttons.

Anyway, in order to create a Button in our layout file, we need some XML code, as shown below.


So, we can use this much code for showing it on the screen. But no interaction should happen unless we set a listener (View.OnClickListener). And for that, we should set an ID first.


Now move on to the Java file where you are actually using the layout file. It could be an Activity, Dialog Box, Fragment, anything for that matter. So simply we can now initialize the Button as shown below.

//Initializing or Referring the XML button in Java
Button downloadButton = findViewById(;

//Attaching the OnClickListener
downloadButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
      //Here write your download code
// …..

Yes! We can set the OnClickListener as shown in the above code snippet. Although, I’ve already discussed this Topic on my Youtube channel. Must check out these below tutorials.

I hope you got all the points of Android AutoCompleteTextView. Anyway if you liked this article then please Subscribe to our Youtube channel for keeping this talk continue. You can also find us on Facebook and Instagram. Thanks for your support.


Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »