Adding Code to a Button Click in Android Studio


The entered code (so you can copy and paste):
public void buttonOnClick(View v) {
// do something when the button is clicked
Button button=(Button) v;
Then I typed v.settext and hit Tab to autocomplete the line ((Button) v).setText(“clicked”);

See supplemental information at

Read more all post Android Technology :
  1. Heiko Simmchen says

    i wish some one would do a simple step by step procedure to add a button and add functionality. not worriying about colours, sizes… simple functionality so beginners can learn.

  2. Kyron Mayhew says

    thanks, super easy to follow unlike a lot of information on android studio

  3. Tony Liu says

    This is so cool man! Will look forward for more video from you great line by-the-way ! =pOh by the way, is it ok if I can also seek your advice in this open source android app I have posted below? Just need some feedback about it… www . bit . ly/2RX5NpcSo sorry about the url having spaces in between because Youtube don't allow me to place url or anything related to Google Play url.. they are somehow hidden by Youtube… As a result i created this shortened url to prevent this issue. Will appreciate if you could remove the spaces to access this url of google play store. thank you !

  4. Vladimír Vozár says

    the voice crack at the beginning always cracks me up, lovely video, thanks

  5. OllegN says

    you dont need the cast on the second line if you use the converted object on the first line lol

  6. Fabian Reschke says

    thanks dude it´s finally working 😀

  7. Broockle says

    Was trying for over an hour to figure out why it wasn't working right. It kept putting another activity before this one for some reason even though I went into the Manifest and changed the order. Now I figured that somewhere while copying your code an ")" slipped in there somewhere and that made the whole script invalid and so couldn't be summoned so I guess it pulled the other Activity instead. Such a little mistake can cause so much brain damage

  8. Jordan Ash says

    Good video!

  9. Robert Kirk says

    Mate, could you explain how you add the code behind into Activity view? You say you drap the button and move where?

  10. Merde says

    How do I use this method to change the activity?

  11. Miri No says

    doesnt work

  12. Lizard King says

    Is there a way in Android studio to automatically add this method for you? In your video, it suddenly appeared. Is there a shortcut/key combination that will do that?

  13. Park Tesla says

    Thank you

  14. Daniel Claudio says

    I have a similar problem that +TheNoobBigCrap had, literally the same exact error . Here's my code: public void buttonOnClick (View v) {// does something when button is tapped Button button = (Button) v ; ((Button) v).setText("Your order has been sent! Expect a phone call within the hour.") ;

  15. Flax says

    HiI really need help to make my app! i have 16.1K followers on twitter i will do you a shout out if you help.My twitter Is: @_MCPECommunityPlease help.ThanksMCPECommunity

  16. Jarred Daniels says

    why is v red in mine?

  17. Balani says

    How come when in the line getMenuInflater().inflate(, menu);the menu is red?

  18. Konrad Welenc says

    Hi mate. I use in my app button and "oneclick" Now i need to use "press and hold button" (press finger on button and hold 1s) to share on facebook or other app and i dont know how do it. Can u help me?

  19. Lex says

    @Prof. Scott Vanselow – How did you drag the button over to the tab, as drag is not working for me on my Mac?

  20. Baron Boyscout says

    I did it! Thank you Sir.

  21. AfonsodelCB says

    I dont really expect an answer, but how does "(Button) v" direct to the button in the activity? what if I have 2 buttons?

  22. Tyler McKee says

    Someone explain step 2? I see he's created a new Button 'button' but I dont understand what he is setting it to.

  23. Typical user says

    after this code "Expected receiver of type com.example.familyapp.MyActivity, but got android.view.ContextThemeWrapper"

  24. KreepyKayy says

    Hey guys anyone know how I can reduce the area of a clickable button, basically I have an irregular shape and an onclicklistener for it however when I click the transparent regions the click is picked up.

  25. K Badsha says

    Unfortunate to see so many dislikes just because people don't understand Java

  26. Khaleid Radwi says

    hey mate how r u ,, i wanna ask u how can o change the name of button when i open nexus phone .. sorry for bothering ^_^ and thanks <3

  27. acalarmarrrr says

    ughh, Java…. UGHHH

  28. Kevin Willbur says

    Trying to make a button that will dial a number. I have this Intent callIntent = new Intent(Intent.ACTION_DIAL); callIntent.setData(Uri.parse("tel:123456789")); startActivity(callIntent);But I dont know where to put it. I click the button, and then it just pulls up the phone dialer and the number is entered

  29. Chris Wood says

    Thanks!!!! A Great help.

  30. Christian Gentry says


  31. Jordy gaat je niks aan says

    what kind of programminglanguage does it use i dont get it java html javascript what does it use PLEASE TELL !!!! thanks and does anybody nows a good book about it !!!!!

  32. Flor2Easy says

    Help!!I cant get that where it is onClick at 1:02!

  33. André Verwijs says

    My code:  (with multiple buttons)     public void buttonOnclick1(View v) {        Button button=(Button) v;        ((Button) v).setText("Clicked");               }    public void buttonOnclick2(View v) {     …..    }

  34. Diablo tristian says

    the code writing is very quick and is not following the explanation time

  35. Ezra Hulleman says

    +Prof. Scott Vanselow , how can put a internet link behind a button?

  36. Dan Chrostowski says

    Why did you declare and instantiate the button variable, then do nothing with it?  It kind of confused me.  Maybe you should have written button.setText("clicked") ?

Leave A Reply

Your email address will not be published.