Android apps are programmed in Java using a particular set of APIs and libraries. IntelliJ IDEA & Android Studio are the preferred IDEs for Android development. In this tutorial, you will use IntelliJ IDEA to implement “Hello World” (or a reasonable facsimile) as an Android app.

Part 1: Creating the app

Video

Instructions

  1. Start a new project in IntelliJ IDEA and select Android as the project type from the left sidebar.

  2. For this tutorial, select the Navigation Drawer Activity as the main (and only) activity of this app. (An Android activity is a display screen.)

  3. Click the Next button.

  4. For the Name of the project (which will be displayed name of the app, by default), type “Android Hello World”. Note that a lower-case form of this same name, without spaces, becomes the last part of the Package name.

  5. The name of the project also provides the default name of the Save location, where the project files will be located. Make sure this is a subdirectory of the bootcamp/projects directory that you set up during the environment preparation portion of the pre-work. Important: After verifying that, change the name of the final subdirectory in the path shown, to “hello-world-android”; if the final subdirectory shown is projects, then add “/hello-world-android” at the end. In this bootcamp, we’ll follow a strict rule of using lower-case project directory names, with dashes between the words; this is sometimes called “spinal-case”. (This is not required by IntelliJ, the Android Studio plug-in, Git, GitHub, or the Java compiler; however, each development organization has its own set of conventions, and this is one we’ve adopted in the Deep Dive Coding Java + Android organization.)

  6. Make sure the selected Language is “Java”. We’ll explore Kotlin a bit in this bootcamp, but our primary focus, is (of course) Java.

  7. Set the Minimum API level to “API 26: Android 8.0 (Oreo)”. While this is by no means the latest version of Android, it’s a reasonable trade-off between device coverage and functionality.

  8. Do not select the Use legacy android.support libraries checkbox.

  9. Click Finish. This creates the project and opens it in IntelliJ.

  10. If you’ve already set up an emulator, skip to part 3.

Part 2: Creating an emulator

Video

Instructions

  1. To set up an Android emulator, first select the Tools/Android/AVD Manager menu option.

  2. Click the Create virtual device… button.

  3. Select one of the emulator models with the Google Play icon in the Play Store column. (If this is the first emulator you’re setting up, we recommend using the Pixel 3 or Pixel 3a model.)

  4. Click Next.

  5. If you’re running an Intel or AMD CPU, click on the x86 Images tab; otherwise, click on the Other Images tab.

  6. Select an Android/API release from the list. To be able to run our app, the API level selected for the emulator must be greater than or equal to the minimum API level used for the project. On the other hand, selecting an API level corresponding to an Android version that is still very new comes with certain risks. For this emulator, select the “Pie” (API level 28) or “Q” (API level 29) image; in either case, look for an option that doesn’t have the “Download” link, since you already downloaded images when you ran the Android SDK setup script.

  7. Click Next.

  8. Specify the Android virtual device (AVD) name. We recommend using the default name, which includes the emulator model and the API level.

  9. Click Finish to create the emulator.

  10. Since this will be the first time running this emulator, start-up time is significant; this can interfere with IntelliJ’s ability to start the app on the emulator. To mitigate this, click the small “play” triangle in the Actions column. This will start up the emulator. (Note: This initial startup takes some time; subsequent startups of the same emulator will be much faster.)

  11. After the emulator is started, switch back to IntelliJ.

Part 3: Modifying the app

Video

Note for OS X: The OS X version of IntelliJ IDEA 2021.2.2, released on 13 September, 2021, includes a fix to a long-standing bug in the Android Studio plugin. OS X users who are using IntelliJ IDEA 2021.2.2 should disregard the section of the following video starting at 00:30 and ending at 09:30; this portion of the video corresponds to steps 9 through 12 of the instructions, which should be disregarded as well by OS X users of IntelliJ IDEA 2021.2.2.

Instructions

