How to create and handle button in Android Studio

To get started with Android we must know how to play with buttons, text fields(TextEdit), labels, etc. and you must know, how to fix common problems in Android Studio.

In this example, we will learn how to create a button and its handler.

  1. Open Android Studio and create a new Project.
  2. Android Studio panel will open. In the main panel, you will see java file MainActivity.java this is the java file of the activity which you created at the time of creation of Project.
  3. Now you need to open up a few files you will need.
    a.  XML layout file of your activity. i.e. activity_main.xml (placed in app -> res(resources folder) -> layout -> activity_main.xml)

    activity_main.xml file location
    activity_main.xml file location

    Note: You may see rendering problems. This is due to missing SDK(Software Development Kit) for the target Android version. SDK version tells the IDE, how to render layouts. To fix the rendering problems update SDK from Tools -> Android ->SDK Manager.

    Open SDK Manager Android Studio
    Open SDK Manager Android Studio

    Android Studio SDK Manager will show installed and available SDK versions.

    SDK Panel Android Studio
    SDK Panel Android Studio

    Update for the required target SDK. By checking the required SDK and click OK. It will download the checked SDK versions. (download may be slow as because of large file size also depends on your internet connection, so check only the required SDK).
    Now change SDK version from the icon from the layout in activity_main.xml.

    change SDK version Android Studio
    Change SDK version Android Studio

    b.  strings file of your project i.e. strings.xml (placed in app->res->values->strings.xml) in which all the variables of your interface are placed.

    strings.xml in Android Studio
    strings.xml in Android Studio
  4. In activity_main.xml open Design view from the bottom of the panel. Drag and drop Button from the Palette.

    Place NewButton in Android Activity
    Place NewButton in Android Activity
  5. Open strings.xml create a new string for button name.

    create a new string in strings
    create a new string in strings
  6. Open text of activity_main.xml from the bottom of activity_main.xml panel.
    open text view of activity_main
    Open text view of activity_main

    Change the android:text value in inverted commas with @strings(reference to strings.xml file)/button_name(variable name in strings.xml).

    change text of button in activity_main
    change text of button in activity_main

    Change android: id value in inverted commas with the new id with “@+id/click_me_button” (@+id add up a new id for the Palette items) to handle button in Android Studio.

    change id of button in activity_main
    Change id of the button in activity_main

    you can also change it in design view by double-clicking the button and changing the value of id.

    change button id Android
    Change button id Android
  7. Add button click property in activity_main.xml in <Button /> tag.

    add button onclick in Android Studio Activity
    add button onClick in Android Studio Activity
  8. In your java file i.e. MainActivity.java implement the method for click_me_button and pass instance reference of the View.

    implement button onclick method
    Implement button onclick method

    An error will show up you need to import the View class. Pressing Alt+Enter will show up suggested options, or it will automatically import the View class. You can manually import the view class by placing the import code outside the class.

  9. Create a Toast in implemented method body.

    Import toast class to make it work.

    Toast: Toast is to show the user a little message on any action. For more detail click here.

    handle button in android studio
    handle button in android studio
  10. Run app using AVD(android virtual device), many other alternates are also available like genymotion, blue stacks, etc. I prefer genymotion based on its performance.
    run button app
    Run button app
    button app running
    button app running

    Download the project zip file here.
    Alternate download here.

    Download apk file here.
    Alternate apk here.

Leave a Reply

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