CHAPTER 1: OVERVIEW
What is android?
Android is an open source Operating System based on the Linux kernel designed primarily for mobile devices such as smart phones and tablet computers. Android is developed by the Open Handset Alliance, led by Google.
Android Software Development Kit (SDK) is generally used to develop android applications and the basic language used here is java. The developed applications can be easily packaged and distributed.
The aim of the tutorial is to make you ready to design, develop and deploy your own android applications.
You mainly require 3 types of software to start your programming and they are Java Development Kit (JDK), Java Runtime Environment (JRE) & Android Studio.
Latest versions of Java Development Kit (JDK) and Java Runtime Environment (JRE) can be downloaded from http://www.oracle.com/technetwork/java/javase/downloads/index.html and the latest version of Android Studio can be found at https://developer.android.com/studio/index.html
All these required tools to develop an android application are freely available and can be easily downloaded & installed from the internet.
Android application programming is basically java programming and you need a basic understanding on java programming to start. Some basic java knowledge you require to proceed are in the areas of :
- Data types (int , float, boolean, etc.)
- Strings & Arrays
- Selection Control Structures (if, if-else, switch etc)
- Object Oriented Concepts like Inheritance, etc.
- Methods, Objects & Classes
CHAPTER 2: ANDROID STUDIO INSTALLATION
First go to this android developer’s site to download the Android Studio https:/developer.android.com/studio/index.html
This is the main page,we can see the download button for Android Studio latest version
According to your operating system scroll down to the bottom of the page & download the one necessary for you.
After download complete open it and see the installation wizard.
Please wait until installer verification is completed. Then this window opens, Click on Next.
This will install android studio , Android SDK and Android virtual device click on Next.
Agree to the terms and conditions by clicking on ‘I Agree‘
The next step is to specify the install location, there will be predefined location in the OS partition we can change if we need then click on Next
Click on Install
Installation will take some time be patient..
After installation click on Next
Now we have successfully installed Android studio, check the start Android studio and click on Finish
Next, click on the “I do not have previous version….” checkbox and click OK.
Note: If this popup doesn’t appear, then Android Studio automatically pops open, if that’s the case then go to File ->Settings ->Appearance & Behaviour ->System Settings ->Android SDK and on the right side of the window download necessary SDK Platforms & SDK Tools
Click on Next
We need standard setup for this ,check standard and click Next
We need to install the whole items in the screen so click on Finish. Then it will start Downloading and Installing
Downloading may take longer according to the internet you are using be patient
After Downloading and Installing click on Finish
Now the starting page appears .Now we can start projects in android studio.. Enjoy!!!!
CHAPTER 3: BASIC COMPONENTS OF APP DEVELOPMENT
- Activity: Activity specifies a single page, its views and the functions it performs. In a simple android application project, views/user interface are usually created using a xml layout and is connected to our java class file.
- Manifest File: Try creating an app and an AndroidManifest.xml file is automatically generated. All app characteristics, permissions, etc are specified here. It’s the core of an android application program.
- Broadcast Receivers: The basic function of a broadcast receiver is to respond to any system or application event that has been triggered. For example, consider a broadcast receiver is created and registered for CALL BUTTON. This broadcast receiver will perform the function specified to it when a user tries to make a call. After creating a broadcast receiver we should register it in manifest.
- Fragment: A fragment is used to make life easier for us while using activity, as an activity is a single screen/ui component. Fragment helps us to implement multiple view/ui into a single activity, add/remove it, etc. Fragment has its characteristics and its lifecycle is pretty much associated with the activity holding the fragment.
- Intent: Intent is a means by which we communicate to make things happen in android with different components. It’s basically our intention described. Say for example we have ActivityFirst and ActivitySecond; to go from ActivityFirst to ActivitySecond we use intent (explicit). Also, consider sharing a picture which also uses intent except the difference here is that we do not know which component to use (implicit).
- Service: A service is a background running task used for long running operations, network calls, etc. A service doesn’t have any user interface/interaction and can be started and stopped by us according to our requirement. Always remember to declare the service created in the manifest.
The above mentioned are some basic components in android application development and a detailed explanation will be provided as we proceed.
CHAPTER 4: HELLO WORLD! EXAMPLE
Open Android Studio and click “Start a new Android Studio Project”
Then enter your application name, project location, etc. Always note to provide a valid/unique package name
Next step is to select the android devices the app is targeted for. For this example, we select phone and tablets with minimum API version 14+, i.e, app will run fine from Android OS IceCreamSandwich+.
The next step is to select add an activity, since this is our first project, we select a basic activity. The rest of activities are self explanatory.
The most important elements/components used in an application program development are listed below:
It is the XML file that defines layout of the activity or the user interface. Open it and you can find a TextView element which is basically a text that says “Hello World!”.
There are 2 files with this name: one is for the whole project and one is for the “app” module. Each module will own its build.gradle file. The “app” module’s build.gradle file helps in configuring how the Gradle tools compile and build your app.
Before we move forward just note that the build system first compiles the app resources and source code, and packages them into APK (Application Package) that we test, deploy, sign, and distribute. Gradle is an advanced build toolkit, which manages the build process.
Now to run app, we need a physical device (actual android mobile phone)/an emulator.
An emulator is a virtual device provided by studio which has almost all features as that of a real phone.
The above screenshot shows the buttons for running the app and selecting the AVD (Android Virtual Device) Manager
When you click the AVD Manager a window shown above appears, here select the CREATE VIRTUAL DEVICE BUTTON
Next, select the desired phone for the emulator
System Image for an emulator is necessary, so select your required one and download it.
After you RUN the HELLO WORLD app, the emulator will be opened as shown above.VIDEO 1
CHAPTER 5: Basic Views And View Groups
A view represents the basic building block for user interface components.
Let’s consider the example used in Chapter 4
Here we have a textview. A TextView is an user interface to show text to user. For any view, we can define its basic features in the xml file or in the java file as we wish. Here the text is set in xml as android:text=”Hello World!”. We can similarly set the text via java, all we need to do is
Similarly a textview’s size, color etc. can be set in android. I holds true for other views too.
Note: If at any point if an error exist in the Java class file try using Alt+Enter (in Windows) while placing the cursor on the error. We will be provided with a list of solutions to solve the error, if present.
Now let’s checkout some other basic views in android:
EditText is a user interface for entering and modifying a text, like the input fields used in Login/Signup pages. Whenever defining the EditText try specifying the inputType of the textinputType can be text,password,etc.
Button is the user interface element which is used to tap or click to perform an action.
ImageView is used to display an image either inside the project or from an external url, etc.
A ViewGroup is a collection of views and acts as parent and position child views accordingly.
LinearLayout is a view group that aligns all its children in a single direction, i.e., horizontal or vertical.This can be specified by the attribute ‘android:orientation’.
RelativeLayout is a view group that displays child views in relative positions. Views can be placed anywhere in the layout, i.e., top or bottom or left or right, etc.
Now let’s consider the 2 layouts given below, one is LinearLayout and other is using a RelativeLayout.
For the LinearLayout, the orientation tag is important as it defines the way child views are placed.
For RelativeLayout, everything is surrounded in line with the initial view, here it’s the textview.
The views and view groups specified here are some samples and more components exist. Along with the tutorial we will learn more and more about these.
- Note: When we start a new Activity in studio, the UI/xml files will have the parent view as a Coordinator Layout/ Constraint Layout, etc. We have changed it to Linear/Relative Layouts in our examples. Since we are beginning our tutorial, it will be better if we start with these basic Layouts.Also <include> is used to include a second layout into our present layout. Say we need to add an actionbar.xml to activity_main.xml and second_activity.xml, we can include actionbar.xml in both these layouts rather than writing code for each layout.
CHAPTER 6: Resources
The resources are static contents that used by our application such as bitmaps, strings, colors, layouts etc. These all are maintained separately in various sub directories under res/ directory of the project.
The different types of Resources are drawable, anim, layout, values (strings, styles, colors, dimens etc)
drawable: This is the folder used to put all the images needed to the application.There are different folder variants (iehdpi, ldpi, xdpi, xxdpi, xxxdpi) according to the screen size of phones.
anim: This is the folder used to put all the animation xml files.
animator – XML files that define property animations.
color – XML files that define a state list of colors.
drawable – Bitmap files / Shapes / Animation drawables / Other drawables
layout – XML files that define a user interface layout.
menu – XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu.
raw – files to save in their raw form.(music video doc etc)
values – XML files that contain simple values, such as strings, integers, and colors.
colors.xml for color values
dimens.xml for dimension values.
strings.xml for string values.
CHAPTER 7: ACTIVITY LIFE CYCLE
Every Activity has a Life cycle from the time it starts till it ends and everything in between can be known and accessed by us.
Life cycle of an activity: Main elements in an activity Life cycle are:
- onCreate – this function is called when an activity is created.
- onStart – this function is called when an activity becomes visible.
- onResume – this function is for interacting with the user, basically whenever activity is resumed. Say, user presses home button and after some time returns to the activity, on returning to the activity onResume is called.
- onPause – this function is called when activity loses focus like another app pops up, etc.
- onStop – this function called when activity is no longer visible.
- onRestart – this function called after your activity restarts, i.e stopped and then recalled.
- onDestroy – this function called before the activity is destroyed.
Note: onPause is called on a partially hidden state and onStop is called on a fully hidden state of the activity.
Activity Life Cycle Flowchart :
Now let’s check out the example shown below:
Run the app and note that you won’t see any output on the emulator or device. For this open logcat, i.e., click the Android Monitor button marked in the screenshot above.
The Logcat now shows,onCreate, onStart and onResume method invoked.
Next click the HOME Button and we see onPause method invoked.
After a while we see onStop method invoked.
Next click the overview button of your phone and launch the app again or click the app icon from home screen – and now we can see that the onRestart, onStart and onResume methods are invoked in the logcat.VIDEO 4
CHAPTER 8: INTENT & SERVICES
Intent is our intention described. A basic example is when we have to go from ActivityFirst to ActivitySecond on a button click, here we use intent.
To create a new activity, right click on the project and add a new Java Class plus its corresponding xml file & add it to your manifest or right click on the project and add a new Activity (Basic Activity for the time being). If you directly add an Activity, it will automatically generate its xml file & add it to the manifest.
Note: Toast is a small pop up that displays for a small amount of time to show the user some message. In the below screenshot we can see a toast which shows us the value passed from ActivityFirst.
Consider the above screen shots. To start a new activity when Submit button is clicked we use:
Intent intent = new Intent(ActivityFirst.this, ActivitySecond.class);
Now, consider we have some data from ActivityFirst to pass to ActivitySecond here we use:
Intent intent = new Intent(ActivityFirst.this, ActivitySecond.class);
To receive the value at ActivitySecond we use:
String value = getIntent().getStringExtra(“key”);
Also, say we want to open a website. This is done along with a choice of installed web browsers using:
Intent intent = new Intent(Intent.ACTION_VIEW);
Consider opening an app from your phone’s home page. There are multiple activities in manifest and the system doesn’t know which activity to open. This is one of the places where an intent filter comes into play. An intent filter is used to specify the type of intents a component (activity, service, or broadcast receiver) will get.
The above screenshot shows us 2 activities in manifest: MainActivity&SecondActivity. The system doesn’t know which activity to open on launch. So we use an intent-filter which defines the MainActivity as the launcher activity.
A service is an operation that runs in the background usually used for long running tasks, network calls, etc.
The above screenshot defines a Service which starts music and plays it until stopped. Now don’t forget to declare our service in manifest:
<service android:name=”.MySampleService” />
After this, call startService(new Intent(this, MySampleService.class)); to start the Service &
stopService(new Intent(this, MySampleService.class)); to stop it.VIDEO 6
CHAPTER 9: Broadcast Receiver
Use broadcast receivers to get the application control whenever a system or application event is triggered. Always remember to register broadcast receiver in manifest. Some examples:
- Sample codes :
- System Events:
Event Description Intent.ACTION_BOOT_COMPLETED Boot completed. Requires the android permission. RECEIVE_BOOT_COMPLETED permission Intent.ACTION_POWER_CONNECTED Power got connected to the device. Intent.ACTION_POWER_DISCONNECTED Power got disconnected to the device. Intent.ACTION_BATTERY_LOW Triggered on low battery. Typically used to reduce activities in your app which consume power Intent.ACTION_BATTERY_OKAY Battery status good again.
Some system event examples are shown above, that can be used for broadcast receivers.
CHAPTER 10: FRAGMENT
Fragment acts as a part of activity itself. We can add multiple fragments to constitute an activity and add/remove it according to our requirements.
Consider a Navigation Drawer Activity as shown below. Here say we need to show the drawer in “Gallery”, “Tools”, etc pages too. We can create separate activities with drawer for “Gallery”, “Tools”, etc or keep this activity and generate Fragments for “Gallery”, “Tools”, etc.
A simple Fragment class looks like the screenshot above.
A fragment has its own lifecycle, its own onPause(), onResume(), etc. A fragment can be easily added and removed to an activity.
Fragment Life Cycle