Important: The OS X version of IntelliJ IDEA 2021.2.2, released on 13 September, 2021, includes a fix to a long-standing bug in the Android Studio plugin. OS X users who are using IntelliJ IDEA 2021.2.2 should disregard steps 9 through 12 of the instructions.

  1. Return to IntelliJ.

  2. From the pull-down menu above the Project tool window in the upper-left-hand corner, select Android. This displays the Android view of the project tool window; this organizes (for display) the most relevant source components of the project in a relatively “flat” fashion.

  3. From the Android view of the Project tool window, expand the Gradle Scripts portion of the tree structure.

  4. Open the second of the build.gradle files listed; this is the file with “Module” (and some additional text) in parentheses after it.

  5. Around line 7, you should see buildToolsVersion 31.0.0. Unfortunately, as of this writing, that version of the build tools doesn’t download and install cleanly; fortunately, commenting this line out will instruct IntelliJ to use the build tools version that corresponds with the compile SDK version (30).

  6. Click on the buildToolsVersion line, so that the insertion point is in that line.

  7. Use the Ctrl+/ (on Windows or Linux) or +/ (OS X) keyboard shortcut to comment this line out. Note that this simply places two forward slash characters (//) at the start of the line; this starts a single-line comment in Java, as well as in Groovy (the scripting language used in the build.gradle file you’re editing).

  8. At the top of the editing area, you’ll see a colored line indicating that Gradle changes have been made; to incorporate those changes into the project, click the “Sync Now” link in that same line.

  9. From the Android view of the Project tool window, expand the app/res/layout portion of the tree structure.

  10. Open the activity_main.xml file.

  11. Notice that lines 12–24 (approximately) include the following XML:

    <com.google.android.material.navigation.NavigationView
      android:id="@+id/nav_view"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:fitsSystemWindows="true"
      app:headerLayout="@layout/nav_header_main"
      app:menu="@menu/activity_main_drawer"/>
    
    <include
      layout="@layout/app_bar_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
    

    The order in which the NavigationView element and include element appear is actually incorrect, and is causing clicks/presses on items in the navigation drawer not to be handled correctly.

    Rearrange lines 12–24 so that the order of elements is reversed:

    <include
      layout="@layout/app_bar_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"/>
    
    <com.google.android.material.navigation.NavigationView
      android:id="@+id/nav_view"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:fitsSystemWindows="true"
      app:headerLayout="@layout/nav_header_main"
      app:menu="@menu/activity_main_drawer"/>
    
  12. Run the app, and experiment with clicking/pressing on the items in the navigator drawer; what do you notice?

  13. From the Android view of the Project tool window, expand the app/res/menu portion of the tree structure.

  14. Select the activity_main_drawer.xml file and open it, by double-clicking, or typing F4, or typing Ctrl-Enter (Windows and Linux) or Cmd-Down (OS X).

  15. Note that there are 3 item elements in this XML file; each of them corresponds to a menu item that appears in the navigation drawer of the app.

  16. Note that each of these item elements has an android:title attribute. If you click on any one of these, you will notice that it is actually a reference to another resource, a string resource in this case, where the actual text is defined.

  17. Open the strings.xml file, located in the app/res/values portion of the Android view of the Project tool window.

  18. Near the bottom of the file, you’ll see these 3 XML elements:

    <string name="menu_home">Home</string>
    <string name="menu_gallery">Gallery</string>
    <string name="menu_slideshow">Slideshow</string>
    

    Change the 3 lines so that they read as follows:

    <string name="menu_home">My home base</string>
    <string name="menu_gallery">Stuff I\'m working on</string>
    <string name="menu_slideshow">Friends &amp; family</string>
    
  19. Try running the app again. What happens?

  20. If you have not yet done so, share your project on GitHub, using the VCS/Share Project on GitHub menu command. When prompted, name the GitHub repository “hello-world-android”. (If you already did this earlier in the process, then commit and push your changes now, using the Git/Commit menu command.)

Part 4: Disqus post

Go to the comments section at the bottom of this page, and answer the following questions:

  • What is the URL of the GitHub repository you created?
  • If this activity left you with significant questions, what are they?