Skip to main content

My Android Notes - 1

This will be a series of tutorials on basics of Android app development. Although I won’t get too much deep into the practicalities of Android app developement (you have YouTube tutorials for that), but I will try to focus more on the concepts involved.
So, let’s begin!

Project Structure


This is the project structure of a sample app called “Netphlix”. In your Android Studio, it will be displayed on the left sidebar of the window.
  • manifest: You can see a file called AndroidManifest.xml in a directory (folder) called manifests. This AndroidManifest.xml file holds all the necessary information about the app. Information like: what permissions your app requires to run (like Internet permission, permission to access local disk, permission to access the camera etc.), what all components your app has, name of the app, path to the app’s icon image etc.
  • java: Next, you can see a directory named java. This is where all the Java files that you write for your app reside. All the app’s logic code, test code etc. are placed in this directory.
  • res: This is the folder where your resource files reside. All the UI design code, your app’s icon image and any other binary files required by your app. You can see a sub-directory called layout. This is where you will be spending most of your time while designing the UI of your app.
  • gradle: Gradle is a software that is used to build your project, i.e., compile Java files, generate JAR files, generate APK files, run tests on your app and add/remove dependencies etc. Gradle scripts are used to automate all the aforementioned processes. Android Studio comes with a Gradle preinstalled.
We will worry about what other files and directories are for; later.

App components

There are mainly four types of components in an Android app:
  1. Activity
  2. Service
  3. Broadcast receiver
  4. Content provider

Activity

Activity is the most fundamental part of an app. It is the visible part of the app that is actively running. Each screen of your app is an Activity. During your app development, to create an Activity, you need to create an XML file in res directory and also create a Java file in java directory and this Java file is required to give life to that static XML design file. So, an Activity is composed by an XML file and a Java file (XML file to define the UI and Java file to write the logic). Of course, you don’t have to take the pain of creating these files. In your Android Studio, if you go to File » New » Activity, you will get a list of which type of activity you want to create. Select any one type of the Activity and Android Studio will create the Java file, XML file and it’ll do the required changes in AndroidManifest.xml file automatically.
When we create a new Empty Activity in Android Studio, we will be provided with a boilerplate code. I created an Activity called FirstActivity and Android Studio created a Java file and an XML file for me.
FirstActivity.java
activity_first.xml
Here, I would like to make it clear that you don’t have to learn XML at all. If you have some familiarity with HTML or a similar markup language, it is more than enough. Android Studio has an excellent code completion mechanism and there are plenty of XML code snippets (related to Android) available online that you can just copy-and-paste and alter the parameters according to your needs. If you are too lazy to do that, then Android Studio offers a visual editor where you can just drag-and-drop UI elements and Android Studio generates the XML file automatically (although, I don’t recommend using visual editor unless you are using ConstraintLayout).
Now, before we move further, we will get familiar with some important terms: Intent and Context.

Intent

So, if you want to build a decent app which has multiple Activities, then how do you navigate from one Activity to another? Let’s say that you have a button in your 1st Activity. When that button is pressed, you want to go to another Activity. To do this, a class called Intent is used. You have to tell your ‘intention’ to the Intent class that you would like to go from the current Activity to another Activity. You can also give some “payload” data (which is optional) for the Intent class to carry it to the next Activity and it will do that for you.
In the code example below, we are creating an instance of Intent class and specifying that we want to go from the FirstActivity (which is currently active) to the SecondActivity. Also, we want to put some payload data called “name” with value “nandan” for the intent to carry it to the SecondActivity. When we call startActivity(intent);, the SecondActivity will be created and the payload data will be available in SecondActivity.

Of course, you need to place the above code in the OnClickListener of your Button, so that, SecondActivity will be launched when the Button is clicked.
It should also be noted that Intent is not just for navigating from one Activity to another. It is used to start a Service, can be used in Broadcast Receivers etc.
There is also something known as PendingIntent which is used to specify something that is to be done in future. It is used when you are dealing with AlarmManager (used to schedule tasks). But that is out of scope of these notes.
page 2

Comments

Popular posts from this blog

My-Cloud-IDE - a SaaS built using Docker

I always wanted to know about how services like Codenvy  and Heroku  internally worked. The only way to understand that was to build a similar project. The main component of this project is Docker. Docker  Docker is a software that performs OS-level virtualisation and is developed for Linux. More information about it can be found here . About My-Cloud-IDE: My-Cloud-IDE is a proof-of-concept for a Software as a Service (SaaS). It   can be used to perform software development on cloud without worrying about resolving software dependencies, software installations etc. The user gets a fully functional IDE in his/her browser after registration. Each user has his/her own isolated software environment because My-Cloud-IDE uses Docker to perform OS-level virtualisation in the backend. Technical Details I divided the project into two modules. One module was for 'management' purpose. This includes user interfaces, user management and preparing some files that a...

How did I setup a Rasberry Pi 3 without any peripherals

I am an absolute beginner in this Raspberry Pi (I'll be using "RPi" for short) world and this post shows how I started up the RPi without using external keyboard or mouse (for RPi) and still managed to get a graphical desktop environment of Raspbian on my Debian workstation (and controlled my RPi remotely). Step 1: I downloaded 'Raspbian Stretch with Desktop' ( here ). I got a ZIP file. I unzipped it and ended up with a .img which was roughly 4.6 GB. Step 2: Now, to create a bootable media (a memory card perhaps) by copying the .img file onto it, I used dd command. sudo dd if=<path to img> of=/dev/<sdb,sdc..> Be particularly careful with using dd command. You can mess up a lot of things. Using dd command on your memory card will wipe out all contents in it. Using dd command on your hard disk, umm.. not a good idea. To know the disk path of your memory card, use sudo fdisk -l  (el in small caps) . Your hard disk will probably be /dev/sda. You...

Privacy Concerns in Android

Introduction       Android has a robust Access control system. If an Android app needs to use resources or information outside of its own scope, the app has to request the appropriate permission . It needs to get permissions before accessing critical resources like Camera, Location, Contacts etc. On Android 6.0 (Marshmallow) or higher, the app needs to request these permissions at runtime by showing a dialog box to the user. Also, the user will have an option to revoke these permissions for that app at any time in future.       But, in this article, I would like to point out a few resources that an app can access without any permission s and this might raise a serious privacy concern for the user. Privacy Concerns An app can fetch a list of all other apps installed on your phone. An app doesn't need to request any permissions from you to get a list of all the apps installed on your phone. For example, a Netflix app can get to know wh...