Versions of Android
Versions of Android
Android architecture or
Android software stack is
categorized into five parts:
1. linux kernel
2. native libraries
(middleware),
3. Android Runtime
4. Application
Framework
5. Applications
Linux Kernel
● Mainly, it provides the base for the application framework and powers our
application with the help of the core libraries.
Later on, it was purchased by Google in 2005. The beta version of Android OS
was released on November 5, 2007, while the software development kit (SDK)
was released on November 12, 2007.
The first Android mobile was publicly released with Android 1.0 of the T-Mobile
G1 (aka HTC Dream) in October 2008.
Google announced in August 2019 that they were ending the confectionery
scheme, and they use numerical ordering for future Android versions.
The first Android version which was released under the numerical order format
was Android 10.
API level :API Level is an integer value that uniquely identifies the framework API revision offered
by a version of the Android platform.
Android Version 1.0 to 1.1: No codename
Android officially publish its Android version 1.0 in September 2008.
It is the initial version of Android operating system. It supports Web
browser to show HTML and XHTML web pages, camera, access web email
server (POP3, IMAP4, and SMTP).
This version contains Google Calendar, Google Maps, Google Sync, Google
Search, Google Talk, Instant messaging, Media player, Notifications appear
in the status bar, wallpaper, YouTube video player, Alarm Clock, Calculator,
Dialer, Pictures (Gallery), Wi-Fi and Bluetooth support.
Android version 1.5: Cupcake
On April 27, 2009, the Android updated to 1.5 with the codename of the
dessert item (Cupcake). It has Linux kernel 2.6.27. It supports third-party
virtual keyboard, Video recording and playback in MPEG-4, Copy and paste
feature, Animated screen translations, auto-rotation option, ability to
upload a video to YouTube, upload photos to Picasa, check phone usage
history.
Android version 1.6: Donut
On September 15, 2009, Android 1.6 was released with the name Donut. It
contains numerous new features such as voice and text entry search,
bookmark history, contacts, web, "speak" a string of text, faster camera access,
user can select multiple photos for deletion, support text-to-speech engine,
WVGA screen resolutions.
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
}
2. Services
These components run at the backend, updating your data sources and Activities, triggering Notification,
and also broadcast Intents.
They also perform some tasks when applications are not active. A service can be used as a subclass of class
Service:
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.
3. Content Providers
● It is used to manage and persist the application data also typically interacts with the SQL database.
● They are also responsible for sharing the data beyond the application boundaries.
● The Content Providers of a particular application can be configured to allow access from other
applications, and the Content Providers exposed by other applications can also be configured.
A content provider should be a sub-class of the class ContentProvider.
● 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.
}
4. Broadcast Receivers
They are known to be intent listeners as they enable your application to listen to the Intents that satisfy the
matching criteria specified by us. Broadcast Receivers make our application react to any received Intent
thereby making them perfect for creating event-driven applications.
● 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.
}
Additional Components
There are additional components which will be used in the construction of above mentioned entities, their
logic, and wiring between them. These components are −
Activities
● An application typically has multiple activities, and the user flips back and
forth among them.
● As such, activities are the most visible part of your application.
● Just like a website consists of multiple pages, so does an Android
application consist of multiple activities.
● As a website has a “home page,” an Android app has a “main” activity,
usually the one that is shown first when you launch the application.
● And just like a website has to provide some sort of navigation among
various pages, an Android app should do the same.
● We can jump from an activity of one application to another activity in a
completely separate application.
● For example, if you are in your Contacts app and you choose to text a friend, you’d be
launching the activity to compose a text message in the Messaging application.
Activity
Life Cycle
onCreate()
● It is called when the activity is first created.
● This is where all the static work is done like creating views, binding data to lists,
etc.
● This method also provides a Bundle containing its previous frozen state, if there
was one.
onStart()
● It is invoked when the activity is visible to the user.
● It is followed by onResume() if the activity is invoked from the background.
● It is also invoked after onCreate() when the activity is first started.
onRestart()
● It is invoked after the activity has been stopped and prior to its starting stage.
● Thus is always followed by onStart() when any activity is revived from background
to on-screen.
onResume()
● It is invoked when the activity starts interacting with the user.
● At this point, the activity is at the top of the activity stack, with a user
interacting with it.
● Always followed by onPause() when the activity goes into the
background or is closed by the user.
onPause()
● It is invoked when an activity is going into the background but has not
yet been killed.
● It is a counterpart to onResume().
● When an activity is launched in front of another activity, this callback
will be invoked on the top activity (currently on screen).
● The activity, under the active activity, will not be created until the
active activity’s onPause() returns, so it is recommended that heavy
processing should not be done in this part.
onStop()
● It is invoked when the activity is not visible to the user.
● It is followed by onRestart() when the activity is revoked from the
background, followed by onDestroy().
onDestroy()
● onDestroy() is called before the activity is destroyed.
● The activity is finishing due to the user completely dismissing the
activity or due to finish() being called on the activity.
● The system is temporarily destroying the activity due to a
configuration change ex. device rotation or multi-window mode.
onStop()
● It is invoked when the activity is not visible to the user.
● It is followed by onRestart() when the activity is revoked from the
background, followed by onDestroy().
onDestroy()
● onDestroy() is called before the activity is destroyed.
● The activity is finishing due to the user completely dismissing the
activity or due to finish() being called on the activity.
● The system is temporarily destroying the activity due to a
configuration change ex. device rotation or multi-window mode.
Intents
● An intent is to perform an action on the screen.
● It is mostly used to start activity, send broadcast receiver, start
services and send message between two activities.
● It is a messaging object which tells what kind of action to be
performed.
● The intent’s most significant use is the launching of the activity.
Body of Intent:
● action: The general action to be performed, such as ACTION_VIEW,
ACTION_EDIT, ACTION_MAIN, etc.
● data: The data to operate on, such as a person record in the contacts
database, expressed as a Uri
Basically two intents are there in android.
● Implicit Intents
● Explicit Intents
Implicit Intent
For Example: In the below images, no component is specified, instead, an action is performed i.e. a webpage
is going to be opened. As you type the name of your desired webpage and click on the ‘CLICK’ button. Your
webpage is opened.
Explicit Intent
For Example: In the below example, there are two activities (FirstActivity, and SecondActivity). When you
click on the ‘GO TO OTHER ACTIVITY’ Button in the FirstActivity, then you move to the SecondActivity.
When you click on the ‘GO TO HOME ACTIVITY’ button in the SecondActivity, then you move to the first
activity. This is getting done through Explicit Intent.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="example.javatpoint.com.implicitintent.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:ems="10"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.575"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="172dp"
android:text="Visit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package example.javatpoint.com.implicitintent;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
editText = findViewById(R.id.editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url=editText.getText().toString();
Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}
Android Explicit Intent
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.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="example.javatpoint.com.explicitintent.FirstActivity
">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="First Activity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.454"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="392dp"
android:onClick="callFirstActivity"
android:text="Call first activity"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
ActivityTwo class
package example.javatpoint.com.explicitintent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
}
Fragment
A Fragment represents a reusable portion of app's UI.
A fragment defines and manages its own layout, has its own
lifecycle, and can handle its own input events.
Fragments cannot live on their own they must be hosted by an
activity or another fragment.
Android Fragment is the part of activity, it is also known as
sub-activity.
There can be more than one fragment in an activity.
Using with fragment transaction. we can move one fragment to
another fragment.
Figure 1. Two versions of the same screen on different screen sizes. On the left, a
large screen contains a navigation drawer that is controlled by the activity and a grid
list that is controlled by the fragment. On the right, a small screen contains a bottom
navigation bar that is controlled by the activity and a linear list that is controlled by
the fragment.
Fragment
Life Cycle
onAttach():
The fragment instance is associated with an activity instance.
The fragment and the activity is not fully initialized.
Typically you get in this method a reference to the activity which uses the
fragment for further initialization work.
onCreate():
The system calls this method when creating the fragment.
You should initialize essential components of the fragment that you want to
retain when the fragment is paused or stopped, then resumed.
onCreateView() :
The system calls this callback when it's time for the fragment to draw its user
interface for the first time.
To draw a UI for your fragment, you must return a View component from this
method that is the root of your fragment's layout.
You can return null if the fragment does not provide a UI.
onActivityCreated():
The onActivityCreated() is called after the onCreateView() method when the host
activity is created.
Activity and fragment instance have been created as well as the view hierarchy of
the activity.
onStart():
The onStart() method is called once the fragment gets visible.
onResume():
Fragment becomes active.
onPause():
The system calls this method as the first indication that the user is leaving the
fragment.
This is usually where you should commit any changes that should be persisted
beyond the current user session.
onStop():
Fragment going to be stopped by calling onStop().
onDestroyView():
Fragment view will destroy after call this method.
onDestroy():
onDestroy() called to do final clean up of the fragment's state but Not
guaranteed to be called by the Android platform.
Android Emulator
● The Android emulator is an Android Virtual Device (AVD), which
represents a specific Android device.
● We can use the Android emulator as a target device to execute and test
our Android application on our PC.
● The Android emulator provides almost all the functionality of a real
device. We can get the incoming phone calls and text messages.
● It also gives the location of the device and simulates different network
speeds. Android emulator simulates rotation and other hardware
sensors. It accesses the Google Play store, and much more
● Testing Android applications on emulator are sometimes faster and
easier than doing on a real device. For example, we can transfer data
faster to the emulator than to a real device connected through USB.
● The Android emulator comes with predefined configurations for several
Android phones, Wear OS, tablet, Android TV devices.
Install the emulator
The Android emulator is installed while installing the Android Studio.
However some components of emulator may or may not be installed while installing Android Studio.
To install the emulator component, select the Android Emulator component in the SDK Tools tab of
the SDK Manager.
1. In Android Studio, we need to create an Android Virtual Device (AVD) that the emulator can use to
install and run your app. To create a new AVD:-
1.1 Open the AVD Manager by clicking Tools > AVD Manager.
1.2 Click on Create Virtual Device, at the bottom of the AVD Manager dialog.
Then Select Hardware page appears.
1.3 Select a hardware profile and then click Next. If we don?t see the
hardware profile we want, then we can create or import a hardware profile.
The System Image page appears.
1.4 Select the system image for the particular API level and click Next. This leads to
open a Verify Configuration page.
1.5 Change AVD properties if needed, and then click Finish.
2. In the toolbar, choose the AVD, which we want to run our app from the target
device from the drop-down menu.
3. Click Run.
Dalvik Virtual Machine(DVM)
Android DDM was written by Dan Bornstein, and Dalvik is the name of a town in
Iceland.
A Dalvik virtual machine enables Android applications to run in its own process with its
own instance.
It executed bytecode generated from APK’s, which are compiled from Java source code
into Dalvik Executable(DEX) files.
The Role of the DVM in Android includes:
● Optimizing the Virtual Machine for memory, battery life, and
performance
● Conversion of class files into .dex file through Dex compiler that runs on
Dalvik VM.
● Converting multiple class files into dex files.
The Dex compiler helps convert the class file into .dex file, the
following image shows how it flows:
● First of all the .java file converts into .class file with the help of Java
compiler.
● Next .class file converts into .dex file using Dec compiler.
● Then finally the packaging process is handled by the Android Assets
packaging (aapt) tools.
Features of Dalvik Virtual Machine
● Register-Based Architecture: Dalvik is a register-based virtual machine unlike the
traditional stack based one (like JVM). This helps minimize overhead that would be
incurred by constantly pushing and popping values off the stack making execution faster,
especially on low memory mobile devices.
● Optimized for Mobile Devices: It is an optimizing BSJ virtual machine that was designed
specifically to run on the Android operating system, with low memory and power
requirements. It virtualizes multiple apps by running a separate instance of the VM for
each application as it does best in simultaneously handling many applications.
● Dex Bytecode Execution: Dalvik uses. dex (Dalvik Executable) files—these compile the
source into a more compact bytecode in order to make it better-suited for low-memory
environments. We compile multiple Java classes into a single. dex file so that it was just
63% of the original size, meaning a small parse and load time on mobile devices.
● Just In Time (JIT) Compilation: Dalvik was released from Android 2.2 (Froyo)
and later included a JIT compiler to translate the bytecode into machine code
directly at execution runtime, which could eventually boost frequently
executed codes performance.
● Garbage Collection: The above code ran on the DALVIK which has automatic
garbage collection to make sure that apps will not consume large amount of
resources and thus there is no effect in system stability.
● Support for Multithreading: Dalvik is multithreading mobile machine can
have multiple threads at the same time for an application to work on. This
feature is one of the most required things when it comes to mobile apps e.g.,
background process along with foreground user tracking.
Advantages of DVM(Dalvik Virtual Machine)
● DVM supports the Android operating system only.
● In DVM executable is APK.
● Execution is faster.
● From Android 2.2 SDK Dalvik has it’s own JIT (Just In Time) compiler.
● Applications are given their own instances.
● Layout objects and UI controls that allow you to build the graphical
user interface for your app.
● Android also provides other UI modules for special interfaces such
as dialogs, notifications, and menus.
Views
● View class represents the basic building block for user interface
components.
● A View occupies a rectangular area on the screen and is
responsible for drawing and event handling.
● All of the views in a window are arranged in a single tree.
● View is the base class for widgets, which are used to create
interactive UI components (buttons, text fields, etc.)
View Group
● A ViewGroup is a special view that can contain other views (called
children.)
● The view group is the base class for layouts and views containers.
● It’s child can be Views or View Group.
Layouts
● A layout defines the structure for a user interface in your activity, such as the
UI for an activity or app widget. You can declare a layout in two ways:
● Declare UI elements in XML. Android provides a straight forward XML
vocabulary that corresponds to the View classes and subclasses, such as
those for widgets and layouts.
● Instantiate layout elements at runtime. Your application can create View
and ViewGroup objects (and manipulate their properties) programmatically.
● All elements in the layout are built using a hierarchy of View and ViewGroup
objects.
● A View usually draws something the user can see and interact with.
● Layout(View Group) is an invisible container that defines the layout structure
for View and other Layout(View Group) objects.
● It provide a different layout structure, such as LinearLayout or
ConstraintLayout.
Common layouts in
Android are:
1) Linear Layout
2) Relative Layout
3) Table Layout
4) Frame Layout
dp (Density-independent
Pixels),
sp ( Scale-independent
Pixels),
pt ( Points which is 1/72 of an
inch),
px( Pixels),
mm ( Millimeters) and
in (inches).
android:layout_width=wrap_content tells your view to size itself to the dimensions required by its
content.
The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand
the rest of the ID string and identify it as an ID resource.
The plus-symbol (+) means that this is a new resource name that must be created and added to our
resources.
Density-independent pixels (dp) are units of measurement that scale to the same size on different screens.
Scale-independent Pixels," which is a unit of measurement used primarily for font sizes in user interfaces,
particularly on Android.
"Pt" stands for "point," and in typography, one point is defined as 1/72 of an inch; meaning that a single "pt" is
equal to 1/72 inch in measurement.
A pixel is the smallest unit of a digital image or display and stands for “picture element.”
Linear Layout
● LinearLayout is a view group that aligns all children in a single direction,
vertically or horizontally.
● You can specify the layout direction with the android:orientation attribute.
● Linear Layout can be created in two direction: Horizontal & Vertical.
● LinearLayout also supports assigning a weight to individual children with the
android:layout_weight attribute.
● This attribute assigns an important value to a view in terms of how much
space it should occupy on the screen.
● A larger weight value allows it to expand to fill any remaining space in the
parent view.
● All children of a Linear Layout are stacked one after the other, so a vertical list
will only have one child per row, no matter how wide they are, and a
horizontal list will only be one row high (the height of the tallest child, plus
padding).
● A Linear Layout respects margins between children and the gravity (right,
center, or left alignment) of each child.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText <EditText
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/to" />
android:hint="@string/subject" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send" />
</LinearLayout>
Relative Layout
● RelativeLayout is a view group that displays child views in relative positions.
● The position of each view can be specified as relative to sibling elements or in
positions relative to the parent RelativeLayout area.
● As it allows us to position the component anywhere, it is considered as most flexible
layout.
● Relative layout is the most used layout after the Linear Layout in Android.
● In Relative Layout, you can use “above, below, left and right” to arrange the
component’s position in relation to other component.
● In this view group child views can be layered on top of each other.
● Relative Layout lets child views specify their position relative to the parent view or to
each other (specified by ID). So you can align two elements by right border, or
make one below another, centered in the screen, centered left,and so on.
● By default, all child views are drawn at the top-left of the layout, so you must define
the position of each view using the various layout properties available from
RelativeLayout. LayoutParams.
There are so many properties that are supported by relative layouts. Some of the most used properties are
listed below:
● layout_alignParentTop
● layout_alignParentBottom
● layout_alignParentRight
● layout_alignParentLeft
● layout_centerHorozontal
● layout_centerVertical
● layout_above
● layout_below
Step 1: Create a New Project
Step 2: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml
and add the below code to that file. <Button
activity_main.xml: android:id="@+id/button1"
<?xml version="1.0" encoding="utf-8"?> android:layout_width="wrap_content"
<RelativeLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_marginTop="35sp"
android:layout_height="fill_parent" android:text="Top Left Button"
android:background="#168BC34A" android:layout_alignParentLeft="true"
xmlns:android="http://schemas.android.com/apk/ android:layout_alignParentTop="true"/>
res/android">
<Button
<Button
android:id="@+id/button2"
android:layout_width="wrap_content" android:id="@+id/button4"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginTop="35sp" android:layout_height="wrap_content"
android:text="Top Right Button" android:text="Bottom Right Button"
android:layout_alignParentTop="true" android:layout_alignParentRight="true"
android:layout_alignParentRight="true"/
<Button
android:layout_alignParentBottom="true"/>
android:id="@+id/button3"
android:layout_width="wrap_content" <Button
android:layout_height="wrap_content" android:id="@+id/button5"
android:text="Bottom Left Button" android:layout_width="fill_parent"
android:layout_alignParentLeft="true" android:layout_height="wrap_content"
android:text="Middle Button"
android:layout_alignParentBottom="true"/> android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
3: Working with the MainActivity.java file
Go to the MainActivity.java/
MainActivity.kt file and refer to the
following code. Below is the code for
the MainActivity file. There is nothing
to write inside the MainActivity file.
Frame Layout
● Frame Layout Frame Layout is designed to block out an area on the
screen to display a single item.
● Generally, Frame Layout should be used to hold a single child view,
because it can be difficult to organize child
● views in a way that's scalable to different screen sizes without the
children overlapping each other.
● You can, however, add multiple children to a Frame Layout and
control their position within the Frame Layout by assigning gravity to
each child, using the android:layout_gravity attribute. Child views
are drawn in a stack, with the most recently added child on top.
● The size of the Frame Layout is the size of its largest child (plus
padding), visible or not (if the Frame Layout's parent permits).
Step 1: Create a New Project in Android Studio
All the views are placed on each other but we displace them according to our requirements.
First, we add an image in the background and add other widgets on the top.
On the screen, we can see the beautiful login page having an image in the background.
<FrameLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="32dp"
android:background="@color/white"
tools:context=".MainActivity">
<TextView
<EditText
android:id="@+id/textView"
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:layout_marginTop="156dp"
android:layout_gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:background="@color/green"
android:background="@color/grey"
android:padding="16dp"
android:textColorHint="@color/white"
android:text="Login Details"
android:hint="Enter your email"
android:textColor="#FFFFFF"
android:padding="10dp" />
android:textSize="20sp" />
<EditText <Button
android:id="@+id/editText2"
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="300dp"
android:layout_marginTop="220dp"
android:backgroundTint="@color/green"
android:background="@color/grey"
android:textColorHint="@color/white" android:layout_gravity="center_horizontal"
import android.os.Bundle;
protected void onCreate(Bundle
import android.widget.EditText;
savedInstanceState) {
import android.widget.TextView;
super.onCreate(savedInstanceState);
import
androidx.appcompat.app.AppCompat setContentView(R.layout.activity_main);
Activity;
// finding the UI elements
public class MainActivity
extends AppCompatActivity { textView = findViewById(R.id.textView);
EditText editText1,
editText2 = findViewById(R.id.editText2)
editText2;
@Override } }
Table Layout
● A layout that arranges its children into rows and columns.
● A Table Layout consists of a number of TableRow objects, each defining a row
(actually, you can have other children, which will be explained below).
● Table Layout containers do not display border lines for their rows, columns, or
cells.
● Each row has zero or more cells; each cell can hold one View object.
● The table has as many columns as the row with the most cells.
● A table can leave cells empty. Cells can span columns, as they can in HTML.
● The width of a column is defined by the row with the widest cell in that
column.
However, a Table Layout can specify certain columns as shrinkable or stretchable
by callingn setColumnShrinkable() or setColumnStretchable().
If marked as shrinkable, the column width can be shrunk to fit the table into its
parent object.
android:background="@color/green" android:layout_width="wrap_content"
android:padding="16dp"> android:layout_height="wrap_content"/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:id="@+id/main" android:text="ICC Ranking of Players:"
android:padding="32dp"
android:textSize = "20sp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="bold">
android:background="@color/white" </TextView>
tools:context=".MainActivity">
<TableRow <TextView
android:layout_width="wrap_content"
android:background="@color/green" android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"> android:gravity="center"
android:text="Player"
<TextView android:textStyle="bold" />
android:layout_width="wrap_content"
<TextView
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_weight="1"
android:gravity="center"
android:gravity="center" android:text="Team"
android:textStyle="bold" />
android:text="Rank"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Points"
android:textStyle="bold" />
</TableRow>
ConstraintLayout
It is the latest layout which was introduced in 2016 and supports android
2.3 or higher version .
It is a viewgroup which allows us to create large and complex layouts
with a flat view hierarchy.
Its main advantage is that it reduces the nesting of view or viewgroup
which in order improves the performance.
It is very similar to relativelayout because it also makes the relationship
between multiple views.
Advantages of using ConstraintLayout in Android
● ConstraintLayout provides you the ability to completely design your UI with the drag-and-drop feature
provided by the Android Studio design editor.
● It helps to improve the UI performance over other layouts.
● With the help of ConstraintLayout, we can control the group of widgets through a single line of code.
● It helps to manage the position and size of views without using nested layouts which improves
performance and makes it easier.
● ConstraintLayout is more efficient than Linear or Relative layouts, especially for complex layouts, because
ConstraintLayout uses a more efficient algorithm to calculate the positions and sizes of widgets.
● When we use the Constraint Layout in our app, the XML code generated becomes a bit difficult to
understand.
● In most of the cases, the result obtain will not be the same as we got to see in the design editor.
● Sometimes we have to create a separate layout file for handling the UI for the landscape mode.
GridLayout
● Let us not get confused with GridView and GridLayout to be the
same.
● GridView simply gives us a two-dimensional view to display the
items on the screen, under ViewGroup. On the other hand,
GridLayout is a layout manager that arranges the views in a grid.
● GridLayout basically places its children in a rectangular grid. This
grid has a set of a number of thin lines that separate the view area
into cells.
● Suppose you have a grid of N columns, then we will have N+1 grid
indices that would be starting from 0.
<GridLayout <ImageButton
xmlns:android= “http://schemas.android.com/apk/res/android”
android: id= “@+id/GridLayout” android:id="@+id/imageButton1"
android: layout_width= “match_parent”
android: layout_height= “match_parent” android:layout_width="160dp"
</GridLayout>
Example: android:layout_height="90dp"
● You can also create custom toast as well for example toast displaying image.
Toast class
Toast class is used to show notification for a particular interval of time. After sometime it disappears. It
doesn't block the user interaction.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"Hello Javatpoint",Toast.LENGTH_SHORT).show();
}
Android debugging
● The process of identifying and resolving issues with any Android application is
known as Android debugging.
● problems could arise from unexpected behaviour, faulty UI, logic errors, or
application failures.
● When developing an app, debugging might take place locally, in coding
environments, during quality assurance (QA) when errors are found, or in live
environments when users report difficulties.
● Typically, to debug an Android device, one needs to use debugging tools, create
breakpoints, log, or use a monitoring tool.
various methods of Android debugging
● Logcat: All log messages generated by the Android application are shown by Logcat.
To concentrate on certain problems, the logs can be filtered using tags, levels, and
other parameters. Several log levels are available off-the-shelf for printing messages
with logcat. These consist of functions such as Log.d(), Log.e(), and Log.i().
● Breakpoints: Breakpoints can be included in the code when debugging locally in
programmes such as Android Studio. The developer can step into or leap through the
code to move line by line and check the values of various variables, etc. in real time,
as the app reaches the breakpoint, which stops the workflow from executing. To
debug applications, conditional breakpoints can also be added.
● ADB(Android Debug Bridge): ADB is an effective built-in utility that developers can
use with a command line tool.
● DDMS (Dalvik Debug Monitor Service):This programme, which is a part of the ADB
Suite, allows users to take screenshots and monitor network activity.
● Android Profiler: Most IDEs, including Android Studio, have a profiler to help with
understanding CPU and local memory problems. Android Profiler also facilitates
quicker debugging of certain problems.
● Testing: In-depth testing of UT and QA bugs aids in problem understanding
duplicate.
most common issues in Android
NullPointerException:
Error Example (Java):
ArrayIndexOutOfBoundsException:
int[] numbers = {1, 2, 3};int index = 3;if (index >= 0 && index < numbers.length) {int value = numbers[index];}
IllegalStateException: