1. Explain Android platform architecture.ORWhat Is Android. Explain Android Software Stack.Android Architectureandroid architecture or Android software stack is categorized into five parts:
- linux kernel
- native libraries (middleware),
- Android Runtime
- Application Framework
- Applications
Let's see the android architecture first.6.1) Linux kernel
It is the heart of android architecture that exists at the root of android architecture. Linux kernel is responsible for device drivers, power management, memory management, device management and resource access.
2) Native Libraries
On the top of linux kernel, their are Native libraries such as WebKit, OpenGL, FreeType, SQLite, Media, C runtime library (libc) etc.The WebKit library is responsible for browser support, SQLite is for database, FreeType for font support, Media for playing and recording audio and video formats.
3) Android Runtime
In android runtime, there are core libraries and DVM (Dalvik Virtual Machine) which is responsible to run android application. DVM is like JVM but it is optimized for mobile devices. It consumes less memory and provides fast performance.
4) Android Framework
On the top of Native libraries and android runtime, there is android framework. Android framework includes Android API's such as UI (User Interface), telephony, resources, locations, Content Providers (data) and package managers. It provides a lot of classes and interfaces for android application development.
5) Applications
On the top of android framework, there are applications. All applications such as home, contact, settings, games, browsers are using android framework that uses android runtime and libraries. Android runtime and native libraries are using linux kernal.2. List and describe in detail Android development toolsThe android developer tools let you create interactive and powerful application for android platform. The tools can be generally categorized into two types.· SDK tools· Platform toolsSDK tools
SDK tools are generally platform independent and are required no matter which android platform you are working on. When you install the Android SDK into your system, these tools get automatically installed. The list of SDK tools has been given below −
Sr.No
|
Tool & description
|
1
|
android
This tool lets you manage AVDs,
projects, and the installed components of the SDK
|
2
|
ddms
This tool lets you debug
Android applications
|
3
|
Draw 9-Patch
This tool allows you to easily
create a NinePatch graphic using a WYSIWYG editor
|
4
|
emulator
This tools let you test your
applications without using a physical device
|
5
|
mksdcard
Helps you create a disk image
(external sdcard storage) that you can use with the emulator
|
6
|
proguard
Shrinks, optimizes, and
obfuscates your code by removing unused code
|
7
|
sqlite3
Lets you access the SQLite data
files created and used by Android applications
|
8
|
traceview
Provides a graphical viewer for
execution logs saved by your application
|
9
|
Adb
Android Debug Bridge (adb) is a
versatile command line tool that lets you communicate with an emulator
instance or connected Android-powered device.
|
We will discuss three important tools here that are android,ddms
and sqlite3.
Android
Android is a development tool that lets you perform these tasks:
· Manage
Android Virtual Devices (AVD)
· Create
and update Android projects
· Update
your sdk with new platform add-ons and documentation
android [global options] action [action options]
DDMS
DDMS stands for Dalvik debug monitor server, that provide many
services on the device. The service could include message formation, call
spoofing, capturing screenshot, exploring internal threads and file systems
e.t.c
Running DDMS
From Android studio click on Tools>Android>Android
device Monitor.
How it works
In android, each application runs in its own process and each
process run in the virtual machine. Each VM exposes a unique port, that a
debugger can attach to.
When DDMS starts, it connects to adb. When a device is connected,
a VM monitoring service is created between adb and DDMS, which notifies DDMS
when a VM on the device is started or terminated.
Making SMS
Making sms to emulator.we need to call telnet client and server as
shown below
Now click on send button, and you will see an sms notification in
the emulator window. It is shown below −
Making Call
In the DDMS, select the Emulator Control tab. In the emulator
control tab , click on voice and then start typing the incoming number. It is
shown in the picture below −
Now click on the call button to make a call to your emulator. It
is shown below −
Now click on hangup in the Android studio window to terminate the
call.
The fake sms and call can be viewed from the notification by just
dragging the notification window to the center using mouse. It is shown below −
Capturing ScreenShot
You can also capture screenshot of your emulator. For this look
for the camera icon on the right side under Devices tab. Just point your mouse
over it and select it.
As soon as you select it , it will start the screen capturing
process and will capture whatever screen of the emulator currently active. It
is shown below −
The eclipse orientation can be changed using Ctrl + F11 key. Now
you can save the image or rotate it and then select done to exit the screen
capture dialog.
Sqlite3
Sqlite3 is a command line program which is used to manage the
SQLite databases created by Android applications. The tool also allow us to
execute the SQL statements on the fly.
There are two way through which you can use SQlite , either from
remote shell or you can use locally.
Use Sqlite3 from a remote shell.
Enter a remote shell by entering the following command −
adb [-d|-e|-s {<serialNumber>}] shell
From a remote shell, start the sqlite3 tool by entering the
following command −
sqlite3
Once you invoke sqlite3, you can issue sqlite3 commands in the
shell. To exit and return to the adb remote shell, enter exit or press CTRL+D.
Using Sqlite3 directly
Copy a database file from your device to your host machine.
adb pull <database-file-on-device>
Start the sqlite3 tool from the /tools directory, specifying the
database file −
sqlite3 <database-file-on-host>
Platform tools
The platform tools are customized to support the features of the
latest android platform.
The platform tools are typically updated every time you install a
new SDK platform. Each update of the platform tools is backward compatible with
older platforms.
Some of the platform tools are listd below −
· Android
Debug bridge (ADB)
· Android
Interface definition language (AIDL)
· aapt,
dexdump , and dex e.t.c
3.
Explain the lifecycle of Android Activity OR
Explain Activity
Life-Cycle in android by giving a suitable
example.
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.
An activity is the single screen in android. It is like window or
frame of Java.
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.
Android Activity Lifecycle methods
Let's see the 7 lifecycle methods of android activity.
Method
|
Description
|
onCreate
|
called
when activity is first created.
|
onStart
|
called
when activity is becoming visible to the user.
|
onResume
|
called
when activity will start interacting with the user.
|
onPause
|
called
when activity is not visible to the user.
|
onStop
|
called
when activity is no longer visible to the user.
|
onRestart
|
called
after your activity is stopped, prior to start.
|
onDestroy
|
called
before the activity is destroyed.
|
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.javatpoint.com.activitylifecycle.MainActivity">
8.
9.
<TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="Hello World!"
13. app:layout_constraintBottom_toBottomOf="parent"
14. app:layout_constraintLeft_toLeftOf="parent"
15. app:layout_constraintRight_toRightOf="parent"
16. app:layout_constraintTop_toTopOf="parent" />
17.
18. </android.support.constraint.ConstraintLayout>
Android Activity Lifecycle
Example
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.javatpoint.com.activitylifecycle;
2.
3.
import android.app.Activity;
4.
import android.os.Bundle;
5.
import android.util.Log;
6.
7.
public class MainActivity extends Activity {
8.
9.
@Override
10. protected void onCreate(Bundle savedInstanceState) {
11. super.onCreate(savedInstanceState);
12. setContentView(R.layout.activity_main);
13. Log.d("lifecycle","onCreate invoked");
14. }
15. @Override
16. protected void onStart() {
17. super.onStart();
18. Log.d("lifecycle","onStart invoked");
19. }
20. @Override
21. protected void onResume() {
22. super.onResume();
23. Log.d("lifecycle","onResume invoked");
24. }
25. @Override
26. protected void onPause() {
27. super.onPause();
28. Log.d("lifecycle","onPause invoked");
29. }
30. @Override
31. protected void onStop() {
32. super.onStop();
33. Log.d("lifecycle","onStop invoked");
34. }
35. @Override
36. protected void onRestart() {
37. super.onRestart();
38. Log.d("lifecycle","onRestart invoked");
39. }
40. @Override
41. protected void onDestroy() {
42. super.onDestroy();
43. Log.d("lifecycle","onDestroy invoked");
44. }
45. }
4.
Write
detailed note on different types of layouts
Android Layout Types
There are number of Layouts provided by Android which you will use
in almost all the Android applications to provide different view, look and
feel.
Sr.No
|
Layout & Description
|
1
|
LinearLayout is a view group
that aligns all children in a single direction, vertically or horizontally.
|
2
|
RelativeLayout is a view group
that displays child views in relative positions.
|
3
|
TableLayout is a view that
groups views into rows and columns.
|
4
|
AbsoluteLayout enables you to
specify the exact location of its children.
|
5
|
The FrameLayout is a
placeholder on screen that you can use to display a single view.
|
6
|
ListView is a view group that
displays a list of scrollable items.
|
7
|
GridView is a ViewGroup that
displays items in a two-dimensional, scrollable grid.
|
|
|
Android
LinearLayout is a view group that aligns all children in either vertically or horizontally.
LINEAR LAYOUT
LinearLayout Attributes
Following are the important attributes specific to LinearLayout −
Sr.No
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:baselineAligned
This must be a boolean value,
either "true" or "false" and prevents the layout from
aligning its children's baselines.
|
3
|
android:baselineAlignedChildIndex
When a linear layout is part of
another layout that is baseline aligned, it can specify which of its children
to baseline align.
|
4
|
android:divider
This is drawable to use as a
vertical divider between buttons. You use a color value, in the form of
"#rgb", "#argb", "#rrggbb", or
"#aarrggbb".
|
5
|
android:gravity
This specifies how an object
should position its content, on both the X and Y axes. Possible values are
top, bottom, left, right, center, center_vertical, center_horizontal etc.
|
6
|
android:orientation
This specifies the direction of
arrangement and you will use "horizontal" for a row,
"vertical" for a column. The default is horizontal.
|
7
|
android:weightSum
Sum up of child weight
|
Following is the content of
the modified main activity file src/com.example.demo/MainActivity.java.
This file can include each of the fundamental lifecycle methods.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity
extends Activity
{
@Override
protected void
onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Following will be the
content of res/layout/activity_main.xml file −
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button android:id="@+id/btnStartService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="start_service"/>
<Button android:id="@+id/btnPauseService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="pause_service"/>
<Button android:id="@+id/btnStopService"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:text="stop_service"/>
</LinearLayout>
Following will be the
content of res/values/strings.xml to define two new constants
−
<?xml version="1.0"
encoding="utf-8"?>
<resources>
<string name="app_name">HelloWorld</string>
<string name="action_settings">Settings</string>
</resources>
RELATIVE LAYOUT
Android
RelativeLayout enables you to specify how child views are positioned relative
to each other. The position of each view can be specified as relative to
sibling elements or relative to the parent.
RELATIVE LAYOUT
RelativeLayout
Attributes
Following are the important attributes specific to RelativeLayout
−
Sr.No.
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:gravity
This specifies how an object
should position its content, on both the X and Y axes. Possible values are
top, bottom, left, right, center, center_vertical, center_horizontal etc.
|
3
|
android:ignoreGravity
This indicates what view should
not be affected by gravity.
|
Using RelativeLayout, 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 and few of the important
attributes are given below −
Sr.No.
|
Attribute & Description
|
1
|
android:layout_above
Positions the bottom edge of
this view above the given anchor view ID and must be a reference to another
resource, in the form "@[+][package:]type:name"
|
2
|
android:layout_alignBottom
Makes the bottom edge of this
view match the bottom edge of the given anchor view ID and must be a
reference to another resource, in the form
"@[+][package:]type:name".
|
3
|
android:layout_alignLeft
Makes the left edge of this
view match the left edge of the given anchor view ID and must be a reference
to another resource, in the form "@[+][package:]type:name".
|
4
|
android:layout_alignParentBottom
If true, makes the bottom edge
of this view match the bottom edge of the parent. Must be a boolean value,
either "true" or "false".
|
5
|
android:layout_alignParentEnd
If true, makes the end edge of
this view match the end edge of the parent. Must be a boolean value, either
"true" or "false".
|
6
|
android:layout_alignParentLeft
If true, makes the left edge of
this view match the left edge of the parent. Must be a boolean value, either
"true" or "false".
|
7
|
android:layout_alignParentRight
If true, makes the right edge
of this view match the right edge of the parent. Must be a boolean value,
either "true" or "false".
|
8
|
android:layout_alignParentStart
If true, makes the start edge
of this view match the start edge of the parent. Must be a boolean value,
either "true" or "false".
|
9
|
android:layout_alignParentTop
If true, makes the top edge of
this view match the top edge of the parent. Must be a boolean value, either
"true" or "false".
|
10
|
android:layout_alignRight
Makes the right edge of this
view match the right edge of the given anchor view ID and must be a reference
to another resource, in the form "@[+][package:]type:name".
|
11
|
android:layout_alignStart
Makes the start edge of this
view match the start edge of the given anchor view ID and must be a reference
to another resource, in the form "@[+][package:]type:name".
|
12
|
android:layout_alignTop
Makes the top edge of this view
match the top edge of the given anchor view ID and must be a reference to
another resource, in the form "@[+][package:]type:name".
|
13
|
android:layout_below
Positions the top edge of this
view below the given anchor view ID and must be a reference to another
resource, in the form "@[+][package:]type:name".
|
14
|
android:layout_centerHorizontal
If true, centers this child
horizontally within its parent. Must be a boolean value, either "true"
or "false".
|
15
|
android:layout_centerInParent
If true, centers this child
horizontally and vertically within its parent. Must be a boolean value,
either "true" or "false".
|
16
|
android:layout_centerVertical
If true, centers this child
vertically within its parent. Must be a boolean value, either
"true" or "false".
|
17
|
android:layout_toEndOf
Positions the start edge of
this view to the end of the given anchor view ID and must be a reference to
another resource, in the form "@[+][package:]type:name".
|
18
|
android:layout_toLeftOf
Positions the right edge of
this view to the left of the given anchor view ID and must be a reference to
another resource, in the form "@[+][package:]type:name".
|
19
|
android:layout_toRightOf
Positions the left edge of this
view to the right of the given anchor view ID and must be a reference to
another resource, in the form "@[+][package:]type:name".
|
20
|
android:layout_toStartOf
Positions the end edge of this
view to the start of the given anchor view ID and must be a reference to
another resource, in the form "@[+][package:]type:name".
|
Following is the content of the modified main activity
file src/com.example.demo/MainActivity.java. This file can include
each of the fundamental lifecycle methods.
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
public class MainActivity
extends Activity {
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Following will be the content of res/layout/activity_main.xml file
−
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_below="@+id/name">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
/>
</LinearLayout>
</RelativeLayout>
Following will be the content of res/values/strings.xml to
define two new constants −
<?xml
version="1.0" encoding="utf-8"?>
<resources>
<string
name="action_settings">Settings</string>
<string
name="reminder">Enter your name</string>
</resources>
Let's try to run our modified Hello World! application
we just modified. I assume you had created your AVD while
doing environment setup. To run the app from Android Studio, open one of your
project's activity files and click Run icon
from the toolbar. Android Studio installs the app on your AVD and starts it and
if everything is fine with your setup and application, it will display
following Emulator window −
TableLayout
Android
TableLayout going to be arranged groups of views into rows and columns. You
will use the <TableRow> element to build a row in the table. Each row has
zero or more cells; each cell can hold one View object.
TableLayout containers do not display border lines for their rows,
columns, or cells.
TableLayout Attributes
Following are the important attributes specific to TableLayout −
Sr.No.
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:collapseColumns
This specifies the zero-based
index of the columns to collapse. The column indices must be separated by a
comma: 1, 2, 5.
|
3
|
android:shrinkColumns
The zero-based index of the
columns to shrink. The column indices must be separated by a comma: 1, 2, 5.
|
4
|
android:stretchColumns
The zero-based index of the
columns to stretch. The column indices must be separated by a comma: 1, 2, 5.
|
ABSOLUTE LAYOUT
An
Absolute Layout lets you specify exact locations (x/y coordinates) of its
children. Absolute layouts are less flexible and harder to maintain than other
types of layouts without absolute positioning.
ABSOLUTE LAYOUT
AbsoluteLayout
Attributes
Following are the important attributes specific to AbsoluteLayout
−
Sr.No
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:layout_x
This specifies the x-coordinate
of the view.
|
3
|
android:layout_y
This specifies the y-coordinate
of the view.
|
Public Constructors
AbsoluteLayout(Context
context)
|
AbsoluteLayout(Context
context, AttributeSet attrs)
|
AbsoluteLayout(Context
context, AttributeSet attrs, int defStyleAttr)
|
AbsoluteLayout(Context
context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
|
FRAME LAYOUT
Frame
Layout is designed to block out an area on the screen to display a single item.
Generally, FrameLayout 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 FrameLayout and control
their position within the FrameLayout by assigning gravity to each child, using
the android:layout_gravity attribute.
FRAME LAYOUT
FrameLayout Attributes
Following are the important attributes specific to FrameLayout −
Sr.No
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:foreground
This defines the drawable to
draw over the content and possible values may be a color value, in the form
of "#rgb", "#argb", "#rrggbb", or
"#aarrggbb".
|
3
|
android:foregroundGravity
Defines the gravity to apply to
the foreground drawable. The gravity defaults to fill. Possible values are
top, bottom, left, right, center, center_vertical, center_horizontal etc.
|
4
|
android:measureAllChildren
Determines whether to measure
all children or just those in the VISIBLE or INVISIBLE state when measuring.
Defaults to false.
|
ListView
Android ListView is
a view which groups several items and display them in vertical scrollable list.
The list items are automatically inserted to the list using an Adapter that
pulls content from a source such as an array or database.
LIST VIEW
An adapter actually bridges between UI components and the data
source that fill data into UI Component. Adapter holds the data and send the
data to adapter view, the view can takes the data from adapter view and shows
the data on different views like as spinner, list view, grid view etc.
The ListView and GridView are
subclasses of AdapterView and they can be populated by binding
them to an Adapter, which retrieves data from an external source
and creates a View that represents each data entry.
Android provides several subclasses of Adapter that are useful for
retrieving different kinds of data and building views for an AdapterView ( i.e.
ListView or GridView). The common adapters are ArrayAdapter,Base
Adapter,CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter.
We will see separate examples for both the adapters.
ListView Attributes
Following are the important attributes specific to GridView −
Sr.No
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:divider
This is drawable or color to
draw between list items.
|
3
|
android:dividerHeight
This specifies height of the
divider. This could be in px, dp, sp, in, or mm.
|
4
|
android:entries
Specifies the reference to an
array resource that will populate the ListView.
|
5
|
android:footerDividersEnabled
When set to false, the ListView
will not draw the divider before each footer view. The default value is true.
|
6
|
android:headerDividersEnabled
When set to false, the ListView
will not draw the divider after each header view. The default value is true.
|
Android GridView shows
items in two-dimensional scrolling grid (rows & columns) and the grid items
are not necessarily predetermined but they automatically inserted to the layout
using a ListAdapter
GRID VIEW
An
adapter actually bridges between UI components and the data source that fill
data into UI Component. Adapter can be used to supply the data to like spinner,
list view, grid view etc.
The ListView and GridView are
subclasses of AdapterView and they can be populated by binding
them to an Adapter, which retrieves data from an external source
and creates a View that represents each data entry.
GridView Attributes
Following are the important attributes specific to GridView −
Sr.No
|
Attribute & Description
|
1
|
android:id
This is the ID which uniquely
identifies the layout.
|
2
|
android:columnWidth
This specifies the fixed width
for each column. This could be in px, dp, sp, in, or mm.
|
3
|
android:gravity
Specifies the gravity within
each cell. Possible values are top, bottom, left, right, center,
center_vertical, center_horizontal etc.
|
4
|
android:horizontalSpacing
Defines the default horizontal
spacing between columns. This could be in px, dp, sp, in, or mm.
|
5
|
android:numColumns
Defines how many columns to
show. May be an integer value, such as "100" or auto_fit which
means display as many columns as possible to fill the available space.
|
6
|
android:stretchMode
Defines how columns should
stretch to fill the available empty space, if any. This must be either of the
values −
·
none − Stretching is disabled.
·
spacingWidth − The spacing between each
column is stretched.
·
columnWidth − Each column is stretched
equally.
·
spacingWidthUniform − The spacing between
each column is uniformly stretched..
|
7
|
android:verticalSpacing
Defines the default vertical
spacing between rows. This could be in px, dp, sp, in, or mm.
|
ð
Android Intent Tutorial
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.
Android intents are mainly
used to:
- Start the service
- Launch an activity
- Display a web page
- Display a list of contacts
- Broadcast a message
- Dial a phone call etc.
Types
of Android Intents
There
are two types of intents in android: implicit and explicit.
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.
1. Intent intent=new Intent(Intent.ACTION_VIEW);
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.
1. Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
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
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.javatpoint.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"
20. app:layout_constraintTop_toTopOf="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"
33. app:layout_constraintTop_toBottomOf="@+id/editText" />
34. </android.support.constraint.ConstraintLayout>
size=1 width="100%" noshade style='color:#D4D4D4' align=center>
Activity class
File: MainActivity.java
1. package example.javatpoint.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.
11. public class MainActivity extends AppCompatActivity {
12.
13. Button button;
14. EditText editText;
15.
16. @Override
17. protected void onCreate(Bundle savedInstanceState) {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_main);
20.
21. button = findViewById(R.id.button);
22. editText = findViewById(R.id.editText);
23.
24. button.setOnClickListener(new View.OnClickListener() {
25. @Override
26. public void onClick(View view) {
27. String url=editText.getText().toString();
28. Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
29. startActivity(intent);
30. }
31. });
32. }
33. }
No comments:
Post a Comment