Andr Unit 1 Notes
Andr Unit 1 Notes
What is Android
Android is a software package and linux based operating system for mobile devices such as tablet
computers and smartphones.
It is developed by Google and later the OHA (Open Handset Alliance). Java language is mainly used to
write the android code even though other languages can be used.
The goal of android project is to create a successful real-world product that improves the mobile
experience for end users
Features of Android
After learning what is android, let's see the features of android. The important features of android are
given below:
1) It is open-source.
3) There are a lot of mobile applications that can be chosen by the consumer.
4) It provides many interesting features like weather details, opening screen, live RSS (Really Simple
Syndication) feeds etc.
It provides support for messaging services(SMS and MMS), web browser, storage (SQLite), connectivity
(GSM, CDMA, Blue Tooth, Wi-Fi etc.), media, handset layout etc.
History of Android
The history and versions of android are interesting to know. The code names of android ranges from A to
J currently, such as Aestro, Blender, Cupcake, Donut, Eclair, Froyo, Gingerbread, Honeycomb, Ice
Cream Sandwitch, Jelly Bean, KitKat and Lollipop. Let's understand the android history in a sequence.
1) Initially, Andy Rubin founded Android Incorporation in Palo Alto, California, United States in
October, 2003.
2) In 17th August 2005, Google acquired android Incorporation. Since then, it is in the subsidiary of
Google Incorporation.
3) The key employees of Android Incorporation are Andy Rubin, Rich Miner, Chris White and Nick
Sears.
4) Originally intended for camera but shifted to smart phones later because of low market for camera
only.
5) Android is the nick name of Andy Rubin given by coworkers because of his love to robots.
Android Studio
Android Studio is the official Integrated Development Environment (IDE) for android application
development. Android Studio provides more features that enhance our productivity while building
Android apps.
Android Studio was announced on 16th May 2013 at the Google I/O conference as an official IDE for
Android app development. It started its early access preview from version 0.1 in May 2013. The first
stable built version was released in December 2014, starts from version 1.0.
Since 7th May 2019, Kotlin is Google's preferred language for Android application development. Besides
this, other programming languages are supported by Android Studio.
Features of Android Studio
● Android Studio has a consolidated environment where we can develop for all Android devices.
● Apply changes to the resource code of our running app without restarting the app.
● It provides build-in supports for Google Cloud Platform. It makes it easy to integrate Google
Cloud Messaging and App Engine.
Android Studio Project Structure
The Android Studio project contains one or more modules with resource files and source code files. These
include different types of modules
● Library modules
These build files are visible to the top-level under Gradle Scripts. And the app module contains the
following folders:
● java: It contains the source code of Java files, including the JUnit test code.
● res: It contains all non-code resources, UI strings, XML layouts, and bitmap images.
We will see the actual file structure of the project by selecting the Project from the Project dropdown.
2. The navigation bar helps in navigating our project and open files for editing. It gives a compact
view of structure visible in the Project window.
3. The editor window is a space where we can create and modify our code. On the basis of the
current file type, the editor can change. While viewing a layout file, the editor displays the Layout
Editor.
4. The tool window bar runs around the outside the IDE window and contains buttons that allow as
to expand and collapse individual tool windows.
5. The tool windows provide us access specific tasks like search, project management, version
control, and more. We can able expand and collapse them.
6. The status bar displays the status of our project and IDE itself, as well as any messages or
warnings.
We are willing to organize the main window to give ourselves more screen space by moving or hiding
toolbars and tool windows. We can also use keyboard shortcuts to access most of the IDE features.
● We can create multiple APKs from our app, with different features using the same project and
modules.
File: MainActivity.java
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
File: activity_main.xml
Android studio auto generates code for activity_main.xml file. You may edit this file according to your
requirement.
<android.support.constraint.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="com.example.helloworld">
<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" />
</android.support.constraint.ConstraintLayout>
match_parent
match_parent is used for referring to the parent size either through layout_width or layout_height
attributes. If we set layout_height as match_parent then the view's height will be as same as the parent's
height. The parent is the outer layout in which the view is present.
Wrap_content
wrap_content is used to wrap the view according to its actual size. It can be used with
layout_height as well as layout_width. If we set layout_height as wrap_content then the height
will be the same as the content size and if we set layout_width as wrap_content then the width
will be the same as the content size. The size will be defined on the basis of content size, if the
image size is small then the view will wrap only that image according to its size.
1) Create the New Android project
Now an android project has been created. You can explore the android project and see the simple
program, it looks like this:
Action bar
In Android applications, ActionBar is the element present at the top of the activity screen. It is a salient feature of a
mobile application that has a consistent presence over all its activities. It provides a visual structure to the app and
contains some of the frequently used elements for the users. Android ActionBar was launched by Google in 2013 with the
release of Android 3.0(API 11). Before that, the name of this top most visual element was AppBar. AppBar contains only
the name of the application or current activity. It was not very much useful for the users and developers also have
negligible option to customize it.
Google announced a support library along with the introduction of ActionBar. This library is a part of AppCompat and
its purpose is to provide backward compatibility for older versions of Android and to support tabbed interfaces. All
applications that use the default theme provided by the Android(Theme.AppCompat.Light.DarkActionBar), contains an
ActionBar by default. However, developers can customize it in several ways depending upon their needs. Components
included in the ActionBar are:
● View Controls: Section that displays the name of the application or current activity. Developers can
● Action Button: Contains some important actions/elements of the app that may be required to the users
frequently.
The following example demonstrates the steps involved in creating a custom ActionBar for the MainActivity of an
application. All important aspects of visual elements like icon, title, subtitle, action buttons, and overflow menu will be
covered.
Default ActionBar
As mentioned earlier, every android app contains an ActionBar by default. This pre-included ActionBar display title for
the current activity that is managed by the AncdroidManifest.xml file. The string value of the application’s title is
provided by @string/app_name resource present under the application nodes.
<application
…..
…..
android:label=”@string/app_name”
…..
</application>
Output:
Advantages of ActionBar
● Specify the location of the user in the app by displaying the title of the current Activity.
● Support tabs and a drop-down list for view switching and navigation.
Disadvantages of ActionBar
● All features of the ActionaBar are not introduced at once but were introduced with the release of
● The features that were introduced with a particular API does not provide backward compatibility.
Note: To resolve all the problems related to ActionBar, Google introduced ToolBar along with the release of API 21(Android
Lollipop).
Android Intent is the message that is passed between components such as activities, content providers, broadcast
receivers, services etc.
It is generally used with startActivity() method to invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
The LabeledIntent is the subclass of android.content.Intent class.
● Launch an activity
● Broadcast a message
1) Implicit Intent
Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components
provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
2. intent.setData(Uri.parse("http://www.javatpoint.com"));
3. startActivity(intent);
2) Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
2. startActivity(i);
To get the full code of explicit intent, visit the next page.
Android Implicit Intent Example
Let's see the simple example of implicit intent that displays a web page.
activity_main.xml
File: activity_main.xml
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.com.implicitintent.MainActivity">
8.
9. <EditText
10. android:id="@+id/editText"
11. android:layout_width="wrap_content"
12. android:layout_height="wrap_content"
13. android:layout_marginEnd="8dp"
14. android:layout_marginStart="8dp"
15. android:layout_marginTop="60dp"
16. android:ems="10"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.575"
19. app:layout_constraintStart_toStartOf="parent"
21.
22. <Button
23. android:id="@+id/button"
24. android:layout_width="wrap_content"
25. android:layout_height="wrap_content"
26. android:layout_marginRight="8dp"
27. android:layout_marginLeft="156dp"
28. android:layout_marginTop="172dp"
29. android:text="Visit"
30. app:layout_constraintEnd_toEndOf="parent"
31. app:layout_constraintHorizontal_bias="0.0"
32. app:layout_constraintStart_toStartOf="parent"
34. </android.support.constraint.ConstraintLayout>
Activity class
File: MainActivity.java
1. package example.com.implicitintent;
2.
3. import android.content.Intent;
4. import android.net.Uri;
5. import android.support.v7.app.AppCompatActivity;
6. import android.os.Bundle;
7. import android.view.View;
8. import android.widget.Button;
9. import android.widget.EditText;
10.
12.
15.
16. @Override
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_main);
20.
25. @Override
29. startActivity(intent);
30. }
31. }); } }
32. Output:
Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity
in android by explicit intent.
We can also pass the information from one activity to another using explicit intent.
Here, we are going to see an example to call one activity from another and vice-versa.
Let's see the simple example of android explicit example that calls one activity from another and vice versa.
activity_main.xml
File: activity_main.xml
xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="examplet.com.explicitintent.FirstActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callSecondActivity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
37.
38. </android.support.constraint.ConstraintLayout>
ActivityOne class
File: MainActivityOne.java
1. package example.com.explicitintent;
2.
3. import android.content.Intent;
4. import android.support.v7.app.AppCompatActivity;
5. import android.os.Bundle;
6. import android.view.View;
7.
9.
10. @Override
12. super.onCreate(savedInstanceState);
13. setContentView(R.layout.activity_first);
14. }
19. // Set the request code to any code you like, you can identify the
21. startActivity(i);
22. }
23.
24. }
activitytwo_main.xml
File: activitytwo_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.com.explicitintent.SecondActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callFirstActivity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
37. </android.support.constraint.ConstraintLayout>
ActivityTwo class
File: MainActivityTwo.java
1. package example.com.explicitintent;
2.
3. import android.content.Intent;
4. import android.support.v7.app.AppCompatActivity;
5. import android.os.Bundle;
6. import android.view.View;
7. import android.widget.Toast;
8.
10.
11. @Override
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.activity_second);
20. }
23. startActivity(i);
24. } } Output:
Android Activity Lifecycle
Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of
ContextThemeWrapper class.
By the help of activity, you can place all your UI components or widgets in a single screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.
Method Description
onResume called when activity will start interacting with the user.
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.com.activitylifecycle.MainActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
13. app:layout_constraintBottom_toBottomOf="parent"
14. app:layout_constraintLeft_toLeftOf="parent"
15. app:layout_constraintRight_toRightOf="parent"
17.
18. </android.support.constraint.ConstraintLayout>
It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content
on the logcat.
File: MainActivity.java
1. package example.com.activitylifecycle;
2.
3. import android.app.Activity;
4. import android.os.Bundle;
5. import android.util.Log;
6.
8.
9. @Override
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.activity_main);
14. }
15. @Override
17. super.onStart();
19. }
20. @Override
24. }
25. @Override
27. super.onPause();
29. }
30. @Override
32. super.onStop();
34. }
35. @Override
37. super.onRestart();
39. }
40. @Override
42. super.onDestroy();
43. Log.d("lifecycle","onDestroy invoked");
44. } }
Button,
ImageButton,
Android RadioButton
RadioButton is a two states button which is either checked or unchecked. If a single radio
button is unchecked, we can click it to make checked radio button. Once a radio button is
checked, it cannot be marked as unchecked by user.
RadioButton is generally used with RadioGroup. RadioGroup contains several radio buttons,
marking one radio button as checked makes all other radio buttons as unchecked.