You are on page 1of 22

MA QUES IMP

1. Activity life cycle

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.


2. Spinner
Spinner is a view similar to the dropdown list which is used to select one
option from the list of options. It provides an easy way to select one item
from the list of items and it shows a dropdown list of all values when we click
on it. The default value of the android spinner will be the currently selected
value and by using Adapter we can easily bind the items to the spinner
objects. Generally, we populate our Spinner control with a list of items by
using an ArrayAdapter in our Kotlin file. 
Different Attributes for Spinner Widget

XML attributes Description

android:id Used to specify the id of the view.

android:textAlignmen
t Used to the text alignment in the dropdown list.

android:background Used to set the background of the view.

android:padding Used to set the padding of the view.

android:visibility Used to set the visibility of the view.

android:gravity Used to specify the gravity of the view like center, top,
XML attributes Description

bottom, etc

Steps Description

1 You will use Android studio to create an Android application and name it as
AndroidSpinnerExample under a package com.example.spinner.

2 Modify src/AndroidSpinnerExampleActivity.java file to create a simple list view with


items which are showing as spinner items

3 Modify res/layout/activity_main.xml file to add respective XML code.

4 No need to define default string constants. Android studio takes care of default
string constants at string.xml

5 Run the application and choose a running android device and install the application
on it and verify the results.

3 .Intents
In Android, it is quite usual for users to witness a jump from one application to
another as a part of the whole process, for example, searching for a location on the
browser and witnessing a direct jump into Google Maps or receiving payment links
in Messages Application (SMS) and on clicking jumping to PayPal or GPay (Google
Pay). This process of taking users from one application to another is achieved by
passing the Intent to the system. Intents, in general, are used for navigating among
various activities within the same application, but note, is not limited to one single
application, i.e., they can be utilized from moving from one application to another
as well. 
Intents could be Implicit, for instance, calling intended actions and explicit as well,
such as opening another activity after some operations like onClick or anything
else. Below are some application of Intents:
1. Sending the User to Another App
2. Getting a Result from an Activity
3. Allowing Other Apps to Start Your Activity

Or

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:

o Start the service


o Launch an activity
o Display a web page
o Display a list of contacts
o Broadcast a message
o Dial a phone call etc.

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);  
3. View and view group
The View class is the base class or we can say that it is the superclass for all the GUI
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, and another example of the
TextView class which is used to display text labels in Android apps is also a subclass
of View. 
Or the other definition,
View refer to the android.view.View class, which is the base class of all UI classes.
android.view.View class is the root of the UI class hierarchy. So from an object
point of view, all UI objects are View objects. Following are some of the common
View subclasses that will be used in android applications.
 TextView
 EditText
 ImageView
 RadioButton
 Button
 ImageButton
 CheckBox
 DatePicker
 Spinner
 ProgressBar and etc.
These are some of the view subclass available in android. 
ViewGroup

The ViewGroup class is a subclass of the View class. And also it will act as a base
class for layouts and layouts parameters.  The ViewGroup will provide an invisible
container to hold other Views or ViewGroups and to define the layout properties.
For example, Linear Layout is the ViewGroup that contains UI controls like Button,
TextView, etc., and other layouts also. ViewGroup Refer to
the android.view.ViewGroup class, which is the base class of some special UI
classes that can contain other View objects as children. Since ViewGroup objects
are also View objects, multiple ViewGroup objects and View objects can be
organized into an object tree to build a complex UI structure. Following are the
commonly used ViewGroup subclasses used in android applications.
 FrameLayout
 WebView
 ListView
 GridView
 LinearLayout
 RelativeLayout
 TableLayout and many more.
The ViewGroup subclasses listed above group View instances together and takes
care of their layout. For instance, the LinearLayout will render the components
after each other either horizontally or vertically.

View ViewGroup

View is a simple
rectangle box that
responds to the user’s ViewGroup is the invisible container. It holds View and
actions. ViewGroup

View is the SuperClass


of All component like
TextView, EditText, ViewGroup is a collection of Views(TextView, EditText,
ListView, etc ListView, etc..), somewhat like a container.

A View object is a
component of the
user interface (UI) like
a button or a text box, A ViewGroup object is a layout, that is, a container of
and it’s also called a other ViewGroup objects (layouts) and View objects
widget. (widgets)

Examples are For example, LinearLayout is the ViewGroup that


EditText, Button, contains Button(View), and other Layouts also.
View ViewGroup

CheckBox, etc.

View refers to the


android.view.View
class ViewGroup refers to the android.view.ViewGroup class

android.view.View
which is the base class
of all UI classes. ViewGroup is the base class for Layouts.

4. Layouts
Android Layout is used to define the user interface that holds the UI controls or
widgets that will appear on the screen of an android application or activity screen.
Generally, every application is a combination of View and ViewGroup. As we know,
an android application contains a large number of activities and we can say each
activity is one page of the application. So, each activity contains multiple user
interface components and those components are the instances of the View and
ViewGroup. All the elements in a layout are built using a hierarchy
of View and ViewGroup objects.

View- A View is defined as the user interface which is used to create interactive UI
components such as TextView, ImageView, EditText, RadioButton, etc., and is
responsible for event handling and drawing. They are Generally Called Widgets.

A ViewGroup act as a base class for layouts and layouts parameters that hold other
Views or ViewGroups and to define the layout properties. They are Generally Called
layouts.
The Android framework will allow us to use UI elements or widgets in two ways:  
 Use UI elements in the XML file
 Create elements in the Kotlin file dynamically

Types of Layouts in Android

 Linear Layout
 Relative Layout
 Constraint Layout
 Table Layout
 Frame Layout
 List View
 Grid View
 Absolute Layout
 WebView
 ScrollView
These are the types of layouts and out of them we’ll learn about the two very
important layouts:

1. Linear Layout

We use this layout to place the elements in a linear manner. A Linear manner means
one element per line. This layout creates various kinds of forms on Android. In this,
arrangement of the elements is in a top to bottom manner.

This can have two orientations:


a. Vertical Orientation – It is shown above where the widgets such as TextViews, and
all in a vertical manner.
b. Horizontal Orientation – It is shown above where the widgets such as TextViews,
and all in a horizontal manner.
2. Relative Layout

This layout is for specifying the position of the elements in relation to the other
elements that are present there.

In the relative layout, alignment of the position of the elements to the parent
container is possible. To define it in such a way, we write the following:

 android:layout_alignParentTop= “true”
 android:layout_alignParentLeft= “true”
 Android Constraint Layout: ConstraintLayout is a ViewGroup subclass, used
to specify the position of layout constraints for every child View relative to
other views present. A ConstraintLayout is similar to a RelativeLayout, but
having more power.
 Android Frame Layout: FrameLayout is a ViewGroup subclass, used to
specify the position of View elements it contains on the top of each other to
display only a single View inside the FrameLayout.
 Android Table Layout: TableLayout is a ViewGroup subclass, used to display
the child View elements in rows and columns.
 Android Web View: WebView is a browser that is used to display the web
pages in our activity layout.
 Android ListView: ListView is a ViewGroup, used to display scrollable lists of
items in a single column.
 Android Grid View: GridView is a ViewGroup that is used to display a
scrollable list of items in a grid view of rows and columns.

Attributes of Layout in Android

The following are the attributes for customizing a Layout while defining it:

 android:id: It uniquely identifies the Android Layout.


 android:hint: It shows the hint of what to fill inside the EditText.
 android:layout_height: It sets the height of the layout.
 android:layout_width: It sets the width of the layout.
 android:layout_gravity: It sets the position of the child view.
 android:layout_marginTop: It sets the margin of the from the top of the layout.
 android:layout_marginBottom: It sets the margin of the from the bottom of the
