Your First Android Java Program - Hello, World!
Getting a computer program to display Hello, World! is traditionally the first program written to test a system. If you are new to writing Java programs for Android and have just installed Android Studio then why not start by writing a Hello, World! application, or app as it is commonly known.
A Tutorial on Building an Android Hello World App
This tutorial walks through a basic Android Hello, World! app. This is done in the Android Studio Integrated Development Environment (IDE) with the Java programming language. Android Studio and Java are supported by Google as a development environment for producing apps. When an Android app project is started in Studio it gives you the basic Hello World program as a starting point. This tutorial runs through making that first basic Android Hello World app. The article assumes that you have set up and configured Android Studio on your computer ready for development. To do this for a Windows computer see our article Android Studio Windows Install for PCs.
When Android Studio loads select Start a new Android Studio project (or use New then New Project from the File menu if an existing project is open).
Fill out the fields on the Create Android Project dialog:
- Application name - Hello World (the default name seen in the Google Play store and on the device)
- Company Domain - example.com (enter you own domain if you have one)
- Project Location - accept the default or chose a location
The Importance of Package Names
Here the Package Name will default to com.example.helloworld. The Company Domain and the Package Name are important. Especially if you intend to publise apps on the Play store, other app stores or the Internet. It serves as a method to uniquely identify an app publisher and the different apps from a publisher. In this tutorial example.com is used for the domain name but if you have you own domain or web address use it.
The Package Name is very important, it is the under the covers identifier for you app, e.g. com.example.mycoolapp. Each app must have its own unique identifier to allow Google Play and all Android devices to distinguish your application from others. The recommended way is to use a web address in reverse (without the http and www parts) followed by an identification string (related to the application). Use your company's web site address or a personal blog address. If the web site was www.example.com then the Package Name would be com.example.helloworld. Thus com.example becomes a Namespace for all your apps and will be the first part for all your apps' Package Names. With the last part referencing a particular app. If your blog was example.wordpress.com then the Package Name could be com.wordpress.example.helloworld. Remember do not use com.example, reverse your own domain. E.g. sample programs on this website use uk.tekeye. Organisations producing multiple apps must keep track of the names used to prevent duplication of Package Names.
Complete the Create New Project Wizard
Click Next and on the Target Android Devices dialog. Leave the settings as default, the app will be for a Phone and Tablet. The Minimun SDK Application Programming Interface (API) can be changed if required. Newer APIs have more features but run on fewer devices. Older APIs support a wider set of devices but not all the features of the latest Android Software Development Kit (SDK). The Google Play store no longer supports devices earlier than API 9 (Android 2.3 Gingerbread), and some Android API features only work on Android Ice Cream Sandwich, API 14, and later. Plus the Studio SDK may issue warning messages for some features on earlier APIs. As a general rule the minimum recommend API to select should match the minimum API supported by Google's advertising AdMob code, at the time of this article update the minimum AdMob requirements are Android API 14 (Ice Cream Sandwich, version 4.0.1).
Again select Next. On the Add an activity to Mobile dialog use the Empty Activity and press Next.
On the Configure Activity screen use the default settings:
- Activity Name: MainActivity
- Generate Layout File: checked
- Layout Name: activity_main
- Backwards Compatibility (AppCompat): checked
Select Finish. Android Studio will prepare the project. This may take some time, especially if Android Studio is run for the first time. Check the status bar at the bottom of the Android Studio IDE when it appears.
When an app is created the first screen is normally displayed by Studio.
If not click on the activity_main.xml tab or go into the Project explorer and open the activity_main.xml (in the layout folder under res in the app project tree). If necessary click on the Design tab at the bottom of the editing area.
The Android Studio IDE is divided into several areas. A menu bar and toolbar at the very top, a status bar at the very bottom. Access to various explorers on the far left, a tabbed central editing area, tabs for various utilities below the editing area, and various tabs for assorted tools to the right. Most icons have tool tips so run the mouse around the IDE to see the functions of the various visible items.
Rendering Problems Error
Occasionally you may see an error message:
Rendering Problems Missing styles. Is the theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style references.
The Studio cache files may need refreshing from the File menu select Invalidate Caches / Restart. Click the Invalidate and Restart button on the message that appears.
Run the Hello World App
Use the tab at the bottom of the edit area to switch between the Design (graphical) and the Text (code) views. The Hello World project can be explored by clicking on the Project tab at the left, exploring the tree and opening the files. The Java code is under the java folder (see Android Project Structure). Tip: Click on the project tree and start typing to search.
The app is ready to be executed. It can run on an Android device plugged into the computer if the drivers are present and USB debugging is enabled via the enable Android developer mode options, or on an Android Virtual Device (AVD). See the article Set Up an Android Virtual Device for App Testing.
To run the Android Hello World app press the run toolbar button or select Run 'app' from the Run menu.
Choose (or create) the device to run the app in the Select Deployment Target dialog. If an AVD needs to load it can take some time as Android may need to boot the AVD. If the AVD is running very slowly trying using a Intel x86 Atom virtualized emulator (HAXM is required) or a lower level API and smaller screen (e.g. a Nexus One). If more than one AVD is configured then you may need to choose which one to start. For tips on using the keyboard with the AVD see Android Emulator Key Mapping. If an Android device is plugged into the computer and configured for USB debugging that can be used to run the app.
Once your first Android Java programming is running try adding functionality. Investigate Studio's features and become familiar with the IDE.
- Installing Java on Windows
- Android Studio Install for Windows
- Set Up an Android Virtual Device for App Testing
- Android Project Structure
- Try our tutorial Start a Second Android Activity from the First.
- Intel Hardware Accelerated Execution Manager, HAXM
- Android Emulator Key Mapping
- The code is available in a zip file, HelloWorld.zip, with an instructions.txt on how to import the project into Studio.
- See the other Android Studio example projects to learn Android app programming.
- For a full list of the articles on Tek Eye see the full site Index
Author:Daniel S. Fowler Published: Updated: