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.

<Button
   android:layout_width="wrap_content"
   android:layout_height="wrap_content”
   android:text="Download"/>

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.

<Button
   android:id="@+id/btn_download"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content”
   android:text="Download"/>

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(R.id.btn_download);

//Attaching the OnClickListener
downloadButton.setOnClickListener(new View.OnClickListener() {
   @Override
   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.


0 Comments

Leave a Reply

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

Translate »