layout.
 android:layout_marginLeft: It sets the margin of the from the left of the layout.
 android:layout_marginRight: It sets the margin of the from the right of the
layout.
 android:layout_x: It specifies the x coordinates of the layout.
 android:layout_y: It specifies the y coordinates of the layout.

6. Implicit & Explicit with peudo code


There are two types of intents in android: 
1. Implicit and
2. Explicit.
1. Implicit Intent
Implicit Intent doesn’t specify the component. In such a case, intent provides
information on available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);  
intent.setData(Uri.parse(“http://www.javatpoint.com”));  
startActivity(intent);  
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 ‘CLICK’ button. Your webpage is opened. 

 
2. Explicit Intent
Explicit Intent specifies the component. In such a case, intent provides the external
class to be invoked.
Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
startActivity(i);  
Example: 
In the below example, there are two activities (FirstActivity, 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.

7. Define UI testing & its steps

UI testing, also known as GUI testing, is a technique for testing the features of any
software that a user will interact with. This usually involves testing the visual
components to ensure that they are meeting the outlined  requirements - both in
terms of functionality and performance.

It involves testing all visual indicators and graphical icons, including menus, radio
buttons, text boxes, checkboxes, toolbars, colors, fonts, and more.

UI testing is performed manually or with an automated testing tool. Regardless of


the method used, the goal is to ensure all UI elements meet the requested
specifications.
The main aspects checked in UI testing include:

 Visual Design
 Functionality
 Usability
 Performance
 Compliance

 UI testing is centered around two main things. First, checking how the
application handles user actions carried out using the keyboard, mouse, and
other input devices. Second, checking whether visual elements are displayed
and working correctly.

 It is only by doing this that organizations can ensure that applications meet
their functional requirements and that end-users will successfully adopt them.
For this reason, UI testing plays a significant role before an application is
released to production.

There are three main GUI testing approaches, namely:

1. Manual Testing

In manual testing, a human tester performs a set of operations to check whether the
application is functioning correctly and that the graphical elements conform to the
documented requirements. Manual-based testing has notable downsides in that it
can be time-consuming, and the test coverage is extremely low. Additionally, the
quality of testing in this approach depends on the knowledge and capabilities of the
testing team.

2. Record-and-Playback Testing

Also known as record-and-replay testing, it is executed using automation tools. The


automated UI testing tool records all tasks, actions, and interactions with the
application. The recorded steps are then reproduced, executed, and compared with
the expected behavior. For further testing, the replay phase can be repeated with
various data sets.

3. Model-Based Testing

In this testing approach, we focus on building graphical models that describe the
behavior of a system. This provides a deeper understanding of the system, which
allows the tester to generate highly efficient test cases. In the models, we determine
the inputs and outputs of the system, which are in turn, used to run the tests. Model-
based testing works as follows:
 Create a model for the system
 Determine system inputs
 Verify the expected output
 Execute tests
 Check and validate system output vs. the expected output

The model-based approach is great because it allows a higher level of automation. It


also covers a higher number of states in the system, thereby improving the test
coverage.

Steps Description

1 You will use Android studio to create an Android application under a package
com.tutorialspoint.myapplication.

2 Modify src/MainActivity.java file to add Activity code.

3 Modify layout XML file res/layout/activity_main.xml add any GUI component if


required.

4 Create src/second.java file to add Activity code.

5 Modify layout XML file res/layout/view.xml add any GUI component if required.

6 Run the application and choose a running android device and install the application
on it and verify the results.

8. Explain material design wid eg


Material Design Components (MDC Android) offers designers and developers a way
to implement Material Design in their Android application. Developed by a core
team of engineers and UX designers at Google, these components enable a reliable
development workflow to build beautiful and functional Android applications.
Material design in Android is one of the key features that attracts and engages the
customer towards the application. This is a special type of design, which is guided
by Google. So in this article, it has been introduced to the basic things that need to
be considered before designing or developing any Materialistic Android
Application.
The topics covered in these articles are
1. Color and theming
2. Typography (Choosing the right font)
3. Material design components
4. Shaping the material design components
1. Colors and Theming
By choosing the right kind of color combination reflects the application’s brand and
style. For example, the application’s main or primary color is the Green, then in the
whole application, the green color will be frequently shown. Choosing the color for
the application, there are three types of colors to be chosen for developing the
android application.
 Primary Color: This color should be chosen very cautiously because this color is
frequently visible in the application components like high emphasis buttons or
the button ripple color, and also the top and bottom navigation bar.
 Secondary Color: This color should be chosen only when there is a low
complexity level of the application. This color will be applied to those elements
which need a little color accent like the background color for the Floating Action
Buttons (FAB), sliders, toggle buttons, chips (Active State), progress bars, etc.
 Light and Dark variants: These colors are the variants of the primary color. The
dark variant of the primary color is set for the status bar and the light variant of
the primary color is set for the Floating action button, outline for the edit texts,
and where the elements need some color accents the light variant of the
primary colors will be set for them. Have a look at the following image when
only the primary color is set for the application theme looks like.

But according to the complexity of the application, there are various types of
color can be chosen.
 Have a look at the following image, having selected the primary color, with that
the Complementary color can be chosen or Analogous color can be chosen
or Triadic color can be chosen or Secondary color can be chosen to meet the
required style.

2. Typography (Choosing the Right Font)


 In android, however, the Roboto  font meets all the requirements. But too, if the
developer wants to customize the application more with the font, the font
needs to be chosen where it has all its variants. The variants are the light face,
regular face, medium face, and sometimes the dark face.
 Choosing the font from Google Font  is recommended. As it offers a variety of
font families, and almost all the fonts have contained all the variants.
 There are some guidelines that need to be followed by having the font chosen.
In this case, the Roboto is chosen for demonstration purposes only. Looking at
the following image which is the type scale chart for applying the styles of the
fonts for various contexts.
 The various contexts include captions, Body, Subtitles, Button, captions, etc.
 In the below image left side column contains the font is chosen, font style, and
the font size. The second column contains the preview of the selected context
of the font.
.
3. Material Design Components
 Material design components are the components that allow a lot of features for
the users and easy to implement for the developers.
 Have a look at the following image on how material design components stand
out in terms of the customization, styling, and look, from the normal UI
components.

 The features offered by the material design components can be implemented in


various contexts. One can notice in the above image how the normal button and
edit text has adapted to the application’s theme and how the material design
button has adapted to the application’s theme.
 These components can even adapt to the dark theme and change their styles
when it is toggled by the user. Have look at the following image to differentiate
the behaviors between the material design components and normal UI
components.

4. Shaping the Components

 In material design, there are three types of shaping methods.


1. Cut corner
2. Rounded corner
3. Triangle edge

 These methods are also can be applied for the material design buttons, text
fields, chips, floating action buttons, cards, navigation bars, bottom sheets, etc.
 These features are available with the Material design components out of the
proverbial box. Just need to add the dependency of the material design
components and start implementing the styling for material design components.

9. What do u understand by localization in Android


The applications that we use in Android devices can be specific to a particular
language. That is where Localization comes in the role. Localization is a process that
changes the string into various different languages based on user requirements. In
this tutorial, we will implement it practically in our application.
Localization can be done for Dates and time as well. Localizing the string is good for
the users, but, how about localizing the date and the time? Yes, it can be done too.
Luckily, Android SDK includes the classes that format dates and times according to
the locale. In Android SDK these date and time are handled using Date class
from java.util namespace. To return the current date and time, you can
use java.util.Calendar.
You can code it this way to return the Date and the Time:

Date date= Calendar.getInstance().getTime(); // this will get the date and the time
java.text.DateFormat date_format; //This will get the standard format
Date_format = android.text.format.DateFormat.getDateFormat(this);
And for Time:

java.util.Date date_today = Calendar.getInstance().getTime();


java.text.DateFormat time_format;
time_format = android.text.format.DateFormat.getTimeFormat(this);

Sr.No Language & code

1 Afrikanns
Code: af. Folder name: values-af

Arabic
2
Code: ar. Folder name: values-ar

Bengali
3
Code: bn. Folder name: values-bn

Czech
4
Code: cs. Folder name: values-cs
Chinese
5
Code: zh. Folder name: values-zh

German
6
Code: de. Folder name: values-de

French
7
Code: fr. Folder name: values-fr

Japanese
8
Code: ja. Folder name: values-ja

Example

To experiment with this example , you can run this on an actual device or in an
emulator.

Steps Description

1 You will use Android studio to create an Android application under a package
com.example.sairamkrishna.myapplication.

2 Modify the res/layout/activity_main to add respective XML components

3 Modify the res/values/string.xml to add necessary string components

4 Run the application and choose a running android device and install the application
on it and verify the results

10. Define ScrollView and ListView


In android, ScrollView is a kind of layout that is useful to add vertical or horizontal
scroll bars to the content which is larger than the actual size of layouts such
as linearlayout, relativelayout, framelayout, etc.
 
Generally, the android ScrollView is useful when we have content that doesn’t fit our
android app layout screen. The ScrollView will enable a scroll to the content which is
exceeding the screen layout and allow users to see the complete content by scrolling.
 
The android ScrollView can hold only one direct child. In case, if we want to add
multiple views within the scroll view, then we need to include them in another
standard layout like linearlayout, relativelayout, framelayout, etc.
 
To enable scrolling for our android applications, ScrollView is the best option but we
should not use ScrollView along with ListView or Gridview because they both will
take care of their own vertical scrolling.
 
In android, ScrollView supports only vertical scrolling. In case, if we want to
implement horizontal scrolling, then we need to use
a HorizontalScrollView component.
 
The android ScrollView is having a property called android:fillViewport, which is used
to define whether the ScrollView should stretch it’s content to fill the viewport or
not.
 
Now we will see how to use ScrollView with linearlayout to enable scroll view to the
content which is larger than screen layout in android application with examples.
<ScrollView
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- add child view’s here -->

</ScrollView>

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.
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 WrapperListA
dapter. We will see separate examples for both the adapters.

ListView Attributes

Following are the important attributes specific to GridView −

Sr.N Attribute & Description


o

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.

ArrayAdapter

You can use this adapter when your data source is an array. By default,
ArrayAdapter creates a view for each array item by calling toString() on each item
and placing the contents in a TextView. Consider you have an array of strings you
want to display in a ListView, initialize a new ArrayAdapter using a constructor to
specify the layout for each string and the string array −
ArrayAdapter adapter = new
ArrayAdapter<String>(this,R.layout.ListView,StringArray);
Here are arguments for this constructor −
 First argument this is the application context. Most of the case, keep it this.
 Second argument will be layout defined in XML file and having TextView for
each string in the array.
 Final argument is an array of strings which will be populated in the text view.
Once you have array adapter created, then simply call setAdapter() on
your ListView object as follows −
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
You will define your list view under res/layout directory in an XML file. For our
example we are going to using activity_main.xml file.
Example
Following is the example which will take you through simple steps to show how to
create your own Android application using ListView. Follow the following steps to
modify the Android application we created in Hello World Example chapter −

Step Description

1 You will use Android Studio IDE to create an Android application and name it
as ListDisplay under a package com.example.ListDisplay as explained in the Hello
World Example chapter.

2 Modify the default content of res/layout/activity_main.xml file to include ListView


content with the self explanatory attributes.

3 No need to change string.xml, Android studio takes care of default string constants.

4 Create a Text View file res/layout/activity_listview.xml. This file will have setting to


display all the list items. So you can customize its fonts, padding, color etc. using this
file.

6 Run the application to launch Android emulator and verify the result of the changes
done in the application.

You might also like