Brief us your requirements below, and let's connect
1101 - 11th Floor
JMD Megapolis, Sector-48
Gurgaon, Delhi NCR - India
1st floor, Urmi Corporate Park
Solaris (D) Opp. L&T Gate No.6
Powai, Mumbai- 400072
#12, 100 Feet Road
Banaswadi,
Bangalore 5600432
UL CyberPark (SEZ)
Nellikode (PO)
Kerala, India - 673 016.
Westhill, Kozhikode
Kerala - 673005
India
There are a couple of reasons why you might be reading this;
Whatever your reasons might be, this article is your baby step towards Android programming.
Table of Contents
Android has been with us for quite a while now. Here’s a quick rundown of its history
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;
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;
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.
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;
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.)
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.
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 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 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.
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 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.
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.
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 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.
<?xml version=”1.0″ encoding=”utf-8″?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Hello World!”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” />
</androidx.constraintlayout.widget.ConstraintLayout>
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.
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;
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.
Contact us and we'll give you a preliminary free consultation
on the web & mobile strategy that'd suit your needs best.
Gamification in Mobile Apps: Engaging Users and Driving User Retention
Posted on Jul 15, 2024 | Mobile ApplicationFuture of Mobile App Development with Open AI: Key Insights
Posted on Feb 08, 2024 | Mobile ApplicationWhat is App Localization? Why it’s Essential for Mobile App Success
Posted on Jan 16, 2024 | Mobile Application
This was a very meaningful post, so informative and encouraging information, Thank you for this post.
online food ordering app development