What First-Time Android Developers Really Need To know.

Android History

Android is a mobile operating system that began in November 5, 2007 with the public release of the Android beta.

Android is continually developed by Google and the Open Handset Alliance.

Android code names are confectionery-themed and have been in alphabetical order since 2009's Android 1.5 Cupcake. with the most recent major version being Android 8.1 Oreo, released in December 2017.

Android is not just For Phones and Tablets

After we knew the Android History, it’s time to know how android has been used.

Your phone runs Android, your tablet runs Android… your refrigerator runs Android? your Oven runs Android? your Washer runs android? your Toilet runs android?

Let’s discover if it is possible or not.

  • For refrigerator, Samsung actually built a refrigerator called the RF4289HARS with an 8″ LCD display that runs Android. You know, in case you want to tweet from your fridge.
  • For Oven, who could resist Dacor’s Discovery IQ oven? It boasts a 1GHz Samsung processor, 512MB of RAM, Wi-Fi, and a 7-inch LCD. The Discovery IQ Controller is your cooking app and guide which can be accessed on any Android tablet or smartphone on the same Wi-Fi network.
  • For Washer, Samsung is determined to invade your kitchen with Android compatible devices and it offers a washer and dryer range that can be remote controlled by your Android smartphone or tablet. The appliances themselves don’t really run Android, but they do have 8-inch touchscreen LCDs and you can control them via an Android app.
  • For Toilet, The Satis Smart Toliet doesn’t come with an Android device — nor do most appliances that run on Android — but downloading the accompanying mobile allows you to adjust the height of the toilet seat and activate the bidet from your smartphone.

What we will do?

After we knew the history of Android and How it has been used not just for Phone or Tablet. Let’s dive and make our first app.

we go through the steps needed to build your very first Android app. But before we start, it is worth mentioning some of the other resources we have related to writing Android apps.

If you want to develop Android apps, step one is picking a language.

You’ll find a short break down of each language option here. Pick the one that looks interesting to you and then jump to it.

The languages you might consider learning for Android development include:

  • Java – Java is the official language of Android development and is supported by Android Studio. It has a steep learning curve however.
  • Kotlin – Kotlin was recently introduced as a secondary “official” Java language. It is similar to Java in many ways but is a little easier to get your head around.
  • C/C++ — Android Studio also supports C++ with the use of the Java NDK. This allows for native coding applications, which can be handy for things like games. C++ is more complicated though.
  • C# — C# is a slightly more beginner-friendly alternative to C or C++ that obfuscates more code. It’s supported by some very handy tools like Unity and Xamarin which are great for game development and for cross-platform development.
  • BASIC – A bonus option is to learn BASIC and try the B4A IDE from Anywhere Software. This is an easy but powerful tool, though definitely much more niche!
  • Corona/LUA – Another cross-platform tool build on LUA. It massively simplifies the app-building process and allows you to call native libraries.
  • PhoneGap (HTML, CSS, JavaScript) – If you already know how to build interactive web pages, then you can use this knowledge with PhoneGap to build a more basic cross-platform app.

We will use the first and most popular option Java

Android Studio

To write an app you are going to need to download and install Android Studio. Included in the download are the Software Development Kit, with all the Android libraries and bits that you need to develop an app; and the Android emulator, so that you can initially test your app on your PC without needing to install it on a real device.

Note: It is preferable to use Linux OS instead of Windows. This would change in the speed of the building and running of the application.

However, first you are going to need to download and install the Java Development Kit (JDK) from Oracle. Look for the section called “Java SE Development Kit 7u79” and download the version for your PC. It is important that you download the right version (32-bit or 64-bit) because if you don’t you can have problems with Android Studio not finding the Java Runtime Environment (JRE).

Note: Oracle will not be posting any updates of Java SE 7 to its public download sites and it is suggested that users move to Java 10.

When you first run Android Studio it will perform some initialization including downloading and installing the latest Android SDK. This can take several minutes, you will just need to be patient.

