How to Get Started With Android Development – Basic Structure
In this post I’m going to tell you a bit about the basic structure of an Android application.
Join the DZone community and get the full member experience.Join For Free
In a previous article, I wrote about how you can launch your first Android application from the samples provided with Android Studio. Now that you’ve had a chance to poke around a few samples, I’m going to tell you a bit about the basic structure of an Android application.
A good place to start understanding an application is the manifest. The AndroidManifest.xml file links all the parts of an Android application together, including:
- The activities contained in your application,
- The theme you use,
- The name of the application and its icon on the main screen,
- The permission the application must request to run.
Activities and the Java Folder
The Java code of your application lives in the java folder. The first class you’ll create is an Activity class, which is the main building block of all Android applications. An activity is a full-screen window in which all your code will run. Each activity has it's own life cycle which starts when it’s first launched and last until it is finally destroyed.
The user navigates between different activities in your application: the back button of the device closes the current activity by default and goes back to the previous one. If the user closes the first activity of your application, he’ll go back to the application that was previously open, if any. You can also launch other applications (with their own activities) using intents. You can use this to request an application that can send an email or play music, and the installed applications that can manage this will be displayed to the user.
The activity lifecycle can be affected by events outside your control: for example, if a phone call comes in, your current activity (so your application) will be paused. Each activity has a layout associated with it that describes how controls that are displayed, and the activities are declared in the AndroidManifest.xml file.
Unfortunately, the fact that the activity is bound pretty strongly to the navigation AND to the user interface/layout makes it hard to separate the business logic and the display logic properly. You can create a usable Android application with only a .java file with an Activity class, but eventually, you’ll want to move on to a better architecture. There are some patterns out there that can help you do this, but for your first application you should stick with this and avoid introducing additional complexity.
Resources are everything that your application needs that is not Java code, such as layouts, strings, images and other constants. The Android API uses XML for most resources. The res folder contains all the resources of your application, such as:
- XML layouts for each activity and component in the layout folder, styles in the values\styles.xml and images in the drawable folders. Layouts in Android are inspired by HTML/CSS, so if you’ve ever done some web development you’ll feel right at home pretty soon.
- All the strings used in your application in values\strings.xml. You can hardcode them in the layouts, but if you ever need to translate your application you’ll be glad you put them all in one place to get started with.
- Menus for your application are defined in the menu folder.
You can now understand all the basic parts of an Android part, and start creating your own application!
Published at DZone with permission of Cindy Potvin, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.