Anatomy of an Android Apptutorial android
- Android Studio Projects
- Source Code
- Android Concepts
Now that you’ve built your first Android app, let’s talk about the different components that make up an Android app.
Android Studio Projects
You’re probably using Android Studio to organize the files and code that make up your app, or maybe you’re using another IDE like Eclipse. You can build everything manually using basic text editors and the command line as well, but because of how complicated app projects are, most people use an IDE.
In any case, your Android app is split up into several directories, and you’ll use your IDE to access all of them.
AndroidManifest.xml file contains a bunch of properties that you’ll need to set when you eventually deploy your app to the Play Store or on other phones. This is where stuff like the name of your app and its permissions gets set.
We’ll come back to this file when we talk about deploying your app.
Android apps are written in Java (technically it’s not quite Java, but let’s not worry too much about that), so the source code of Android apps is stored in
.java files, just like you’re already used to.
.java files are stored in whatever package you chose when you created your app project. The entry point of an Android app is the main activity class: by default it’s
MainActivity.java. We’ll talk more about activities in a minute, but for now just know that your code will go in a bunch of
.java files inside your project.
res directory contains non-code files that are needed to run your app. Stuff like images and property files go here.
There are a few subdirectories under the
drawablecontains image files that you use in your code.
layoutcontains layout files. See the Layouts section below for more info.
mipmapcontains image files to use as your app’s icon. We’ll talk more about this when we deploy our app.
valuescontains files that define various properties. For example, the
strings.xmlfile can be used to list the various user-facing messages used in your app, which allows you to translate your app into multiple languages.
We’ll talk more about resources as we need them in the other tutorials, but for now just know that non-code stuff goes here.
Now that we know how our project is laid out, let’s talk about the structure of an Android app.
Views are things like buttons, text fields, and labels. They’re individual components that the user can view and interact with. Views are the basic building blocks that make up your app. You can think of a view as a widget, or a component, or an element, depending on which UI library you’ve used before.
Android views are represented by classes. For example, a button is represented by the
Button class. To create a button, you’d create an instance of the
Our hello world app uses a
TextView to show a label and a
Button view to show a button.
Views are put together into layouts, which decide how the views are shown on screen. A layout decides the placement and size of the views it holds. You can think of a layout as a single screen in your app.
In Anrdoid, layouts are containers that hold views (as opposed to being a property on a container, like in Swing or CSS). For example, the
LinearLayout class represents a layout that dispays views in a single vertical column or horizontal row.
Our example uses a
LinearLayout to position the views in our app. We’ll talk more about layouts in the next tutorial.
The code that runs an Android app is called an activity. An app can be divided into several activities, and there’s usually one activity per screen. An activity is also the entry point (think
main method) of an app.
Activities have a lifecycle, which is a series of events that happen to an activity: stuff like creation, pausing, resuming, and exiting.
Activities are represented by the
Activity class. To create an activity, create a subclass of the
Activity class and then override the lifecycle functions with code you want to run when that event fires.
An activity usually loads a layout in its
onCreate() function and sets up stuff like event listeners and views. For example, our hello world app defines a
MainActivity class that overrides the
onCreate() function, which is called when the app is first run. That code loads our layout and sets up a click listener on the button.
AndroidManifest.xml file tells Android which activity to run when your app is opened.