When everything has been downloaded (and whenever you subsequently start Android Studio) you will see a menu which allows you to start a new project, open an existing project, import a project, and so on.

Android - Application Components

Before we go through to start new project, we should know the Application Components.

Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file AndroidManifest.xml that describes each component of the application and how they interact.

There are following four main components that can be used within an Android application: - 1. Activities An activity represents a single screen with a user interface, in-short Activity performs actions on the screen. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched. An activity is implemented as a subclass of Activity class as follows

~~~java
public class MainActivity extends Activity {
}
~~~
  1. Services

    • A service is a component that runs in the background to perform long-running operations. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.
    • A service is implemented as a subclass of Service class as follows
    public class MyService extends Service {
    }
    
  2. Broadcast Receivers

    • Broadcast Receivers simply respond to broadcast messages from other applications or from the system. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.
    • A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each message is broadcaster as an Intent object.
    public class MyReceiver  extends  BroadcastReceiver {
        public void onReceive(context,intent){}
    }
    
  3. Content Providers

    • A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. The data may be stored in the file system, the database or somewhere else entirely.
    • A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.
    public class MyContentProvider extends  ContentProvider {
        public void onCreate(){}
    }
    

Start a new project

After we knew Application Components, let's write your first Android App.

Click “Start a new Android Studio project” and enter a name for your app in the “Application name:” field, I would suggest something like “My First App” (without the quotes). In the “Company Domain” field enter the domain name of your company. If you are an independent developer or a hobbyist, enter your domain name. If you are just experimenting with Android and won’t be publishing your apps on Google Play anytime soon, then just leave the domain as it is, just change “user” to your name (without any spaces).

start_project_1.PNG

start_project_2.PNG

On the next dialog make sure “Phone and Tablet” is selected and that the “Minimum SDK” is set to API 15: Android 4.0.3. Make sure that “Wear” and “TV” are not checked.

start_project_3.PNG

On the “Add an activity to Mobile” dialog, use the default of “Empty Activity” and click “Next.”

start_project_4.PNG

On the “Customize the Activity” dialog uncheck Backward Compatibility (AppCompat) and click “Finish.”

Note: You would see that the layout is reverse of the activity name. It is a convention naming for layouts in Android.

start_project_5.PNG

The Integrated Development Environment (IDE) will now start. This can take several minutes (especially if it is the first time you have created a project).

At this point it is possible to compile and run the auto-generated app, but it isn’t very interesting and you won’t learn anything about writing an Android app. So instead we are going to add a few little things, not much, but enough to get you started and give you a taste of Android app development!

The project tree

tree_1.PNG

Under the “app” node in the project tree you will see several nodes (like folders) which can be expanded. The top level nodes are “manifests”, “java”, and “res”. The last one is short for “resources.”

Under manifests you will find the file “AndroidManifest.xml,” every application must have one of these. It is an XML file with information about the app including its name. One of the most common things you will add to this file is the list of permissions needed by the app. For this simple test app, you won’t need to change anything here.

Under “java” you will find the Java code for the app. It will be under a sub folder called something like “com.example.jun.myfirstapp”, which is the reverse of the company domain name you entered earlier, plus the name of the app. Under that folder you will find MainActivity.java. This is the entry point into your app and for our app this is the only Java file that we will need.

Under “res” there are several different folders for graphics, menus, and the UI. The two that interest us for this app are “layout” and “values.” Under “layout” is a file called “activity_main.xml.” It is an XML file that describes the user interface. There are two ways to edit this file. The first is to edit the XML code directly, or the second is to use the built-in UI designer.

The “values” folder contains several different XML files, the most important one for this app is “strings.xml.” Rather than hard coding string values into the Java code, the values are placed into the “strings.xml” file and then referenced using an ID. The advantage of this system is that if a string is used multiple times it can be changed in just once place. It also makes it easier to support multiple languages in the app.

We will need to modify MainActivity.java, activity_main.xml.

Writing the app

