Today 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
The 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.
Source Code
Android apps are written in Java, so the source code of Android apps is stored in .java files, just like you’re already used to.
The .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.
Resources
The 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 sub-directories under the res folder:
- drawable contains image files that you use in your code.
- layout contains layout files. See the Layouts section below for more info.
mipmap
contains image files to use as your app’s icon. We’ll talk more about this when we deploy our app.values
contains files that define various properties. For example, the strings.xml file 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.
Android Concepts
Now that we know how our project is laid out, let’s talk about the structure of an Android app.
Views
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 Button
class.
Our hello world app uses a TextView
to show a label and a Button
view to show a button.
Layouts
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 Android, layouts are containers that hold views. For example, the LinearLayout
class represents a layout that displays 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.
xxx
Activities
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. Activity is also the entry point 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 to run. That code loads our layout and sets up a click listener on the button.
The AndroidManifest.xml
the file tells Android which activity to run when your app is opened.