Friday, April 15, 2016

Buttons and Button Clicks


Running some code on a button click is actually an easy thing to do, and there are many ways to do it. If you are not comfortable with the way you are currently used to, you may try this method.

When you define Buttons in the XML Layout, using the Button tag, just include an extra attribute, android:onClick to it, for example, like this :

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/bStart"
android:onClick="MyClick"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />


If you see, I have written the value for the onClick attribute as "MyClick". This basically means, execute the function named MyClick which is present in the Java Code for the corresponding activity (screen) for which the layout has the button tag.

So, in the Java code for the corresponding activity, put this function :


public void MyClick(View v)
{
               int id = v.getId();
         
               switch(id)
              {

                           case R.id.bStart :         
                                                          tvResult.setText("Start button clicked!");

                                                          break;

               }

}

As you can see, the function takes in an argument, which is a View class object. So, this function can be called by any of the Views in the XML layouts, even by a TextView. But we are calling this only from a button, using the android:onClick attribute. Now, when the function gets called, the value of v is assigned accordingly, based on which view (that had this function name in it's onClick attribute) was clicked. And then, you can get the view's id using the getId() method, which, if you don't remember, when you type "v." in your Android Studio, it will give a lot of functions as suggestions. And to get data, most times, Java methods have the get word at the start, so type "v.get" and you should see a list of get based methods, to get data. Now, press Ctrl + Q to see info about the functions, when they are highlighted in the suggestions. Finally, click on "getId()", and you will have "v.getId()" finally. Actually, you can use this method of determining methods, for all cases, and this was just an easy example, where getId() is an easy method name to remember, but still, I wanted to explain how to do it, when you don't know what methods exist and what is possible and what ain't. And if Android Studio doesn't show any suggestions on it's own, while typing ( probably due to some problem, or due to low RAM, consequently, very slow response of the editor ), the shortcut to get suggestions is Ctrl + Space.

Finally, you can use a switch statement to check the id of the view. So, if you have many buttons, all can have MyClick as the function name in the android:onClick attribute and when the function is called, you can identify which button called the function, using the id of the button. So, use R.id.xyz to refer to the id. And then in the switch case statements, execute what your logic is, for the appropriate button.

Tips : I recommend you to write the function first, then put it's name in the android:onClick attribute value in the XML layout. This way, when you type in the function's name in the value, it will show suggestions, about your MyClick function. This suggestion is show only when MyClick method is public. So, don;t forget, it MUST be a public method, else the App will crash, ie it will crash, if the method is private and if it's being be called from the layout.

Comparing with old code and feeling better ( Please don't look at this section if you get confused with too much code ) :

Finally, the best part is, you don't have to use code like this :

buttonStart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          Intent i = new Intent(MainActivity.this,SecondActivity.class);
          startActivity(i);
       }
});

where buttonStart is a Button object. And the above code is messy and tough to maintain and debug and change.

And there's another way, which is slightly better, but you have some extra code. Which is where, you implement View.OnClickListener interface and have onClick() method implemented and also make the buttons listen in the onCreate method, using this code (after referencing the button to it's id) :

buttonStart.setOnClickListener(this);

So, if you have many buttons, you have to put the above code many times. Then, finally, you will have the onClick method, in which you will implement stuff that you implemented in MyClick function. Same old thing. Just the "this" thingy code for making the buttons listen, will be extra code over here. And in this case, if you miss the code to make the buttons listen, the onClick method is of no use.


Verdict : So, finally, use the Custom Click function, which can be triggered using android:onClick attribute concept in XML layouts.

1 comment:

Note: Only a member of this blog may post a comment.