Mad Unit Ii-1
Mad Unit Ii-1
Mad Unit Ii-1
The android apps will contain multiple screens and each screen of our
application will be an extension of Activity class.
P.Nivetha Page 1
MOBILE APPLICATION DEVELOPMENT
marked as a main activity and that is the first screen to appear when
lifetime.
By using callback methods we can get the activity transitions
P.Nivetha Page 2
MOBILE APPLICATION DEVELOPMENT
which shows how Activity will behave in different stages using callback
methods.
P.Nivetha Page 3
MOBILE APPLICATION DEVELOPMENT
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.
android app using the callback method and it’s not necessary to use all
P.Nivetha Page 4
MOBILE APPLICATION DEVELOPMENT
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
P.Nivetha Page 5
MOBILE APPLICATION DEVELOPMENT
MainActivity.java
package example.activitylifecycle;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
P.Nivetha Page 6
MOBILE APPLICATION DEVELOPMENT
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}
P.Nivetha Page 7
MOBILE APPLICATION DEVELOPMENT
v4 Support Libraries
com.android.support:support-compat:28.0.0
2. v4 core-utils library
Provides a number of utility classes, such
P.Nivetha Page 8
MOBILE APPLICATION DEVELOPMENT
com.android.support:support-core-utils:28.0.0
3. V4 core-ui library
com.android.support:support-core-ui:28.0.0
4. v4 media-compat library
com.android.support:support-media-compat:28.0.0
The Gradle build script dependency identifier for this library is as follows:
com.android.support:support-fragment:28.0.0
P.Nivetha Page 9
MOBILE APPLICATION DEVELOPMENT
v7 Support Libraries
additional features.
library is included in every new Android Studio project, and new activities
This library adds support for the Action Bar user interface design
pattern.
implementations.
inside cards.
P.Nivetha Page 10
MOBILE APPLICATION DEVELOPMENT
The Gradle build script dependency identifier for this library is as follows:
com.android.support:cardview-v7:28.0.0
The Gradle build script dependency identifier for this library is as follows:
com.android.support:gridlayout-v7:28.0.0
P.Nivetha Page 11
MOBILE APPLICATION DEVELOPMENT
The Gradle build script dependency identifier for this library is as follows:
com.android.support:mediarouter-v7:28.0.0
P.Nivetha Page 12
MOBILE APPLICATION DEVELOPMENT
4. v7 palette library
The Gradle build script dependency identifier for this library is as follows:
com.android.support:palette-v7:28.0.0
P.Nivetha Page 13
MOBILE APPLICATION DEVELOPMENT
The Gradle build script dependency identifier for this library is as follows:
P.Nivetha Page 14
MOBILE APPLICATION DEVELOPMENT
The Gradle build script dependency identifier for this library is as follows:
com.android.support:preference-v7:28.0.0
P.Nivetha Page 15
MOBILE APPLICATION DEVELOPMENT
Intent
other applications.
Usage of Android Intents:
P.Nivetha Page 16
MOBILE APPLICATION DEVELOPMENT
1. Component Name
component name android system will deliver intent to the specific app
2.Action
P.Nivetha Page 17
MOBILE APPLICATION DEVELOPMENT
3. Data
it’s important to specify the type of data (MIME type) in addition to its URI.
4. Category
In, the android category is optional for intents and it specifies the
additional information about the type of component that should handle an
intent.
The above properties (Component Name, Action, Data, and Category) will
represent the characteristics of an intent.
P.Nivetha Page 18
MOBILE APPLICATION DEVELOPMENT
1. Explicit Intents
Syntax:
P.Nivetha Page 19
MOBILE APPLICATION DEVELOPMENT
2. Implicit Intents
For example, by using implicit intents we can request another app to show
the location details of the user or etc.
P.Nivetha Page 20
MOBILE APPLICATION DEVELOPMENT
Syntax:
P.Nivetha Page 21
MOBILE APPLICATION DEVELOPMENT
Intent Filter
Intent Filter are the components which decide the behavior of
an intent. The Intent is about the navigation of one activity to another, that
can be achieve by declaring intent filter. We can declare an Intent Filter for
an Activity in manifest file.
P.Nivetha Page 22
MOBILE APPLICATION DEVELOPMENT
1. Action
2. Data
3. Category
Note: Every intent filter must contain action element in it. Data and
1. Action:
It represent an activities action, what an activity is going to do. It is
declared with the name attribute as given below
<intent-filter . . . >
<action android:name="com.example.project.SHOW_CURRENT" />
<action android:name="com.example.project.SHOW_RECENT" />
<action android:name="com.example.project.SHOW_PENDING" />
...
</intent-filter>
P.Nivetha Page 23
MOBILE APPLICATION DEVELOPMENT
There are few common actions for starting an activity like ACTION_VIEW
ACTION_VIEW: This is used in an Intent with startActivity(). This helps when
you redirect to see any website, photos in gallery app or an address to view
in a map app.
There are more actions similar to above like, ACTION_SEND,
ACTION_MAIN, ACTION_WEB_SEARCH and many more.
2. <category>
<intent-filter . . . >
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
...
</intent-filter>
P.Nivetha Page 24
MOBILE APPLICATION DEVELOPMENT
3.<data>
There are two forms in which you can pass the data, using
URI(Uniform Resource Identifiers) or MIME type of data.
The syntax of data attribute is as follows:
<data android:scheme="string"
android:host="string"
android:port="string"
android:path="string"
android:pathPattern="string"
android:pathPrefix="string"
android:mimeType="string" />
This specifies the format of data associated with an Intent which you use
with component. As explained in above code snippet, data passed through
Intent is a type of URI. Check the below given table for better clarification
ACTION DATA MEANING
Opens phone
Intent.ACTION_CALL tel:phone_number application and calls
phone number
Opens phone
application and dials
Intent.ACTION_DIAL tel:phone_number
(but doesn’t call)
phone_number
P.Nivetha Page 25
MOBILE APPLICATION DEVELOPMENT
Adding Categories
CATEGORY_APP_BROWSER
1
Used with ACTION_MAIN to launch the browser application.
CATEGORY_APP_CALCULATOR
2
Used with ACTION_MAIN to launch the calculator application.
CATEGORY_APP_CALENDAR
3
Used with ACTION_MAIN to launch the calendar application.
CATEGORY_APP_CONTACTS
4
Used with ACTION_MAIN to launch the contacts application.
CATEGORY_APP_EMAIL
5
Used with ACTION_MAIN to launch the email application.
CATEGORY_APP_GALLERY
6
Used with ACTION_MAIN to launch the gallery application.
P.Nivetha Page 26
MOBILE APPLICATION DEVELOPMENT
CATEGORY_APP_MAPS
7
Used with ACTION_MAIN to launch the maps application.
CATEGORY_APP_MARKET
8
This activity allows the user to browse and download new applications.
CATEGORY_APP_MESSAGING
9
Used with ACTION_MAIN to launch the messaging application.
CATEGORY_APP_MUSIC
10
Used with ACTION_MAIN to launch the music application.
CATEGORY_BROWSABLE
11 Activities that can be safely invoked from a browser must support this
category.
CATEGORY_CAR_DOCK
12
An activity to run when device is inserted into a car dock.
CATEGORY_DEFAULT
13 Set if the activity should be an option for the default action (center
press) to perform on a piece of data.
CATEGORY_DESK_DOCK
14
An activity to run when device is inserted into a car dock.
P.Nivetha Page 27
MOBILE APPLICATION DEVELOPMENT
CATEGORY_DEVELOPMENT_PREFERENCE
15
This activity is a development preference panel.
CATEGORY_FRAMEWORK_INSTRUMENTATION_TEST
16
To be used as code under test for framework instrumentation tests.
CATEGORY_HOME
17 This is the home activity, that is the first activity that is displayed when
the device boots.
CATEGORY_INFO
18
Provides information about the package it is in.
CATEGORY_LAUNCHER
19
Should be displayed in the top-level launcher.
CATEGORY_OPENABLE
20 Used to indicate that a GET_CONTENT intent only wants URIs that can
be opened with ContentResolver.openInputStream.
CATEGORY_PREFERENCE
21
This activity is a preference panel.
CATEGORY_TAB
22
Intended to be used as a tab inside of a containing TabActivity.
P.Nivetha Page 28
MOBILE APPLICATION DEVELOPMENT
Linking Activities
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="First Activity"
android:textAlignment="center"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
P.Nivetha Page 29
MOBILE APPLICATION DEVELOPMENT
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn1"
android:onClick="newsScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
Mainactivity.java
package com.example.intent2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
P.Nivetha Page 30
MOBILE APPLICATION DEVELOPMENT
import android.view.View;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(i);
}
}
activity_main2.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
P.Nivetha Page 31
MOBILE APPLICATION DEVELOPMENT
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<TextView
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Activity"
android:textAlignment="center"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
P.Nivetha Page 32
MOBILE APPLICATION DEVELOPMENT
<Button
android:id="@+id/btn2"
android:onClick="homeScreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity2.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
P.Nivetha Page 33
MOBILE APPLICATION DEVELOPMENT
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
startActivity(i);
P.Nivetha Page 34
MOBILE APPLICATION DEVELOPMENT
Output:
P.Nivetha Page 35
MOBILE APPLICATION DEVELOPMENT
In Android app, end user interacts with screen which is called user
interface.
This interface can be designed with the help of many controls which
are called views in Android.
Views
The View class is the parent for a large hierarchy of visual controls,
widgets, like
Widgets are displayed only if they are added to the display layout.
ViewGroup
P.Nivetha Page 36
MOBILE APPLICATION DEVELOPMENT
Linear Layout
Relative Layout
Frame Layout
Table Layout
Web View
List View
Grid View
The following are some of the common layout attributes used in the
android application.
P.Nivetha Page 37
MOBILE APPLICATION DEVELOPMENT
Attribute Description
P.Nivetha Page 38
MOBILE APPLICATION DEVELOPMENT
Attribute Description
elements in a layout
P.Nivetha Page 39
MOBILE APPLICATION DEVELOPMENT
Basic Views
The View is a base class for all UI components in android. Following are
the some of common View subclasses that will be used in android
applications.
Text View
Edit Text
Button
Checkbox
Radio Button
Image Button
Spinner
1. Text View
The following are some of the commonly used attributes related to Text
View control in android applications.
Attribute Description
P.Nivetha Page 40
MOBILE APPLICATION DEVELOPMENT
Attribute Description
P.Nivetha Page 41
MOBILE APPLICATION DEVELOPMENT
Attribute Description
2. EditText
Edit Text is a user interface control which is used to allow the user to
enter or modify the text.
While using Edit Text control in our android applications, we need to
specify the type of data the text field can accept using the input
Type attribute.
Attribute Description
android:gravity It is used to specify how to align the text like left, right,
center, top, etc.
P.Nivetha Page 42
MOBILE APPLICATION DEVELOPMENT
Attribute Description
android:background It is used to set the background color for edit text control
3. Button
P.Nivetha Page 43
MOBILE APPLICATION DEVELOPMENT
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.
P.Nivetha Page 44
MOBILE APPLICATION DEVELOPMENT
Attribute Description
4.Check box
Attribute Description
android:gravity It is used to specify how to align the text like left, right, center, top, etc.
P.Nivetha Page 45
MOBILE APPLICATION DEVELOPMENT
Attribute Description
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.
4. Radio Button
unchecked and it’s the same as CheckBox control, except that it will allow
Following are the some of commonly used attributes related to RadioButton control in
android applications.
P.Nivetha Page 46
MOBILE APPLICATION DEVELOPMENT
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 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.
P.Nivetha Page 47
MOBILE APPLICATION DEVELOPMENT
5. Image Button
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.
P.Nivetha Page 48
MOBILE APPLICATION DEVELOPMENT
6. Spinner
Picker Views
In android, Date Picker is a control that will allow users to select the
date by a day, month and year in our application user interface.
If we use Date Picker in our application, it will ensure that the users
will select a valid date.
P.Nivetha Page 49
MOBILE APPLICATION DEVELOPMENT
Attribute Description
activity_main.xml
P.Nivetha Page 50
MOBILE APPLICATION DEVELOPMENT
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker1"
android:layout_marginLeft="100dp"
android:text="Get Date" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="100dp"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textSize="18dp"/>
</RelativeLayout>
MainActivity.java
package com.lane.datepickerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
DatePicker picker;
Button btnGet;
TextView tvw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvw=(TextView)findViewById(R.id.textView1);
picker=(DatePicker)findViewById(R.id.datePicker1);
btnGet=(Button)findViewById(R.id.button1);
btnGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tvw.setText("Selected Date: "+ picker.getDayOfMonth()+"/"+
(picker.getMonth() + 1)+"/"+picker.getYear());
}
});
}
}
P.Nivetha Page 52
MOBILE APPLICATION DEVELOPMENT
ListView
In android, ListView is a ViewGroup that is used to display the list of
scrollable of items in multiple rows and the list items are automatically
inserted to the list using an adapter.
Android Adapter
P.Nivetha Page 53
MOBILE APPLICATION DEVELOPMENT
Adapter Description
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/
res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/userlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
P.Nivetha Page 54
MOBILE APPLICATION DEVELOPMENT
</ListView>
</LinearLayout>
MainActivity.java
package com.lane.listview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
P.Nivetha Page 55
MOBILE APPLICATION DEVELOPMENT
Specialized Fragment
Android Fragment is the part of activity, it is also known as sub-
Each fragment has its own life cycle methods that is affected by
P.Nivetha Page 56
MOBILE APPLICATION DEVELOPMENT
Following are the list of methods which will perform during the
lifecycle of fragment in android applications.
P.Nivetha Page 57
MOBILE APPLICATION DEVELOPMENT
Method Description
P.Nivetha Page 58
MOBILE APPLICATION DEVELOPMENT
Method Description
activity_main.xml
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="example.javatpoint.com.fragmentexample.MainActivity">
<fragment
android:id="@+id/fragment1"
android:name="example.javatpoint.com.fragmentexample.Fragment1"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<fragment
P.Nivetha Page 59
MOBILE APPLICATION DEVELOPMENT
android:id="@+id/fragment2"
android:name="example.javatpoint.com.fragmentexample.Fragment2"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
fragment_fragment1.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F5DC"
tools:context="example.javatpoint.com.fragmentexample.Fragment1">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
P.Nivetha Page 60
MOBILE APPLICATION DEVELOPMENT
fragment_fragment2.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0FFFF"
tools:context="example.javatpoint.com.fragmentexample.Fragment2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Main Activity.class
package example.javatpoint.com.fragmentexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
P.Nivetha Page 61
MOBILE APPLICATION DEVELOPMENT
Fragment1.java
package example.javatpoint.com.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment1, container, false);
}
}
P.Nivetha Page 62
MOBILE APPLICATION DEVELOPMENT
Fragment2.java
package example.javatpoint.com.fragmentexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment2, container, false);
}
P.Nivetha Page 63
MOBILE APPLICATION DEVELOPMENT
Output:
P.Nivetha Page 64
MOBILE APPLICATION DEVELOPMENT
GridView
In android, Grid View is a ViewGroup that is used to display items in
a two dimensional, scrollable grid and grid items are automatically inserted
to the gridview layout using a list adapter.
GridView Example
activity_main.xml
P.Nivetha Page 65
MOBILE APPLICATION DEVELOPMENT
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
MainActivity.java
package com.example.androidgridviewexample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
GridView grid;
P.Nivetha Page 66
MOBILE APPLICATION DEVELOPMENT
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.R.layout.simple_list_item_1, letters);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new OnItemClickListener()
{
id)
{
Toast.makeText(getApplicationContext(),((TextView) v).getText(),
Toast.LENGTH_SHORT).show();
});
}
}
P.Nivetha Page 67
MOBILE APPLICATION DEVELOPMENT
OUTPUT:
P.Nivetha Page 68
MOBILE APPLICATION DEVELOPMENT
Options Menu
Attribute Description
P.Nivetha Page 69
MOBILE APPLICATION DEVELOPMENT
Attribute Description
Activity_main.xml
<item
android:id="@+id/about_us_id"
android:title="About"
android:showAsAction="always"/>
<item
android:id="@+id/settings_id"
android:title="Settings"
android:showAsAction="always"/>
<item
android:id="@+id/Share_id"
P.Nivetha Page 70
MOBILE APPLICATION DEVELOPMENT
android:title="Share"
android:showAsAction="always"/>
<item
android:id="@+id/Help_id"
android:title="Help"
android:showAsAction="always"/>
</menu>
MainActivity.java
package com.example.menudemo;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity
@Override
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
P.Nivetha Page 71
MOBILE APPLICATION DEVELOPMENT
@Override
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
@Override
case R.id.about_us_id:
return true;
case R.id.settings_id:
return true;
case R.id.Share_id:
return true;
case R.id.Help_id:
return true;
default:
P.Nivetha Page 72
MOBILE APPLICATION DEVELOPMENT
return super.onOptionsItemSelected(item);
OUTPUT:
P.Nivetha Page 73
MOBILE APPLICATION DEVELOPMENT
P.Nivetha Page 74
MOBILE APPLICATION DEVELOPMENT
Context Menu
In android, Context Menu is like a floating menu and that appears
when the user performs a long press or click on an element and it is useful
to implement actions that affect the selected content or context frame.
The android Context Menu is more like the menu which displayed on
right-click in Windows or Linux.
Activity_main.xml
P.Nivetha Page 75
MOBILE APPLICATION DEVELOPMENT
MainActivity.java
package example.javatpoint.com.contextmenu;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
P.Nivetha Page 76
MOBILE APPLICATION DEVELOPMENT
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
menu.setHeaderTitle("Select The Action");
}
@Override
public boolean onContextItemSelected(MenuItem item){
if(item.getItemId()==R.id.call){
Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_L
ONG).show();
}
else if(item.getItemId()==R.id.sms){
Toast.makeText(getApplicationContext(),"sending sms code",Toast.LEN
GTH_LONG).show();
}else{
return false;
}
return true;
} }
Output:
P.Nivetha Page 77
MOBILE APPLICATION DEVELOPMENT
P.Nivetha Page 78
MOBILE APPLICATION DEVELOPMENT
Clock View
Analog and digital clocks are used for display the time in android
application.
1. Analog clock: Analog clock is a subclass of View class. It represents a
circular clock. Around the circle, numbers 1 to 12 appear to represent
the hour and two hands are used to show instant of the time- shorter
one for the hour and longer is for minutes.
2. Digital clock: Digital clock is subclass of TextView Class and uses
numbers to display the time in “HH:MM” format.
For Example
P.Nivetha Page 79
MOBILE APPLICATION DEVELOPMENT
Activity_main.xml
<AnalogClock
android:layout_marginTop="20dp"
android:layout_marginLeft="120dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<DigitalClock
android:layout_marginLeft="140dp"
android:textSize="25dp"
android:layout_marginTop="300dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java
package org.geeksforgeeks.navedmalik.analogdigital;
P.Nivetha Page 80
MOBILE APPLICATION DEVELOPMENT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
P.Nivetha Page 81
MOBILE APPLICATION DEVELOPMENT
Web view
P.Nivetha Page 82
MOBILE APPLICATION DEVELOPMENT
activity_main.xml
MainActivity.java
package com.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
P.Nivetha Page 83
MOBILE APPLICATION DEVELOPMENT
Output:
P.Nivetha Page 84
MOBILE APPLICATION DEVELOPMENT
Recycler View
RecyclerView is mostly used to design the user interface with the fine-
If you want to use a RecyclerView, you will need to work with the
following:
P.Nivetha Page 85
MOBILE APPLICATION DEVELOPMENT
the selected item will be located at the center of the horizontal list.
The Adapter can be used to add "n" number of items since it serves
padding: Used to give the padding in Gallery view(from all sides: top,
P.Nivetha Page 86
MOBILE APPLICATION DEVELOPMENT
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
P.Nivetha Page 87
MOBILE APPLICATION DEVELOPMENT
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="fitXY" />
<Gallery
android:id="@+id/languagesGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:animationDuration="2000"
android:padding="10dp"
android:spacing="5dp"
android:unselectedAlpha="50" />
</LinearLayout>
MainActivity.java
import android.os.Bundle;
import android.widget.Gallery;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
Gallery simpleGallery;
P.Nivetha Page 88
MOBILE APPLICATION DEVELOPMENT
CustomizedGalleryAdapter customGalleryAdapter;
ImageView selectedImageView;
int[] images = {
R.drawable.python,
R.drawable.javascript,
R.drawable.java,
R.drawable.python,
R.drawable.r,
R.drawable.python,
R.drawable.javascript,
R.drawable.python,
R.drawable.r,
R.drawable.javascript
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
P.Nivetha Page 89
MOBILE APPLICATION DEVELOPMENT
CustomizedGalleryAdapter
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
P.Nivetha Page 90
MOBILE APPLICATION DEVELOPMENT
// returns the Item of an item, i.e. for our example we can get the
image
public Object getItem(int position) {
return position;
}
P.Nivetha Page 91
MOBILE APPLICATION DEVELOPMENT
Output:
P.Nivetha Page 92
MOBILE APPLICATION DEVELOPMENT
Image Switcher
In android, ImageSwitcher is a specialized view switcher that will
Method Description
P.Nivetha Page 93
MOBILE APPLICATION DEVELOPMENT
ImageSwitcher Example
activity_main.xml
P.Nivetha Page 94
MOBILE APPLICATION DEVELOPMENT
MainActivity.java
package com.imageswitcherexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
P.Nivetha Page 95
MOBILE APPLICATION DEVELOPMENT
previousbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position>0)
position--;
else if(position<0)
position = 0;
imgsw.setImageResource(images[position]);
}
});
nextbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position<images.length)
position++;
if(position>=images.length)
position = images.length-1;
imgsw.setImageResource(images[position]);
}
});
}
}
P.Nivetha Page 96
MOBILE APPLICATION DEVELOPMENT
Output:
P.Nivetha Page 97