Mobile Application Development Complete Notes For Mid Term-1
Mobile Application Development Complete Notes For Mid Term-1
Android is an open-source operating system based on Linux with a Java programming interface for
mobile devices such as Smartphone (Touch Screen Devices who supports Android OS) as well for
Tablets too.
Android was developed by the Open Handset Alliance (OHA), which is led by Google. The Open
Handset Alliance (OHA) is a consortium of multiple companies like Samsung, Sony, Intel and many
more to provide services and deploy handsets using the android platform.
In 2007, Google released a first beta version of the Android Software Development Kit (SDK) and the
first commercial version of Android 1.0 (with name Alpha), was released in September 2008.
In 2012, Google released another version of android, 4.1 Jelly Bean. It’s an incremental update and
it improved a lot in terms of the user interface, functionality, and performance.
In 2014, Google announced another Latest Version, 5.0 Lollipop. In Lollipop version Google
completely revamped the UI by using Material Designs, which is good for the User Interface as well
for the themes related.
Android Features
Android is a powerful open-source operating system which provides a lot of great features, those are
Android History
Initially, Google launched the first version of Android platform on Nov 5, 2007, from that onwards
Google released a lot of android versions under a codename based on desserts, such as Apple Pie,
Banana Bread, Cupcake, Donut, Éclair, Froyo, Gingerbread, Jellybeans, Kitkat, Lollipop, marshmallow,
etc. and made a lot of changes and additions to the android platform.
The following table lists the different version details of android which is released by Google from
2007 to till date.
Android 8.0 26 O
For each version, Google has made a lot of changes and introduced a lot of new features due to the
usage of android in the mobile market increased drastically.
Android OS Market Value
Following is the pictorial representation of using an android in the mobile phone market with
different versions.
This is how Google released multiple versions of the android operating system and acquired a huge
mobile phone market share with different versions.
Android Architecture
Android architecture is a software stack of components to support mobile device needs. Android
software stack contains a Linux Kernel, collection of c/c++ libraries which are exposed through an
application framework services, runtime, and application.
1. Applications
2. Android Framework
3. Android Runtime
4. Platform Libraries
5. Linux Kernel
In these components, the Linux Kernel is the main component in android to provide its operating
system functions to mobile and Dalvik Virtual Machine (DVM) which is responsible for running a
mobile application.
The application layer runs within the Android run time using the classes and services made available
from the application framework.
Application Framework
The Application Framework provides the classes used to create Android applications. It also
provides a generic abstraction for hardware access and manages the user interface and application
resources. It basically provides the services through which we can create a particular class and make
that class helpful for the Application creation.
The application framework includes services like telephony service, location services, notification
manager, NFC service, view system, etc. which we can use for application development as per our
requirements.
Android Runtime
Android Runtime environment is an important part of Android rather than an internal part and it
contains components like core libraries and the Dalvik virtual machine. The Android run time is
the engine that powers our applications along with the libraries and it forms the basis for the
application framework.
Dalvik Virtual Machine (DVM) is a register-based virtual machine like Java Virtual Machine (JVM). It
is specially designed and optimized for android to ensure that a device can run multiple instances
efficiently. It relies on the Linux kernel for threading and low-level memory management.
The core libraries in android runtime will enable us to implement android applications using
standard JAVA programming language.
Platform Libraries
The Platform Libraries includes various C/C++ core libraries and Java-based libraries such as SSL,
libc, Graphics, SQLite, Webkit, Media, Surface Manger, OpenGL, etc. to provide support for Android
development.
The following are the summary details of some core android libraries available for android
development.
• Media library for playing and recording audio and video formats
• The Surface manager library to provide a display management
• SGL and OpenGL Graphics libraries for 2D and 3D graphics
• SQLite is for database support and FreeType for font support
• Web-Kit for web browser support and SSL for Internet security.
Linux Kernel
Linux Kernel is a bottom layer and heart of the android architecture. It manages all the drivers such
as display drivers, camera drivers, Bluetooth drivers, audio drivers, memory drivers, etc. which are
mainly required for the android device during the runtime.
The Linux Kernel will provide an abstraction layer between the device hardware and the remainder of
the stack. It is responsible for memory management, power management, device management,
resource access, etc.
Every app project must have an AndroidManifest.xml file (with precisely that name) at the
root of the project source set. The manifest file describes essential information about
your app to the Android build tools, the Android operating system, and Google Play.
Among many other things, the manifest file is required to declare the following:
• The components of the app, which include all activities, services, broadcast receivers,
and content providers. Each component must define basic properties such as the name
of its Kotlin or Java class. It can also declare capabilities such as which device
configurations it can handle, and intent filters that describe how the component can be
started. Read more about app components.
• The permissions that the app needs in order to access protected parts of the system or
other apps. It also declares any permissions that other apps must have if they want to
access content from this app. Read more about permissions.
• The hardware and software features the app requires, which affects which devices can
install the app from Google Play. Read more about device compatibility.
If you're using Android Studio to build your app, the manifest file is created for you, and
most of the essential manifest elements are added as you build your app (especially
when using code templates).
A build system like Gradle is not a compiler, linker etc, but it controls and
supervises the operation of compilation, linking of files, running test cases, and
eventually bundling the code into an apk file for your Android Application.
The following are the basic core application components that can be used in Android application.
• Activities
• Intents
• Content Providers
• Broadcast Receivers
• Services
All these application components are defined in the android app description file
(AndroidMainfest.xml) like as shown below.
Android Activities
In android, Activity represents a single screen with a user interface (UI) and it will acts an entry point
for the user’s to interact with app.
For example, a contacts app that is having multiple activities like showing a list of contacts, add a
new contact, and another activity to search for the contacts. All these activities in the contact app are
independent of each other but will work together to provide a better user experience.
Android Intents
In android, Intent is a messaging object which is used to request an action from another component.
• Starting an Activity
• Starting a Service
• Delivering a Broadcast
There are two types of intents available in android, those are
1. Implicit Intents
2. Explicit Intents
To know more about Intents in android check this Android Intents.
Android Services
In android, Service is a component that keeps an app running in the background to perform long-
running operations based on our requirements. For Service, we don’t have any user interface and it
will run the apps in background like play music in background when the user in different app.
• Local Services
• Remote Services
To know more about Services in android check this Android Services.
Generally, we use Intents to deliver broadcast events to other apps and Broadcast Receivers use
status bar notifications to let the user know that broadcast event occurs.
To know more about Broadcast Receivers in android check this Android Broadcast Receivers.
By using Content Providers, other apps can query or modify the data of our app based on the
permissions provided by content provider. For example, android provides a Content Provider
(ContactsContract.Data) to manage contacts information, by using proper permissions any app can
query the content provider to perform read and write operations on contacts information.
To know more about Content Providers in android check this Android Content Provider.
Additional Components
In android, we have additional components which are used to build the relationship between the
above components (Activities, Intents, Content Providers, Services and Broadcast Receivers) to
implement our application logic, those are
Component Description
Fragments These are used to represent the portion of user interface in an activity
Layouts These are used to define the user interface (UI) for an activity or app
Views These are used to build a user interface for an app using UI elements like buttons, lists, etc.
Resources To build an android app we required external elements like images, audio files, etc. other than coding
Manifest It’s a configuration file (AndroidManifest.xml) for the application and it will contain the information
File about Activities, Intents, Content Providers, Services, Broadcast Receivers, permissions, etc.
These are the main application components which are required to build an android application
based on our requirements.
Generally, the android apps will contain multiple screens and each screen of our application will be
an extension of Activity class. By using activities, we can place all our android application UI
components in a single screen.
From the multiple activities in android app, one activity can be marked as a main activity and that is
the first screen to appear when we launch the application. In android app each activity can start
another activity to perform different actions based on our requirements.
For example, a contacts app which is having multiple activities, in that the main activity screen will
show a list of contacts and from the main activity screen we can launch other activities that provide
screens to perform tasks like add a new contact and search for the contacts. All these activities in the
contact app are loosely bound to other activities but will work together to provide a better user
experience.
Generally, in android there is a minimal dependency between the activities in an app. To use
activities in application we need to register those activities information in our app’s manifest file
(AndroidMainfest.xml) and need to manage activity life cycle properly.
By using activity cal-back methods we can define how our activity can behave when the user enter or
leaves our application.
Android Activity Lifecycle Callback Methods
In android, an activity goes through a series of states during its lifetime. By using callback methods
we can get the activity transitions between the states.
Android system initiates its program within an Activity starting with a call on onCreate() callback
method. There is a sequence of callback methods that start up an activity and a sequence of callback
methods that tear down an activity.
This section will give you detailed information about callback methods to handle activity transitions
between states during the lifecycle.
onCreate()
This is the first callback method and it fires when the system creates an activity for the first time.
During the activity creation, activity entered into a Created state.
If we have an application start-up logic that needs to perform only once during the life cycle of an
activity, then we can write that logic in onCreate() method.
Following is the example of defining a onCreate() method in android activity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Once onCreate() method execution is finished, the activity will enter into Started state and the
system calls the onStart() method.
onStart()
The onStart() callback method will invoke when an activity entered into Started State by
completing onCreate() method. The onStart() method will make an activity visible to the user and
this method execution will finish very quickly.
@Override
protected void onStart()
{
super.onStart()
}
After completion of onStart() method execution, the activity enters into Resumed state and system
invoke the onResume() method.
onResume()
When an activity entered into Resumed state, the system invokes onResume() call back method. In
this state activity start interacting with the user that means user can see the functionality and
designing part of an application on the single screen.
The app will stay in this Resumed state until an another activity happens to take focus away from the
app like getting a phone call or screen turned off, etc.
In case if any interruption events happen in Resumed state, the activity will enter into Paused state
and the system will invoke onPause() method.
After an activity returned from Paused state to Resumed state, the system again will
call onResume() method due to this we need to implement onResume() method to initialize the
components that we release during onPause() method
@Override
public void onResume() {
super.onResume();
if (mCamera == null) {
initializeCamera();
}
}
If any interruption happens in Resumed state, the activity will enter into Paused state and the
system will invoke onPause() method.
onPause()
Whenever the user leaves an activity or the current activity is being Paused then the system
invokes onPause() method. The onPause() method is used to pause operations like stop playing the
music when the activity is in a paused state or pass an activity while switching from one app to
another app because every time only one app can be focused.
@Override
public void onPause() {
super.onPause();
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
After completion of onPause() method execution, the next method is
either onStop() or onResume() depending on what happens after an activity entered into
a Paused state.
onStop()
The system will invoke onStop() callback method when an activity no longer visible to the user, the
activity will enter into Stopped state. This happens due to current activity entered
into Resumed state or newly launched activity covers complete screen or it’s been destroyed.
The onStop() method is useful to release all the app resources which are no longer needed to the
user.
@Override
protected void onStop()
{
super.onStop();
}
The next callback method which raised by the system is either onRestart(), in case if the activity
coming back to interact with the user or onDestroy(), in case if the activity finished running.
onRestart()
The system will invoke onRestart() method when an activity restarting itself after stopping it.
The onRestart() method will restore the state of activity from the time that is being stopped.
The onRestart() callback method in android activity will always be followed by onStart() method.
onDestroy()
The system will invoke onDestroy() method before an activity is destroyed and this is the final
callback method received by the android activity.
The system will invoke this onDestory() callback method either the activity is finishing or system
destroying the activity to save space.
@Override
public void onDestroy()
{
super.onDestroy();
}
The onDestroy() method will release all the resources which are not released by previous
callback onStop() method.
Following is the pictorial representation of the Android Activity Life cycle which shows how Activity
will behave in different stages using callback methods.
Whenever the user trying to leave an activity like switching from one app to another app, the system
will use callback methods to dismantle the activity completely or partially to resume the activity from
where the user left off.
Based on our requirements we can implement the activity in the android app using the callback
method and it’s not necessary to use all callback methods in each android application.
The user interface in an android app is made with a collection of View and ViewGroup objects.
Generally, the android apps will contain one or more activities and each activity is a one screen of
app. The activities will contain a multiple UI components and those UI components are the instances
of View and ViewGroup subclasses.
The user interface in an android app is made with a collection of View and ViewGroup objects.
Generally, the android apps will contain one or more activities and each activity is a one screen of the
app. The activities will contain multiple UI components and those UI components are the instances
of View and ViewGroup subclasses.
Android View
The View is a base class for all UI components in android. For example, the EditText class is used to
accept the input from users in android apps, which is a subclass of View.
Following are the some of common View subclasses that will be used in android applications.
• TextView
• EditText
• Button
• CheckBox
• RadioButton
• ImageButton
Android ViewGroup
The ViewGroup is a subclass of View and it will act as a base class for layouts and layouts
parameters. The ViewGroup will provide an invisible containers to hold
other Views or ViewGroups and to define the layout properties.
For example, Linear Layout is the ViewGroup that contains a UI controls like button, textview, etc. and
other layouts also.
• Linear Layout
• Relative Layout
• Table Layout
• Frame Layout
• Web View
• List View
• Grid View
Both View and ViewGroup subclasses together will play a key role to create a layouts in android
applications.
In android, we can specify the linear layout orientation using android:orientation attribute.
In LinearLayout, the child View instances arranged one by one, so the horizontal list will have only
one row of multiple columns and vertical list will have one column of multiple rows.
Android LinearLayout Declaration
Following is the way we need to define the LinearLayout in android applications.
ConstraintLayout in Android
ConstraintLayout is similar to that of other View Groups which we have seen in
Android such as RelativeLayout, LinearLayout, and many more. In this article, we
will take a look at using ConstraintLayout in Android.
Important Attributes of ConstraintLayout
Attributes Description
Following is the pictorial representation of user interface (UI) or input controls in android application.
The View is a base class for all UI components in android and it is used to create interactive UI
components such as TextView, EditText, Checkbox, Radio Button, etc. and it is responsible for event
handling and drawing.
The ViewGroup is a subclass of View and it will act as a base class for layouts and layout
parameters. The ViewGroup will provide invisible containers to hold other Views or ViewGroups and
to define the layout properties.
To know more about View and ViewGroup in android applications, check this Android View and
ViewGroup.
Following is the example of defining UI controls (TextView, EditText, Button) in the XML file
(activity_main.xml) using LinearLayout.
Note: we need to create a user interface (UI) layout files in /res/layout project directory, then only
the layout files will compile properly.
Load XML Layout File from an Activity
Once we are done with the creation of layout with UI controls, we need to load the XML layout
resource from our activity onCreate() callback method like as shown below.
Generally, during the launch of our activity, onCreate() callback method will be called by android
framework to get the required layout for an activity.
Create UI Element at Runtime
If we want to create UI elements at runtime, we need to create our own
custom View and ViewGroup objects programmatically with required layouts.
Following is the example of creating UI elements (TextView, EditText, Button) in LinearLayout using
custom View and ViewGroup objects in an activity programmatically.
Following is the example of setting width and height for View and ViewGroup elements in the XML
layout file.
• match_parent
• wrap_content
If we set value match_parent, then the View or ViewGroup will try to match with parent width or
height.
If we set value wrap_content, then the View or ViewGroup will try to adjust its width or height
based on the content.
Android Different Types of UI Controls
We have a different type of UI controls available in android to implement the user interface for our
android applications.
• TextView
• EditText
• AutoCompleteTextView
• Button
• ImageButton
• ToggleButton
• CheckBox
• RadioButton
• RadioGroup
Android TextView
In android, TextView is a user interface control that is used to display the text to the user.
To know more about TextView control check this, Android TextView with Examples.
Android EditText
In android, EditText is a user interface control which is used to allow the user to enter or modify the
text.
To know more about EditText, check this Android EditText with Examples.
Android Button
In android, Button is a user interface control that is used to perform an action when the user clicks
or tap on it.
To know more about Button in android check this, Android Buttons with Examples.
Android Image Button
In android, Image Button is a user interface control that is used to display a button with an image to
perform an action when the user clicks or tap on it.
Generally, the Image button in android looks similar as regular Button and perform the actions same
as regular button but only difference is for image button we will add an image instead of text.
To know more about Image Button in android check this, Android Image Button with Examples.
Android Toggle Button
In android, Toggle Button is a user interface control that is used to display ON (Checked) or OFF
(Unchecked) states as a button with a light indicator.
To know more about Toggle Button in android check this, Android Toggle Button with Examples.
Android CheckBox
In android, Checkbox is a two-states button that can be either checked or unchecked.
To know more about CheckBox in android check this, Android CheckBox with Examples.
Android Radio Button
In android, Radio Button is a two-states button that can be either checked or unchecked and it
cannot be unchecked once it is checked.
To know more about Radio Button in android check this, Android Radio Button with Examples.
Android Radio Group
In android, Radio Group is used to group one or more radio buttons into separate groups based on
our requirements.
In case if we group radio buttons using the radio group, at a time only one item can be selected
from the group of radio buttons.
To know more about Radio Group in android check this, Android Radio Group with Examples.
In android, we can create a TextView control in two ways either in XML layout file or create it
in Activity file programmatically.
Create a TextView in Layout File
Following is the sample way to define TextView control in XML layout file in android application.
Following is the example to set the text of TextView control while declaring it in the XML Layout file.
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Tutlane" />
If you observe above example we used android:text property to the set required text for TextView
control in XML Layout file.
Following is another way to set the text of textview control programmatically in activity file
using setText() method.
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText("Welcome to Tutlane");
If you observe above code snippet, we are getting the TextView control which we defined in XML
layout file using id property and setting the text using setText() method.
Android TextView Attributes
The following are some of the commonly used attributes related to TextView control in android
applications.
Attribute Description
android:autoLink It will automatically found and convert URLs and email addresses as clickable links.
android: ems It is used to make the textview be exactly this many ems wide.
android:gravity It is used to specify how to align the text by the view's x and y-axis.
android:maxWidth It is used to make the TextView be at most this many pixels wide.
android:minWidth It is used to make the TextView be at least this many pixels wide.
android:typeface It is used to specify the Typeface (normal, sans, serif, monospace) for the text.
android:inputType It is used to specify the type of text being placed in text fields.
Create a new android application using android studio and give names as TextViewExample. In case
if you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?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:layout_marginTop="10dp"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="Welcome to Tutlane"
android:textColor="#86AD33"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:textAllCaps="true" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Tutlane"
android:textStyle="bold"
android:textColor="#fff"
android:background="#7F3AB5"
android:layout_marginBottom="15dp"/>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="email|web"
android:text="For more details visit http://tutlane.com and send m
ail to [email protected]" />
</LinearLayout>
Once we are done with creation of layout with required controls, we need to load the XML layout
resource from our activity onCreate() callback method, for that open
main activity file MainActivity.java from \java\com.tutlane.textviewexample path and write the
code like as shown below.
MainActivity.java
package com.tutlane.textviewexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
Generally, during the launch of our activity, the onCreate() callback method will be called by the
android framework to get the required layout for an activity.
Output of Android TextView Example
When we run the above example using the android virtual device (AVD) we will get a result like as
shown below.
This is how we can use TextView control in android applications to set and display the text based on
our requirements.
For example, if it accept plain text, then we need to specify the inputType as “text”. In case
if EditText field is for password, then we need to specify the inputType as “textPassword”.
In android, EditText control is an extended version of TextView control with additional features and
it is used to allow users to enter input values.
In android, we can create EditText control in two ways either in XML layout file or create it
in Activity file programmatically.
Create a EditText in Layout File
Following is the sample way to define EditText control in XML layout file in android application.
Following is the example to set the text of TextView control while declaring it in XML Layout file.
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to Tutlane" />
If you observe above example we used android:text property to the set required text
for EditText control in XML Layout file.
Following is another way to set the text of EditText control programmatically in activity file using
setText() method.
EditText et = (EditText)findViewById(R.id.editText1);
et.setText("Welcome to Tutlane");
If you observe above code snippet, we are finding the EditText control which we defined in XML
layout file using id property and setting the text using setText() method.
Get Text of Android EditText
In android, we can get the text of EditText control by using getText() method in Activity file.
Following is the example to get text of EditText control programmatically in activity file
using getText() method.
If you observe above code snippet, we are finding the EditText control which we defined in XML
layout file using id property and getting the text of EditText control using getText() method.
Android EditText Attributes
The following are some of the commonly used attributes related to EditText control in android
applications.
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:background It is used to set the background color for edit text control
android:ems It is used to make the textview be exactly this many ems wide.
android:maxWidth It is used to make the TextView be at most this many pixels wide.
android:minWidth It is used to make the TextView be at least this many pixels wide.
android:typeface It is used to specify the Typeface (normal, sans, serif, monospace) for the text.
android:inputType It is used to specify the type of text being placed in text fields.
Attribute Description
android:editable If we set false, EditText won't allow us to enter or modify the text
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?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="40dp"
android:orientation="vertical" android:id="@+id/linearlayout" >
<EditText
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Name"
android:inputType="text"
android:selectAllOnFocus="true" />
<EditText
android:id="@+id/txtPwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password 0 to 9"
android:inputType="numberPassword" />
<EditText
android:id="@+id/txtEmai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/txtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText3"
android:ems="10"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="@+id/txtPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Phone Number"
android:inputType="phone"
android:textColorHint="#FE8DAB"/>
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:textSize="16sp"
android:textStyle="normal|bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/resultView"
android:layout_marginTop="25dp"
android:textSize="15dp"/>
</LinearLayout>
If you observe above code we created multiple EditText controls with different inputTypes, such as
password, email address, date, phone number, plain text.
Once we are done with the creation of layout with required controls, we need to load the XML layout
resource from our activity onCreate() callback method, for that open main activity
file MainActivity.java from \java\com.tutlane.edittextexample path and write the code like as
shown below.
MainActivity.java
package com.tutlane.edittextexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
Generally, during the launch of our activity, the onCreate() callback method will be called by the
android framework to get the required layout for an activity.
Output of Android EditText Example
When we run the above example using the android virtual device (AVD) we will get a result like as
shown below.
If you observe the above result, the system displaying an appropriate on-screen keyboard for
each EditText control based on the defined inputType attribute and displayed a message if we click
on the button without entering details in fields.
Once we enter details in all fields and click on Button we will get a result like as shown below.
This is how we can use EditText control in android applications to allow the user to enter or modify
the text based on our requirements.
Android Button with Examples
In android, Button is a user interface control that is used to perform an action whenever the user
clicks or tap on it.
Generally, Buttons in android will contain a text or an icon or both and perform an action when the
user touches it.
In android, we have a different type of buttons available to use based on our requirements, those
are ImageButton, ToggleButton, RadioButton.
In android, we can create a Button control in two ways either in the XML layout file or create it in
the Activity file programmatically.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
If you observe above code snippet, here we defined Button control in xml layout file.
Following is the example of creating Button control dynamically in the activity file.
In android, we can define a button click event in two ways either in the XML layout file or create it in
the Activity file programmatically.
The value of android:onClick attribute must be the name of the method which we need to call in
response to a click event and the Activity file which hosting XML layout must implement the
corresponding method.
Following is the example of defining a button click event using android:onClick attribute in an XML
layout file.
To define button click programmatically, create View.OnClickListener object and assign it to the
button by calling setOnClickListener(View.OnClickListener) like as shown below.
This is how we can handle button click events in android applications based on our requirements.
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:padding It is used to set the padding from left, right, top and bottom.
Create a new android application using android studio and give names as ButtonExample. In case if
you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="First Number" />
<EditText
android:id="@+id/firstNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Number"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/secondNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Add" />
</LinearLayout>
If you observe above code we created one Button, two TextView controls and two EditText controls
in XML Layout file.
Once we are done with the creation of layout with required control, we need to load the XML layout
resource from our activity onCreate() callback method, for that open main activity
file MainActivity.java from \java\com.tutlane.buttonexample path and write the code like as
shown below.
MainActivity.java
package com.tutlane.buttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Generally, during the launch of our activity, onCreate() callback method will be called by the android
framework to get the required layout for an activity.
By default, the ImageButton looks same as normal button and it performs an action when a user
clicks or touches it, but the only difference is we will add a custom image to the button instead of
text.
In android, we have different types of buttons available to use based on our requirements, those
are Button, ImageButton, ToggleButton, and RadioButton.
In android, we can add an image to the button by using <ImageButton> attribute android:src in
XML layout file or by using the setImageResource() method.
In android, we can create ImageButton control in two ways either in the XML layout file or create it in
the Activity file programmatically.
In android, we can define button click event in two ways either in XML layout file or create it
in Activity file programmatically.
The value of android:onClick attribute must be the name of method which we need to call in
response to a click event and the Activity file which hosting XML layout must implement the
corresponding method.
Following is the example of defining a button click event using android:onClick attribute in XML
layout file.
To define button click programmatically, create View.OnClickListener object and assign it to the
button by calling setOnClickListener(View.OnClickListener) like as shown below.
ImageButton btnAdd = (ImageButton)findViewById(R.id.addBtn);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
}
This is how we can handle ImageButton click events in android applications based on our
requirements.
Attribute Description
android:background It is used to set the background color for an image button control.
android:padding It is used to set the padding from left, right, top and bottom of the image button.
android:baseline It is used to set the offset of the baseline within the view.
Create a new android application using android studio and give names as ButtonExample. In case if
you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/l_layout">
<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="First Number"/>
<EditText
android:id="@+id/firstNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>
<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Number"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/secondNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<ImageButton
android:id="@+id/addBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:src="@drawable/add_icon" />
</LinearLayout>
If you observe above code we created one ImageButton, two TextView controls and
two EditText controls in XML Layout file.
Once we are done with the creation of layout with required control, we need to load the XML layout
resource from our activity onCreate() callback method, for that open main activity
file MainActivity.java from \java\com.tutlane.buttonexample path and write the code like as
shown below.
MainActivity.java
package com.tutlane.buttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
Generally, during the launch of our activity, the onCreate() callback method will be called by the
android framework to get the required layout for an activity.
This is how we can use ImageButton control in android applications to perform required operations
on ImageButton tap or click based on our requirements.
The ToggleButton is useful for the users to change the settings between two states
either ON or OFF. We can add a ToggleButton to our application layout by using
the ToggleButton object.
In android, we can create ToggleButton control in two ways either in the XML layout file or create it
in the Activity file programmatically.
If you observe above code snippet, here we defined ToggleButton control and setting ToggleButton
state ON using android:checked attribute in xml layout file.
Following is the example of creating ToggleButton control dynamically in the activity file.
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:textOn It is used to set the text when the toggle button is in the ON / Checked state.
android:textOff It is used to set the text when the toggle button is in the OFF / Unchecked state.
android:background It is used to set the background color for toggle button control.
android:padding It is used to set the padding from left, right, top and bottom.
Create a new android application using android studio and give names as ToggleButtonExample. In
case if you are not aware of creating an app in android studio check this article Android Hello World
App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ToggleButton
android:id="@+id/toggle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="120dp"
android:checked="true"
android:textOff="OFF"
android:textOn="ON"/>
<ToggleButton
android:id="@+id/toggle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/toggle1"
android:layout_toRightOf="@+id/toggle1"
android:textOff="OFF"
android:textOn ="ON"/>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="200dp"
android:text="Submit" />
</RelativeLayout>
If you observe above code we defined a two ToggleButton controls and one Button control
in RelativeLayout to get the state of ToggleButton controls when we click on Button control in XML
layout file.
Once we are done with the creation of layout with required control, we need to load the XML layout
resource from our activity onCreate() callback method, for that open main activity
file MainActivity.java from \java\com.tutlane.togglebuttonexample path and write the code like
as shown below.
MainActivity.java
package com.tutlane.togglebuttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;
Generally, during the launch of our activity, the onCreate() callback method will be called by android
framework to get the required layout for an activity.
This is how we can use ToggleButton control in android applications to switch the settings between
two states either ON or OFF based on our requirements.
Generally, we can use multiple CheckBox controls in android application to allow users to select one
or more options from the set of values.
By default, the android CheckBox will be in the OFF (Unchecked) state. We can change the default
state of CheckBox by using android:checked attribute.
In case, if we want to change the state of CheckBox to ON (Checked), then we need to
set android:checked = “true” in our XML layout file.
In android, we can create CheckBox control in two ways either in the XML layout file or create it in
the Activity file programmatically.
If you observe above code snippet, here we defined CheckBox control and setting CheckBox
state ON using android:checked attribute in xml layout file.
In android, we can define the CheckBox click event in two ways either in the XML layout file or
create it in the Activity file programmatically.
The value of android:onClick attribute must be the name of method which we need to call in
response to a click event and the Activity file which hosting XML layout must implement the
corresponding method.
Following is the example of defining a CheckBox click event using android:onClick attribute in XML
layout file.
To define checkbox click event programmatically, create View.OnClickListener object and assign it
to the button by calling setOnClickListener(View.OnClickListener) like as shown below.
This is how we can handle CheckBox click events in android applications based on our requirements.
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:padding It is used to set the padding from left, right, top and bottom.
android:onClick It’s the name of the method to invoke when the checkbox clicked.
Create a new android application using android studio and give names as CheckBoxExample. In case
if you are not aware of creating an app in android studio check this article Android Hello World App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/chkJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chkPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onCheckboxClicked"/>
<CheckBox
android:id="@+id/chkAngular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="AngularJS"
android:onClick="onCheckboxClicked"/>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Get Details" />
</LinearLayout>
If you observe above code we created a multiple CheckBox controls and one Button control in XML
Layout file.
Once we are done with the creation of layout with required controls, we need to load the XML layout
resource from our activity onCreate() callback method, for that open
main activity file MainActivity.java from \java\com.tutlane.checkboxexample path and write the
code like as shown below.
MainActivity.java
package com.tutlane.checkboxexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
Generally, during the launch of our activity, onCreate() callback method will be called by android
framework to get the required layout for an activity.
This is how we can use CheckBox control in android applications to allow users to select one or
more options based on our requirements.
The user can press or click on the radio button to make it select. In android, CheckBox control allow
users to change the state of control either Checked or Unchecked but the radio button cannot be
unchecked once it is checked.
Generally, we can use RadioButton controls in an android application to allow users to select only
one option from the set of values.
In android, we use radio buttons with in a RadioGroup to combine multiple radio buttons into one
group and it will make sure that users can select only one option from the group of multiple options.
By default, the android RadioButton will be in OFF (Unchecked) state. We can change the default
state of RadioButton by using android:checked attribute.
In android, we can define RadioButton click event in two ways either in the XML layout file or create
it in Activity file programmatically.
The value of android:onClick attribute must be the name of the method which we need to call in
response to a click event and the Activity file which hosting XML layout must implement the
corresponding method.
Following is the example of defining a RadioButton click event using android:onClick attribute in
XML layout file.
To define RadioButton click event programmatically, create View.OnClickListener object and assign
it to the button by calling setOnClickListener(View.OnClickListener) like as shown below.
This is how we can handle RadioButton click events in android applications based on our
requirements.
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
android:background It is used to set the background color for radio button control.
android:padding It is used to set the padding from left, right, top and bottom.
android:onClick It’s the name of the method to invoke when the radio button clicked.
Create a new android application using android studio and give names as RadioButtonExample. In
case if you are not aware of creating an app in android studio check this article Android Hello World
App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_paren
t">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Course"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
<RadioButton
android:id="@+id/rdbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAngular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="AngularJS"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_below="@+id/rdGroup"
android:text="Get Course" />
</RelativeLayout>
If you observe above code we created a multiple RadioButton controls, one TextView control and
one Button control in XML Layout file.
Once we are done with the creation of layout with required controls, we need to load the XML layout
resource from our activity onCreate() callback method, for that open
main activity file MainActivity.java from \java\com.tutlane.radiobuttonexample path and write
the code like as shown below.
MainActivity.java
package com.tutlane.radiobuttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
Generally, during the launch of our activity, onCreate() callback method will be called by the android
framework to get the required layout for an activity.
This is how we can use RadioButton control in android applications to allow users to select only one
option from a set of values based on our requirements.
If we group Radio Buttons using RadioGroup, at a time only one item can be selected from the
group of radio buttons. In case, if we select one radio button that belongs to a radio group will
unselect all other previously selected radio buttons within the same group.
Initially, all the radio buttons of the radio group are in the unchecked state, once we select a radio
button then it’s not possible for us to uncheck it like CheckBox control.
Generally, we use radio buttons within a RadioGroup to combine multiple Radio Buttons into one
group and it will make sure that user can select only one option from the group of multiple options.
Following is the sample way to define RadioButton control using RadioGroup in the XML layout file
in the android application.
Create a new android application using android studio and give names as RadioButtonExample. In
case if you are not aware of creating an app in android studio check this article Android Hello World
App.
Now open an activity_main.xml file from \res\layout path and write the code like as shown below
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_paren
t">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="100dp"
android:textSize="18dp"
android:text="Select Your Course"
android:textStyle="bold"
android:id="@+id/txtView"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/rdGroup"
android:layout_below="@+id/txtView">
<RadioButton
android:id="@+id/rdbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Java"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbPython"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Python"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="Android"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:id="@+id/rdbAngular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginLeft="100dp"
android:text="AngularJS"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
<Button
android:id="@+id/getBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_below="@+id/rdGroup"
android:text="Get Course" />
</RelativeLayout>
If you observe above code we created a multiple RadioButton controls in RadioGroup,
one TextView control and one Button control in XML Layout file.
Once we are done with the creation of layout with required controls, we need to load the XML layout
resource from our activity onCreate() callback method, for that open
main activity file MainActivity.java from \java\com.tutlane.radiogroupexample path and write
the code like as shown below.
MainActivity.java
package com.tutlane.radiobuttonexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
If you observe above code we are calling our layout using setContentView method in the form
of R.layout.layout_file_name in our activity file. Here our xml file name is activity_main.xml so we
used file name activity_main and we are getting the status of RadioButton controls when
they Select / Deselect and getting selected RadioButton control value on Button click.
Generally, during the launch of our activity, the onCreate() callback method will be called by the
android framework to get the required layout for an activity.
If you observe the above result, we are able to select only one option from a set of values and
getting the selected RadioButton value on Button click.
This is how we can use RadioButton control in RadioGroup to allow users to select only one option
from a set of values in android applications.