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 now 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 is supported by Google as the preferred 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.
Fill out the fields on the Configure your new project dialog:
- Application name - Hello World (as seen in the Google Play store and on the device)
- Company Domain - example.com (or 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 Select the form factors you app will run on 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 may mean not using all the features of the Android Software Development Kit (SDK). The Google Play store no longer supports devices earlier than API 9 (Android 2.3 Gingerbread) thus this is the minimum recommend API to select.
Again select Next. On the Add an activity to Mobile dialog use the Empty Activity and press Next.
On the Customize the Activity screen use the default settings:
- Activity Name: MainActivity
- Generate Layout File: checked
- Layout Name: activity_main
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 go into the Project explorer and open the activity_main.xml (in the layout folder under res in the src/main folder under app). 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 (click on the Project tab at the left) and its files opened. 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. Android 2.3.3 with HGVA). If more than one AVD is configured then you may need to choose which one to start. 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 the 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
- For another basic Android App view the SkeletonApp Android Sample. Found in the legacy folder for an Android API in the samples directory where the Android SDK is installed.
- Try our tutorial Start a Second Android Activity from the First.
- The code is available in a zip file, HelloWorld.zip, with an instructions.txt on how to import the project into Studio.
Author:Daniel S. Fowler Published: Updated: