Android Button

 

Android Button is basically an interface by which we can collect user’s click events. We can form android button using Text as well as Images, we will see it soon in this tutorial.

 

Android Buttons can be implemented to listen user clicks in 2 ways.

  1. Creating Button’s onClick-Listener directly from java File (MainActivity.java).
  2. Creating Button click handler method in java File (MainActivity.java) and adding its reference to our button in XML Layout file (activity_main.xml). This approach is better one when we want to reuse the code.

( To understand the above two implementation refer Button-1,2 from our project – Button. )

 



Download
Source Code : Button.zip


Now Let us work on Project : (Refer – Configuring Existing AndroidStudioProject) Button which you might have downloaded from above or you can follow the scripts below by copying it to implement in your Project. To launch Downloaded project, Extract the zip, copy it into AndroidStudioProjects. Now from Android Studio click open Existing Android Studio Project and select the path to extracted file we just copied in AndroidStudioProjects directory. If Sync SDK path prompt comes just click OK and you are ready to work. If still you get error check for AndroidStudioProjects/Button/app/build.gradle , it will contain different SDK Version. Change it to SDK Version which you have.

 

 

1. activity_main.xml : Layout file

 

Explaining above Code Separately :

Button – 1

  • Over here we have created a button and named it using property android:text=”Botton 1″
  • We have added onclick-Listener from MainActivity.java file for our 1st Button.

 

Button – 2

  •  Line – 6 We have added onclick handler method named btn_click_method_btn2, which is implemented inside our MainActivity.java. So whenever user clicks on 2nd Button, btn_click_method_btn2 will be invoked from MainActivity.java File.

 

Button – 3

  • Line – 6 Over here we have placed an Button with image instead of Text-Button, by specifying android:background property. “@mipmap/ic_launcher” means the image ‘ic_launcher’ is placed inside ‘mipmap’ folder inside ‘Res’ folder. We have not added any text to our button, it will be displayed simply as an image.

 

Button – 4

  • Our 4th Button contains both Image + Text.
  • Line – 7 We have added Image which is aligned to left of the text using property android:drawableLeft.

 

 

2. MainActivity.java

Explaining above Code :

  • Line 13 & 24 Initially we have declared the 1st button, so that we can use it to implement onClickListener(). (We don’t require to declare any other Button for using them here, since they will be used directly from click-handler method)
  • Line 29 We attach a click Listener for our 1st button using btn1.setOnClickListener and as its parameter we pass  instance of class OnClickListener.
  • Line 32 So as to use predefined method of button clickListener class, we over-ride onClick(View arg0). Here we are working on UI thread so we had pass instance  (View arg0) as argument.
  • Line 34 This is the place where we should add our code which is to be implemented on button click. Over here with user click, we are changing text of the TextView using textView.setText().
  • Line 45 This is our 2nd way to implement button click using handler method which we had mentioned in our XML Layout File (activity_main.xml-Line:21). Whenever user clicks on 2nd Button method named btn_click_method_btn2 will be invoked.
  • Line 47 We change text for TextView on Button click with reference to 2nd Button click.
  • In same manner we have implemented below 2-handlers for 3rd & 4th Button.

 

 

Final Output :

android button

 

Do It Yourself :

Inside onCreate() method try changing Button string using btn_name.setText(), but make sure that you have declared your button both in XML Layout file as well as java File ( Changing text for a button is quite similar to that we learned for TextView). Also see that you can name different method names for click Handler as they are not reserved ones.

 

Trouble-Shooting :

  • App Crashes (Error says – java.lang.IllegalStateException: Could not find a method for onClick handler on view class )
    Check that you are not working outside of UI thread i.e. you should be working inside onCreate() or onStart() or onResume() or the onClick handler method has been passed View argument i.e. handler_method(View v) or see whether the click-handler method is not missing in MainActivity.java.

 

Report Errors + Bugs & Become Insider for Nestedif.com

We would like to hear you, if you find any error or misspelled phrase while reading our tutorials. By reporting mistakes through email to insider@nestedif.com you could help other peers.