For our app we will add a Button with the label Click on Me!”, we will change the default “Hello world!” label to “Click on me if you dare!” plus change its position so that it is in the center. And finally, we will add some code to display a “toast” like a windows popup when the button is tapped!

Let’s start by altering the text of the label and changing its alignment. First find “activity_main.xml” in the project tree and double-click it. Remember, “activity_main.xml” is the file which holds the User Interface definition. At the bottom of the code window there are two tabs, “Design” and “Text.” Make sure you are using the “Design” tab.

Now click in the text “Hello world!” that is shown on the rendering of the phone. If it is too small use the zoom button (the plus sign in a magnifying glass) to enlarge the rendering of the phone.

In the “properties” window just to the right of the phone image, you find "Text" Click the space next to it and you can rename "Hello World" to "Tap me if you dare!" Then, scroll down until you find “layout:centerHorizontal.” Check the box next to it. The “Tap me if you dare!” text will now jump to the horizontal center.

modify_project_1.PNG

modify_project_2.PNG

To recap. We have aligned the text on the horizontal center and we have changed the text. Now to add a button. Back on the “Design” tab of “activity_main.xml,” find and click on “Button” in the “Palette” list to the left of the phone render. Now click somewhere beneath “Tap me if you dare!” on the phone image, make sure it is in the middle.

modify_project_3.png

Now double-click on the button so that you can change the text to "Tap Me!".

modify_project_4.png

The final step is to add some Java code which reacts to the button being tapped. One of the UI elements of Android is a “toast.” A toast provides simple feedback in a small popup. You will certainly have seen it. For example, in Gmail navigating away from an email before you send it triggers a “Message saved as a draft.” Toasts automatically disappear after a timeout.

For our app we will display a toast every time the button is tapped. The first step is to add some Java code. Find MainActivity.java and add the following code beneath “onCreate”:

public void onButtonTap(View v) {
        Toast myToast = Toast.makeText(getApplicationContext(), "Woow!", Toast.LENGTH_LONG);
        myToast.show();
}

The word “View” in “(View v)” will likely be in red with a message bubble displayed near it. This is Android Studio telling you that you have used a new construct (View) without importing it in the import section, at the top of the Java code. This is easy to fix. Click on the word “View” and then press ALT+ENTER, Android Studio will fix it for you! If the word “Toast” is in red, then do exactly the same thing again. Click on the word Toast and then press ALT+ENTER.

modify_project_5.PNG

Now back in the designer for “activity_main.xml”, click on the button and scroll down through the properties list until you find “onClick”. Click on the box to the right and a list of functions will appear. Click on “onButtonTap”, the function we just added.

modify_project_6.png

So now the “onButtonTap()” function will be called whenever the button is tapped. When it is called it creates a Toast called myToast that will display the message “Ouch!”. To show the Toast we just call myToast.show().

And that’s it, in terms of writing our app, now to test it in the emulator.

Building and testing your app

Under the Tools menu, navigate to Android -> AVD Manager. This tool shows you the list of currently configured Android Virtual Devices. The List will be empty especially if it is the first time you have tested a project. Click on the Create New Virtual Device Button.

On the “Create New Virtual Device” dialog, use the default specs and click “Next.”, Then choose the apporpirate Android Version -As you Like- I will choose "marshmallow", Then Click "Finish."

test_1.PNG

test_2.PNG

After Adding the virtual device, Click on the play icon (the triangle) under the actions column. This will start the emulator.

Depending on the performance of your PC and the amount of memory you have, the emulator can take several minutes to start up. Once the emulator is up go to the Tools menu and click on “Run ‘app'”. This will compile the app and send it to the emulator. During this process Android Studio will ask you which emulator to use. You will see your running emulator in the list, it should be the default option, so just click OK.

The app will appear in the emulator (eventually). Click on the “Tap me!” button and watch for the toast to appear towards the bottom of the emulated device. Congratulations!

Now I would to tell you it is the final destination of our trip.

See you on the next article "How to create Sound Recorder App".

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?