Introduction to Android Programming | An Ultimate Guide
There are a couple of reasons why you might be reading this;
- You’ve been using the Android OS for quite some time, and you’re probably curious if you can build an app yourself
- You’re a Java programmer, and you read somewhere that one of the things you can build with Java is an Android app
- Someone told you that mobile app developers make a ton of money (not true, by the way. Not for everyone), and you wanna take a stab at it. How hard can it be?
- You’ve been building Android applications with web technologies, and you hit a snag on performance. You wanna go native
Whatever your reasons might be, this article is your baby step towards Android programming.
Brief of Android
Android has been with us for quite a while now. Here’s a quick rundown of its history
- Andy Rubin founded Android Inc in 2003. He’s got Google backing at the time
- In 2005, Google bought Android Inc
- Android was officially given to Opensource in 2007. Google turned it over to Open Handset Alliance
- In 2008, Android 1.0 was released; we didn’t get the dessert names yet
- In 2009, the dessert names started with Cupcake (Android 1.1–1.5), followed by Donut (1.6), and then Éclair (2.0)
- From that point on, Android has seen a steady cadence of releases. The current version of Android, at the time of writing, is Android 10
What you need to get started
You can build Android apps using Linux, macOS, or Windows OS. It isn’t too picky. You’ll also need a capable IDE to build Android apps. It is still possible to build apps without an IDE; you can get by with just the Android SDK + your favorite editor, but I wouldn’t recommend this. Nowadays, you really need a full-featured IDE to build non-trivial apps.
There’s a couple of choices for the IDE; if you’re a fan of Eclipse or NetBeans, you can use those. Most Android devs use Android Studio, though. It’s the de-facto IDE for Android app development. You can get it at https://developer.android.com/studio. Follow the link, choose your OS and then, install it the way you would install any other software in your platform, which usually involves just double-clicking, following the prompts, and accept the defaults.
Android studio states the following as hardware requirements;
- 4GB RAM
- 2GB available disk space
- 1280×800 screen resolution
- Either Intel, AMD, or M1 CPU
Those are the bare minimum. You don’t want that. I can tell you from experience that while the minimum requirements “technically” work, it’s miserable. So, here’s my recommendation;
- RAM — as much as you can afford, 32GB seems to be common among devs now
- Disk space — as much as you can afford. SSD is recommended so you don’t get bogged down by I/O when building your projects
- Screen resolution — Full HD at a minimum. If you can snag a 32 inch UHD screen, that’s much better. You’ll need all the screen space you afford. Android Studio has lots of tool windows
- CPU — At the time of writing, there is no M1 native version of Android Studio. You can use it on an M1 (via Rosetta), but the emulator will be problematic. You’ll have to test on an actual Android device. Intel-based machines seem to be best for Android development (this may change in the future)
You might be wondering why I am not listing software development kit JDK (Java Development Kit) as a pre-req for Android Studio. It’s because you don’t need it anymore. Since Android Studio 2.x, the installer automatically includes OpenJDK (which is what Android Studio uses for compilation). So, it’s really just Android Studio that you need.
Overview of Android
The Android operating system has the following architecture (photo is from https://developer.android.com/guide/platform). Let’s take a closer look.
At the lowest level of the architecture are the Linux kernel and HAL. This part of the Android OS is Linux. It’s a very stable and secure OS. At the most basic level, an OS does the following;
- Acts as a go-between for applications and hardware; it manages the hardware on behalf of the apps
- Provides services to apps like networking, memory management, security, etc.
- Manages the execution of apps
Next up are the libraries like WebKit, OpenGL ES, etc. These are not part of the Linux kernel but are still pretty low-level. They’re mostly written in C/C++. On the same level, you’ll find the ART (Android Runtime); this is the container where Android apps are running.
Next is the Java APIs. You build Android apps by making calls to these libraries; it takes care of marshaling the native libraries and other calls to the rest of the Android OS on your app’s behalf.
Finally, on top is the application layer or the application programming interfaces (system apps). This is where all our apps reside (email, phone app, messaging, video player, etc.)
What makes up an Android app?
An Android app may, at first, seem like a desktop app, but it’s not correct to think of them that way. Android apps are structurally different from desktop apps.
A desktop app generally contains all the routines and subroutines it needs to run. It is self-contained. An Android app is quite different. It’s made of loosely coupled components that are held together by a (uniquely Android) messaging system called Intents.
The diagram below shows a logical representation of an Android app.
It’s made up of components like Activities, Services, BroadcastReceivers, ContentProviders, and Intents.
Activities
This component takes care of user interaction. It’s where you put UI elements and where you capture user-generated events (e.g., click, long-clicks, swipes).
BroadcastReceivers
BroadcastReceivers are components that can listen for events (system or user-generated). If you want to perform a task in response to an event (e.g., Network went down, a phone call, somebody sending an SMS message, etc.), you can use BroadcastReceivers for that.
ContentProviders
ContentProviders lets you write apps that can share data with other apps. A good example of a ContentProvider is the “Contacts” app on Android. It can expose contact data to other apps without exposing the raw data store. It facilitates data sharing via API.
Services
If you need to run code in the background without freezing the user interface, you can use services. You can use these components to perform long-running tasks, e.g., downloading a large file, playing background music, etc.
Intents
Intents are used to activate components and to pass data from one component to another if you need to launch an Activity (a UI component) from another Activity (usually MainActivity) as a response to a user-event (like a button click, you will do that by creating an Intent object and launching it.
Manifest file
The Android Manifest is an XML file that describes the application, all its components, and restrictions (whether or not it can use the network/internet, use GPS, etc.). Don’t worry if you’re not too handy with XML files; this is usually generated when you create a new project. This file is also updated (automatically) as you add components to your project.
A simple project in Android
Now that we have enough background on Android apps let’s build one.
Launch Android Studio if it isn’t opened yet.
I’m using the Canary release (canary 15) for this article. If you downloaded the stable release, your welcome screen might look a bit different than mine.
Click on “New project”.
Choose “Empty Activity” on the project templates (as shown in the picture above); then click “Next”.
Fill in the project details like name, package name, location, language, and minimum SDK. As you can see, I’m using Java for this project (instead of Kotlin), and I’m targetting Android v30. Click “Next” to start creating the project.
It might take a while before you see the screen that follows; Android Studio will pull some files from the repos, and Gradle will sync up; but when Android Studio done doing all that, you’ll see your project opened in the main editor window (as shown below).
On the left-hand side is the Project Tool Window. This is where you’ll see all the files in your project; this is also where you launch them.
At the moment, MainActivity.java and activity_main.xml are open on the main editor window. As you can see, the contents of MainActivity.java are shown.
The Activity component
The project wizard generated one Activity for our project. This is the main Activity. It’s the entry point for the project, meaning, when you launch the app, the MainActivity is the first screen that the user sees.
An Activity isn’t simply an object. It’s a component that’s made up of a Java class (MainActivity.java) and an XML resource file (activity_main.xml).
The XML resource file describes what the screen will look like and how. It contains the definitions of all the widgets you have on your screen. It also contains how these widgets are arranged with respect to the screen and other widgets (constraints).
Switch to activity_main.xml in the main editor to see how our MainActivity looks like right now.
As soon as you click the tab of activity_main, Android Studio’s GUI editor comes into view (as shown above). What you see is Android Studio’s rendering of the UI resource file. If you want to see it in XML view, click the “Code” tab (as shown below).
The following listing shows the contents of activity_main.xml.
As you can see, at the moment, it only has one TextView widget. Let’s remove that and place a button instead.
It’s so much easier to work with the UI in Design mode, so switch over to Design mode again. Then, select the TextView by clicking on it. Now, delete it.
Next, from the Palette window, get a Button widget and place it on our Activity resource (as shown below).
Next, while the Button widget is still selected, click on the “Infer constraints” button on the Layout toolbar (as shown below). The infer constraints button is depicted as a magic wand. If you don’t put a constraint, the Button will be shown on the left-upper most portion of the screen (coordinate 0,0); you need to define a constraint for it to tell the Android runtime how far is it from the top, left, right and bottom edges of the screen.
Next, let’s do something when the user clicks the Button. We can tell the Android runtime what to do when the user clicks the button by setting the onClick property of the button to a name of a method.
While the Button widget is still selected on the UI editor, click the “Attributes” tool window (as shown below), then find the onClick property.
Set the onClick property to “greet” (as shown in the picture above); of course, the greet() method doesn’t exist yet. We still have to create it.
Switch over to “MainActivity.java” or open it from the Project Tool window (if you’ve closed it).
Then, after the onCreate() method, add the following method.
public void greet(View view) {
Toast. makeText(this, “Hello”, Toast. LENGTH_SHORT).show();
}
The greet() method takes in a View object (android.view.View) as parameter; the Android runtime passes the reference of the widget that was clicked to the greet() method.
In the body of the greet() method, we’re simply displaying a Toast message. A Toast message is a small message displayed on the screen. It’s similar to a tooltip or other popup notification. It’ll show for a couple of seconds, then fade away.
Testing the app
Our next step is to run and test the app. You can connect an Android device to your PC and run the app there, or you can use the emulator.
Before you can use the emulator, you need to install it first. On the main menu bar of Android Studio, click “Tools”, then choose “AVD Manager”. AVD is short for Android Virtual Device.
In the screen that follows, click “+ Create Virtual Device”.
Choose a form factor from the list of device definitions (shown above); then, click “Next.”
Select a system image. I’ve previously downloaded API level 30 already (as you can see). So, I can select it already.
Give your new AVD a name. You can tweak the settings a bit more (by clicking the “Show Advanced Settings” button), but I usually accept the defaults. You can edit these settings at a later time (if you truly need to mess around with it). Finally, click “Finish”.
To start the emulator, click the green arrow on the “Actions” column of the AVD Manager (shown below).
In the screen that follows, you’ll see the Android emulator in action.
Next, go back to Android Studio and run the app. You can run the app in two ways;
- From Android Studio’s main menu bar, click “Run” > “Run App”; or
- Click the green arrow pointing to the right (as shown below)
Gradle will sync, and Android Studio prepares a build. When that’s done, Android Studio will look either for a connected Android device or a running emulator. Since we already launched an emulator earlier, Android Studio will deploy the app in this emulator.
The picture above shows our small project in all its simplicity and splendor.
Acodez is a renowned web design and Mobile app development company in India. We offer all kinds of web application development services to our clients using the latest technologies. We are also a leading digital marketing company providing SEO, SMM, SEM, Inbound marketing services, etc at affordable prices. For further information, please contact us.
Originally published at https://acodez.in on May 17, 2021.