0% found this document useful (0 votes)
147 views

Android Application Development

The document discusses various aspects of creating a login form in Android, including: - Android uses layouts to define the structure and elements of a user interface. Common layouts like linear, relative, and constraint are used to design login screens. - Common elements needed for a basic login form are edittext views for the username and password, button views for the login action, and text views for labels. - Shared preferences can be used to save login credentials locally so the user stays logged in between sessions. - Intents are used to launch new activities after successful login, like a home screen or main app interface.

Uploaded by

Tushar Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Android Application Development

The document discusses various aspects of creating a login form in Android, including: - Android uses layouts to define the structure and elements of a user interface. Common layouts like linear, relative, and constraint are used to design login screens. - Common elements needed for a basic login form are edittext views for the username and password, button views for the login action, and text views for labels. - Shared preferences can be used to save login credentials locally so the user stays logged in between sessions. - Intents are used to launch new activities after successful login, like a home screen or main app interface.

Uploaded by

Tushar Singh
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 174

• Introduction to

Android
• Android Application
Architecture

SSDN
21-07-2018 2
What year was
Android
founded?

SSDN
21-07-2018 3
WHY
ANDROID ?

SSDN
21-07-2018 4
INTRODUCTION TO ANDROID

A software platform and operating system for mobile


devices
Based on LINUX kernel
Developed by Google and Open Handset
Alliance(OHA)
Allows writing managed code

SSDN
21-07-2018 5
HISTORY OF ANDROID

SSDN
21-07-2018 6
ANDROID 1.0 - API LEVEL 1

First commercial version released on September 23,


2008
The first commercially available Android device was
the HTC Dream
 Features include Android market, camera , Gmail,
Maps, Google talk, YouTube player , Wi-Fi …

SSDN
21-07-2018 7
ANDROID 1.1 - API LEVEL 2

Released on February 9, 2009


The update resolved bugs
 Changed the Android API

SSDN
21-07-2018 8
ANDROID 1.5- API LEVEL 3
(CUPCAKE)

 Released on April 27, 2009


 Support for Widgets, Animated screen transitions,
Autorotation option
Ability to upload videos to YouTube, Picasa

SSDN
21-07-2018 9
ANDROID 1.6- API LEVEL 4
(DONUT)

 Released on September 15, 2009


 Quick search box
 Screen size diversity
 Google play

SSDN
21-07-2018 10
ANDROID 2.2- API LEVEL 8
(FROYO)

Released on May 20, 2010


Voice action
USB tethering and Potable hotspot
 Speed, memory, and performance optimizations

SSDN
21-07-2018 11
ANDROID 2.3-2.3.3
API LEVEL 9-10
(GINGERBREAD)

 Released on December 6, 2010


 Support for multiple cameras
 Gaming APIs
 Battery management

SSDN
21-07-2018 12
ANDROID 3.0,3.1,3.2
API LEVEL 11,12,13
(HONEYCOMB)

Released in February 22, 2011


 Tablet friendly design
 System bar
 Quick settings

SSDN
21-07-2018 13
ANDROID 4.0,4.0.3
API LEVEL 14,15
(ICE CREAM SANDWICH)

Released on October 18, 2011


 Customize home screen
 Data Usage control

SSDN
21-07-2018 14
ANDROID 4.1,4.3
API LEVEL 16,18
(JELLY BEAN)

Released on July 9, 2012


 Google Now
 Actionable notification
 Switchable account

SSDN
21-07-2018 15
ANDROID 4.4,4.4.w
API LEVEL 19,20
(JELLY BEAN)

Released on October 31, 2013


 Ok Google
 Smart Dialer

SSDN
21-07-2018 16
ANDROID 5.0,5.1
API LEVEL 21,22
(KITKAT)

Released on November 12, 2014


 Material Design
 Notification
 Multiscreen

SSDN
21-07-2018 17
ANDROID 6.0
API LEVEL 23
(MARSHMALLOW)

 Released on October 5, 2015


Permission at runtime
App standby for battery optimization.

SSDN
21-07-2018 18
ANDROID 7.0
API LEVEL 24
(NOUGAT)

Released on 22 August 2016


Notification sheets
 Switch apps by double tapping in overview button

SSDN
21-07-2018 19
ANDROID 8.0,8.1
API LEVEL 26
(OREO)

Released on 21 August 2017


 Autosizing Textview
 Webview APIs
 Pointer Capture

SSDN
21-07-2018 20
What is an
Application?

SSDN
21-07-2018 21
SSDN
21-07-2018 22
• Android consists of a kernel based on Linux kernel
with middleware, libraries and APIs written in C
• Application software running on an application
• Framework which includes Java-compatible libraries
based on Apache Harmony.
• Android uses the Dalvik virtual machine with just-in-
time compilation to run Dalvik 'dex-code‘

SSDN
21-07-2018 23
• Android uses Linux for its device drivers, memory
management, process management, and networking.
• Android native libraries written in C/C++ internally,
but you’ll be calling them through Java interfaces.
• Dalvik Virtual Machine. Runs dex files, which are
coverted at compile time from standard class and jar
files.

SSDN
21-07-2018 24
What are OOPS
concepts of
JAVA ?

SSDN
21-07-2018 25
Application components are the essential building
blocks of an Android application.
Each component is a different point through which
the system can enter your application.
Four main types of application components:
 Activities
 Services
Content providers
Broadcast receivers
SSDN
21-07-2018 26
Activities
An activity represents a single screen with a user
interface.
Services
A service is a component that runs in the background
to perform long-running operations or to perform work
for remote processes. A service does not provide a user
interface, or a picture was captured. Applications can
also initiate broadcasts.

SSDN
21-07-2018 27
Content providers
A content provider manages a shared set of application
data. You can store the data in the file system, an SQLite
database, on the web, or any other persistent storage
ocation your application can access. Through the content
provider, other applications can query or even modify the
data (if the content provider allows it).
Broadcast receivers
A broadcast receiver is a component that responds to
system-wide broadcast announcements. Many broadcasts
originate from the system—for example, a broadcast
announcing that the screen has turned off, the battery is
low
SSDN
21-07-2018 28
Hardware Reuirements
Min 4GB RAM
i3 Processor
Software Requirement
JDK
Android Studio

SSDN
21-07-2018 29
An Integrated Development Environment (IDE) is an
all-in-one solution that allows an app developer to
perform the software development cycle repeatedly
and quickly.
FOR ANDROID-
ANDROID STUDIO - Best

SSDN
21-07-2018 30
• Contains your Activity file, which is
stored at s
src/your/package/ActivityName.java
.
• All other source code files (such as
.java go here as well.

SSDN
21-07-2018 31
Contains the Java files generated by
ADT, such as your R.java file

SSDN
21-07-2018 32
Contains application resources, such as
drawable files, layout files and string
values

SSDN
21-07-2018 33
For XML files that are compiled
into animation objects

SSDN
21-07-2018 34
For XML files that describe colors

SSDN
21-07-2018 35
For bitmap files (PNG, JPEG, or GIF),
9-Patch image files, and XML files
that
describe Drawable shapes

SSDN
21-07-2018 36
XML files that are compiled into
screen layouts (or part of a screen)

SSDN
21-07-2018 37
These files are processed by aapt
and must be referenced from the
application using a resource
identifier in the R class. For
example, this is a good place for
media,
such as MP3 or Ogg files.

SSDN
21-07-2018 38
For XML files that are compiled into
many kinds of resource.

SSDN
21-07-2018 39
The control file that describes the
nature of the application and each
of its components..

SSDN
21-07-2018 40
BUILD PROCESS

SSDN
21-07-2018 41
Android is a multi-process system, in which each
application (and parts of the system) runs in its own
process
 User and group IDs are assigned to applications.
 Additional finer-grained security features are
provided through a "permission" mechanism.

SSDN
21-07-2018 42
• Android Widgets
• Layouts
• Shared Preferences
• Intent

SSDN
21-07-2018 43
What do we
need to create
a Login FORM?

SSDN
21-07-2018 44
ANDROID LAYOUTS

Layout is the architecture for the user interface in an


Activity.
It defines the layout structure and holds all the
elements that appear to the user.

SSDN
21-07-2018 45
DECLARING LAYOUTS

#1: Declare UI elements in XML (most common and


preferred) > Android provides a straightforward XML
vocabulary that corresponds to the View classes and
subclasses, such as those for UI controls called widgets
(TextView, Button, etc.) and layouts.

#2: (in Java code) Instantiate layout elements at


runtime (in Java code) > Your application can create
View and ViewGroup objects programmatically.
SSDN
21-07-2018 46
LAYOUTS in application structure

SSDN
21-07-2018 47
LAYOUTS – Reference in JAVA Code

Assuming a view/widget is defined in the layoutfile


with a unique ID
<Button android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
Then you can make a reference to the view object via
findViewById(R.id.<string-id>).
Button myButton = (Button) findViewById (R.id.my_button);
SSDN
21-07-2018 48
LAYOUTS – Reference in JAVA Code

wrap_content
tells your view to size itself to the dimensions required
by its content
match_parent
tells your view to become as big as its parent view
group will allow.
<Button android:id="@+id/my_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
SSDN
21-07-2018 49
LAYOUT TYPES

 LinearLayout
 RelativeLayout
 TableLayout

SSDN
21-07-2018 50
LINEAR LAYOUT

Aligns all children in a single direction — vertically or


horizontally, depending on how you define the orientation
attribute.
All children are stacked one after the other, so a vertical
list will only have one child per row, no matter how wide
they are
A LinearLayout respects
margins between children
gravity (right, center, or left alignment) of each child.
weight to each child
SSDN
21-07-2018 51
RELATIVE LAYOUT

RelativeLayout lets child views specify their position


relative to the parent view or to each other (specified
by ID)
You can align two elements by right border, or make
one below another, centered in the screen, centered
left, and so on
 Elements are rendered in the order given, so if the
first element is centered in the screen, other elements
aligning themselves to that element will be aligned
relative to screen center.
SSDN
21-07-2018 52
RELATIVE LAYOUT
RelativeLayout Example
<?xml version="1.0" encoding="utf-8"?> <EditText android:id="@+id/entry"
android:layout_width="fill_parent"
<RelativeLayout
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.co
android:background="@android:drawab
m/apk/res/android
le/editbox_background"
android:layout_width="fill_parent" android:layout_below="@id/label" />
android:layout_height="wrap_content" <Button android:id="@+id/ok"
android:background="@drawable/blue" android:layout_width="wrap_content"
android:padding="10px" > android:layout_height="wrap_content"
android:layout_below="@id/entry"
<TextView android:id="@+id/label"
android:layout_alignParentRight="true"
android:layout_width="fill_parent" android:layout_marginLeft="10px"
android:layout_height="wrap_content" android:text="OK" />
android:text="Type here:" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
SSDN
</RelativeLayout> 21-07-2018 53
TABLELAYOUT

TableLayout positions its children into rows and


columns
TableRow objects are the child views of a TableLayout
(each TableRow defines a single row in the table).
Each row has zero or more cells, each of which is
defined by any kind of other View.
Columns can be hidden, marked to stretch and fill the
available screen space, or can be marked as shrinkable
to force the column to shrink until the table fits the
screen.
SSDN
21-07-2018 54
SSDN
21-07-2018 55
ANDROID WIDGETS

Textview
Edittext
Button
Checkbox
RadioButton
ImageView
ImageButton
RadioButton
ToggleButton
SSDN
21-07-2018 56
IMAGEVIEW

image = (ImageView) findViewById(R.id.imageView1);


button = (Button)
findViewById(R.id.btnChangeImage);
button.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) {
image.setImageResource(R.drawable.android3d); } });

SSDN
21-07-2018 57
IMAGEVIEW

<ImageView android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android" />

 <Button android:id="@+id/btnChangeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />
SSDN
21-07-2018 58
IMAGEVIEW

SSDN
21-07-2018 59
IMAGEBUTTON

ImageButton = (ImageButton)
findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new
OnClickListener() { @Override public void
onClick(View arg0) {
Toast.makeText(MyAndroidAppActivity.this,
"ImageButton is clicked!",
Toast.LENGTH_SHORT).show(); } });

SSDN
21-07-2018 60
IMAGEBUTTON

<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/android_button" />

SSDN
21-07-2018 61
IMAGEBUTTON

SSDN
21-07-2018 62
BUTTON

Android Button represents a push-button. The


android.widget.Button is subclass of TextView class
and CompoundButton is the subclass of Button class
There are different types of buttons in android such as
RadioButton, ToggleButton etc.

SSDN
21-07-2018 63
BUTTON

edittext1 = (EditText) findViewById(R.id.editText1);


edittext2 = (EditText) findViewById(R.id.editText2);
buttonSum = (Button) findViewById(R.id.button);

buttonSum.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String value1=edittext1.getText().toString();
String value2=edittext2.getText().toString();
int a=Integer.parseInt(value1);
int b=Integer.parseInt(value2);
int sum=a+b;
Toast.makeText(getApplicationContext(),String.valueOf(sum), Toast.LENGTH_
LONG).show();
}
});

SSDN
21-07-2018 64
CHECKBOX

 Android CheckBox is a type of two state button either checked


or unchecked.
 There can be a lot of usage of checkboxes. For example, it can
be used to know the hobby of the user, activate/deactivate the
specific action etc.
 Android CheckBox class is the subclass of CompoundButton
class.

SSDN
21-07-2018 65
CHECKBOX

pizza=(CheckBox)findViewById(R.id.checkBox);
coffe=(CheckBox)findViewById(R.id.checkBox2);
burger=(CheckBox)findViewById(R.id.checkBox3);
buttonOrder=(Button)findViewById(R.id.button);

//Applying the Listener on the Button click


buttonOrder.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {
int totalamount=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(pizza.isChecked()){
result.append("\nPizza 100Rs");
totalamount+=100;
}

SSDN
21-07-2018 66
CHECKBOX

if(coffe.isChecked()){
result.append("\nCoffe 50Rs");
totalamount+=50;
}
if(burger.isChecked()){
result.append("\nBurger 120Rs");
totalamount+=120;
}
result.append("\nTotal: "+totalamount+"Rs");
//Displaying the message on the toast
Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_LONG).sh
ow();
}

});

SSDN
21-07-2018 67
CHECKBOX

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="144dp"
android:layout_marginTop="28dp"
android:text="Burger"
/>

SSDN
21-07-2018 68
RADIOBUTTON

RadioButton is a two states button which is either


checked or unchecked. If a single radio button is
unchecked, we can click it to make checked radio
button. Once a radio button is checked, it cannot be
marked as unchecked by user.
RadioButton is generally used with RadioGroup.
RadioGroup contains several radio buttons, marking
one radio button as checked makes all other radio
buttons as unchecked.

SSDN
21-07-2018 69
RADIOBUTTON

In this example, we are going to implement single radio


button separately as well as radio button
in RadioGroup.

SSDN
21-07-2018 70
RADIOBUTTON

SSDN
21-07-2018 71
CUSTOM TOAST

LayoutInflater li = getLayoutInflater();
//Getting the View object as defined in the customtoast.xml file
View layout = li.inflate(R.layout.customtoast,(ViewGroup) findViewById(R.
id.custom_toast_layout));

//Creating the Toast object


Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setView(layout);//setting the view of custom toast layout
toast.show();

SSDN
21-07-2018 72
CUSTOM TOAST

activity_main.xml


<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" />

SSDN
21-07-2018 73
CUSTOM TOAST

custom.xml

 <ImageView
 android:id="@+id/custom_toast_image"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:contentDescription="Hello world"
 android:src="@drawable/jtp_logo"/>

 <TextView
 android:id="@+id/custom_toast_message"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:contentDescription="To"
 android:text=“SSDN custom Toast" />

SSDN
21-07-2018 74
• Intent in Android
• Adapters
• SharedPreferences

SSDN
21-07-2018 75
INTENT IN ANDROID

Intent is an intention to do something.


Intent contains an action carrying some information.
Intent is used to communicate between android
components.
 To start an activity

SSDN
21-07-2018 76
 Explicit Intents
 Implicit Intents

SSDN
21-07-2018 77
 Used to launch a specific component like activity or a
service.
In this case, android system directly forwards this intent to
that specific component.
 It is faster.
 Always use explicit intents if you know the specific activity
or service that performs intended thing

Intent i = new Intent(getApplicationContext(), ActivityTwo.class);


startActivity(i);

SSDN
21-07-2018 78
 Specifies an action that can invoke an app on the device
that can perform the action.
Useful when your app can not perform the action but
other apps do and you let user to pick up the app.
Its possible that there may not be any app that handles
the implicit intent.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.google.com"));
startActivity(intent);

SSDN
21-07-2018 79
FirstActivity
String value="Hello world";
Intent i = new Intent(FirstActivity.this,
SecondActivity.class);
i.putExtra("key",value);
startActivity(i);

SSDN
21-07-2018 80
SecondActivity
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");

SSDN
21-07-2018 81
Android provides many ways of storing data of an
application. Shared Preferences allow you to save and
retrieve data in the form of key,value pair.

SSDN
21-07-2018 82
 Preferences in Android are used to keep track of application
and user preferences.
 In any application, there are default preferences that can
accessed through the
 PreferenceManager instance and its related method
getDefaultSharedPreferences(Context)
 With the SharedPreference instance one can retrieve the int
value of the any preference with the getInt(String key, int
defVal).
 4. In our case, we can modify the SharedPreference instance in
our case using the edit() and use the putInt(String key, int
newVal)
SSDN
21-07-2018 83
SSDN
21-07-2018 84
• Adapters

SSDN
21-07-2018 85
ADAPTERS IN ANDROID

An Adapter object acts as a bridge between an


AdapterView and the underlying data for that view.
The Adapter provides access to the data items.
The Adapter is also responsible for making a View for each
item in the data set.

SSDN
21-07-2018 86
ADAPTERS CLASS

The AdapterView is a child class of ViewGroup


A special kind of container of view objects (list items)
 Typically you are going to use subsclasses of
AdapterView class instead of using it directly
Example subclasses of AdapterView class
ListView(contact list)
Spinner
GridView(Gallery)
An AdapterView access the data through Adapter
object Instead of accessing data directly itself
SSDN
21-07-2018 87
ADAPTERS RESPONSIBILITIES

 Filling the layout with data (it received through the


help of an Adapter)
Handling user selections - when a user selects an
item, perform some action

SSDN
21-07-2018 88
FILLING LAYOUT WITH DATA

Inserting data into the layout is typically done by


binding the AdapterView class to an Adapter, which
retrieves data from an external source(perhaps a list
that the code supplies or query results from the
device's database).

SSDN
21-07-2018 89
HANDLING USER SELECTIONS

You handle the user's selection by setting the class's


AdapterView.OnItemClickListener member to a
listener and catching the selection changes

SSDN
21-07-2018 90
HANDLING USER SELECTIONS

You handle the user's selection by setting the class's


AdapterView.OnItemClickListener member to a
listener and catching the selection changes

SSDN
21-07-2018 91
LISTVIEW CLASS

List of scrollable items can be displayed in Android


using ListView. It helps you to displaying the data in
the form of a scrollable list. Users can then select
any list item by clicking on it. ListView is default
scrollable so we do not need to use scroll View or
anything else with ListView.

SSDN
21-07-2018 92
LISTVIEW CLASS

SSDN
21-07-2018 93
LISTVIEW CLASS

STEP 1
Create a new project Listexample and activity Main Activity.
Here we will create a ListView in LinearLayout. Below is the
code of activity_main.xml or content_main.xml:

SSDN
21-07-2018 94
LISTVIEW CLASS
STEP 1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res
/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
andrandroid:dividerHeight="1dp" />

</LinearLayout>

SSDN
21-07-2018 95
LISTVIEW CLASS

STEP2
Create a new activity name Listview and below is the code of
activity_listview.xml

SSDN
21-07-2018 96
LISTVIEW CLASS
STEP2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi
d"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding=“10dp"
android:textColor=“#453647" />

</LinearLayout>

SSDN
21-07-2018 97
LISTVIEW CLASS

STEP3
Now in this final step we will use ArrayAdapter to display the
country names in UI. Below is the code of MainActivity.java

SSDN
21-07-2018 98
LISTVIEW CLASS
STEP3
public class MainActivity extends Activity
{
// Array of strings...
ListView simpleList;
String countryList[] = {"India", "China", "australia", "Portugle", "America", "New
Zealand"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layou
t.activity_listview, R.id.textView, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
Run your App……..YoooYooo
SSDN
21-07-2018 99
LISTVIEW CLASS – BASE ADAPTER

SSDN
21-07-2018 100
LISTVIEW CLASS

STEP1
Create a new projec Listebasexample and activity Main
Activity. Here we will create a ListView in LinearLayout. Below
is the code of activity_main.xml :

SSDN
21-07-2018 101
LISTVIEW CLASS
STEP1
<?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:orientation="vertical">

<ListView android:id="@+id/simpleListView"
android:layout_width=“match_parent"
android:layout_height="wrap_content"
android:dividerHeight="1dp" />

</LinearLayout>
SSDN
21-07-2018 102
LISTVIEW CLASS

STEP2
Create a new activity name Listview and below is the code of
activity_listview.xml

SSDN
21-07-2018 103
LISTVIEW CLASS
STEP2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal">

<ImageView android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp“
android:src="@drawable/ic_launcher" />

<TextView android:id="@+id/textView“
android:layout_width="fill_parent"
android:layout_height="wrap_content“
android:layout_gravity="center"
android:padding=“10dp"
android:textColor="@color/black" />
</LinearLayout>
SSDN
21-07-2018 104
LISTVIEW CLASS

STEP3
In third step we will use custom adapter to display the country
names in UI by coding MainActivity.java. Below is the code of
MainActivity.java

Important Note: Make sure flag images


are stored in drawable folder present
inside res folder with correct naming.

SSDN
21-07-2018 105
LISTVIEW CLASS
STEP3
public class MainActivity extends Activity
{
ListView simpleList;
String imagesList[] = {"India", "China", "australia", "Portugle", "America",
"NewZealand"};
int images[] = {R.drawable.india, R.drawable.china, R.drawable.australia,
R.drawable.portugle, R.drawable.america, R.drawable.new_zealand};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
CustomAdapter customAdapter = new
CustomAdapter(getApplicationContext(), imagesList, images);
simpleList.setAdapter(customAdapter);
}
}
SSDN
21-07-2018 106
LISTVIEW CLASS

STEP4
Now create another class Custom Adapter which will extend
BaseAdapter. Below is the code of CustomAdapter.java

SSDN
21-07-2018 107
GRIDVIEW CLASS

In android GridView is a view group that display


items in two dimensional scrolling grid (rows and
columns), the grid items are not necessarily
predetermined but they are automatically inserted
to the layout using a ListAdapter. Users can then
select any grid item by clicking on it. GridView is
default scrollable so we don’t need to
use ScrollView or anything else with GridView.

SSDN
21-07-2018 108
GRIDVIEW CLASS

SSDN
21-07-2018 109
GRIDVIEW CLASS

STEP 1
Create a new Android project in Android Studio and fill all
the required details. In our case we have named
GridViewExample and package
com.example.gourav.GridViewExample

SSDN
21-07-2018 110
GRIDVIEW CLASS

STEP 2
Open activity_main.xml and paste the below code. In this
we have created a Grid view inside Linear Layout and also
set number of columns to 3.

SSDN
21-07-2018 111
STEP 2 GRIDVIEW CLASS
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<GridView android:id="@+id/simpleGridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:padding="1dp“
android:numColumns="3" />
</LinearLayout>

SSDN
21-07-2018 112
GRIDVIEW CLASS

STEP 3
Create a new XML file and paste the below code. We have
named activity_gridview.xml.
In this step we create a new XML file and add
a ImageView in it.

SSDN
21-07-2018 113
STEP 3 GRIDVIEW CLASS
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/andr
oid" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp" android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="120dp"
android:scaleType="fitXY"
android:layout_gravity="center_horizontal"
android:src="@drawable/logo1" />//name of image
</LinearLayout>
SSDN
21-07-2018 114
GRIDVIEW CLASS

STEP4
Now open drawable folder and save small size png images
of different logo’s and name them like logo1,logo2 and etc.

SSDN
21-07-2018 115
GRIDVIEW CLASS

STEP 5- In this step firstly we get the reference of


GridView and then create a int type array for Android
logo’s. After that we call the CustomAdapter and pass the
array in it. At Last we implement setOnItemClickListener
event on GridView and on click of any item we send that
item to another Activity to show the logo image in Full Size.

SSDN
21-07-2018 116
GRIDVIEW CLASS

STEP 6
Create a new class CustomAdapter and
paste the below code.
In this step we create a CustomAdapter class
by extending BaseAdapter in it. In this step
we set the logo image’s in the grid items

SSDN
21-07-2018 117
GRIDVIEW CLASS

STEP 7
Now Create a new XML file named
activity_second and paste the below code in it.
In this step we create an XML file for our Second
Activity to display the logo image in full size.

SSDN
21-07-2018 118
GRIDVIEW CLASS
STEP 7
<RelativeLayout
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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#fff"
tools:context="com.example.gourav.GridViewExample.SecondAc
tivity">
<ImageView android:id="@+id/selectedImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleType="fitXY" />
</RelativeLayout>.
SSDN
21-07-2018 119
GRIDVIEW CLASS

STEP 8
 Now Create a new Activity with name
SecondActivity.class

SSDN
21-07-2018 120
GRIDVIEW
STEP 8
CLASS
public class SecondActivity extends AppCompatActivity
{
ImageView selectedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
selectedImage = (ImageView)
findViewById(R.id.selectedImage);
Intent intent = getIntent();
selectedImage.setImageResource(intent.getIntExtra("
image", 0));
}}
YOYO RUN APP……………………
SSDN
21-07-2018 121
SPINNER CLASS

In Android, Spinner provides a quick way to select


one value from a set of values. Android spinners are
nothing but the drop down-list seen in other
programming languages. In a default state,
a spinner shows its currently selected value. It
provides a easy way to select a value from a list of
values.

SSDN
21-07-2018 122
SPINNER CLASS

SSDN
21-07-2018 123
SPINNER CLASS

Step 1: Create a new project in Android Studio and


name it SpinnerExample.

SSDN
21-07-2018 124
SPINNER CLASS

Step 2: Open res -> layout -> activity_main.xml (or)


main.xml and add following code. Here we will
create a Spinner inside Relative Layout.

SSDN
21-07-2018 125
SPINNER CLASS
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/a
ndroid"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@+id/simpleSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />
</RelativeLayout>
SSDN
21-07-2018 126
SPINNER CLASS

Step 3: Now open app-> java -> package -


> MainActivity.java and add the following code.
Here we will use ArrayAdapter to fill the data in
Spinner. Also we are using Toast to display when the
item in Spinner is selected.

SSDN
21-07-2018 127
SPINNER CLASS

OUTPUT

SSDN
21-07-2018 128
• Networking in Android
• Broadcast Receivers
• Services
• Notifications
• Web View

SSDN
21-07-2018 129
 Accessing the network in Android
Permission to access the network
Check the network availability

SSDN
21-07-2018 130
Accessing the network in Android

Within an Android application you should avoid


performing long running operations on the user
interface thread. This includes file and network
access.
The Android system crashes your application with
a NetworkOnMainThreadExceptionexception, if you
access network is accessed from the main thread.

SSDN
21-07-2018 131
Permission to access the network

To access the Internet your application requires


the android.permission.INTERNETpermission. On
modern Android API versions, this permission is
automatically granted to your application

SSDN
21-07-2018 132
Check the network availability

public boolean isNetworkAvailable() {


ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo(); //
if no network is available networkInfo will be null //
otherwise check if we are connected if (networkInfo !=
null && networkInfo.isConnected()) { return true; } return
false;
}
SSDN
21-07-2018 133
Broadcast Receivers simply respond to broadcast
messages from other applications or from the system
itself. These messages are sometime called events or
intents. For example, applications can also initiate
broadcasts to let other applications know that some
data has been downloaded to the device and is
available for them to use, so this is broadcast receiver
who will intercept this communication and will initiate
appropriate action.
SSDN
21-07-2018 134
Creating the Broadcast Receiver.
Registering Broadcast Receiver

SSDN
21-07-2018 135
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Intent Detected.",
Toast.LENGTH_LONG).show();
}}
SSDN
21-07-2018 136
<application android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" > <receiver
android:name="MyReceiver"> <intent-filter> <action
android:name="android.intent.action.BOOT_COMPLET
ED"> </action> </intent-filter> </receiver>
</application>

SSDN
21-07-2018 137
SSDN
21-07-2018 138
Creating the Broadcast Receiver.
Registering Broadcast Receiver

SSDN
21-07-2018 139
A Service is an application component that can
perform long-running operations in the background,
and it doesn't provide a user interface. Another
application component can start a service, and it
continues to run in the background even if the user
switches to another application

SSDN
21-07-2018 140
 foreground service performs some operation that is
noticeable to the user. For example, an audio app
would use a foreground service to play an audio track.
Foreground services must display a Notification.
Foreground services continue running even when the
user isn't interacting with the app.

SSDN
21-07-2018 141
BackgroundA background service performs an
operation that isn't directly noticed by the user. For
example, if an app used a service to compact its
storage, that would usually be a background service.

SSDN
21-07-2018 142

Bound service is bound when an application component
binds to it by calling bindService(). A bound service offers
a client-server interface that allows components to
interact with the service, send requests, receive results,
and even do so across processes with interprocess
communication (IPC). A bound service runs only as long as
another application component is bound to it. Multiple
components can bind to the service at once, but when all
of them unbind, the service is destroyed.

SSDN
21-07-2018 143
To create a service, you must create a subclass
of Service or use one of its existing subclasses
onStartCommand()The system invokes this method by
calling startService() when another component (such
as an activity) requests that the service be started
nBind()The system invokes this method by
calling bindService() when another component wants
to bind with the service

SSDN
21-07-2018 144
onCreate()The system invokes this method to perform
one-time setup procedures when the service is initially
created (before it calls either onStartCommand()
or onBind()). If the service is already running, this method
is not called.
onDestroy()The system invokes this method when the
service is no longer used and is being destroyed. Your
service should implement this to clean up any resources
such as threads, registered listeners, or receivers. This is
the last call that the service receives.

SSDN
21-07-2018 145
A notification is a message you can display to the user
outside of your application's normal UI.

SSDN
21-07-2018 146
Step 1 - Create Notification Builder
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(this)

SSDN
21-07-2018 147
Step 2 - Setting Notification Properties
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("Notification Alert, Click
Me!");
mBuilder.setContentText("Hi, This is Android
Notification Detail!");

SSDN
21-07-2018 148
 Step 3 - Attach Actions

Intent resultIntent = new Intent(this, ResultActivity.class);


TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class); // Adds the
Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent); PendingIntent
resultPendingIntent =
stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_C
URRENT); mBuilder.setContentIntent(resultPendingIntent);

SSDN
21-07-2018 149
 Step 3 - Attach Actions

Intent resultIntent = new Intent(this, ResultActivity.class);


TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class); // Adds the
Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent); PendingIntent
resultPendingIntent =
stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_C
URRENT); mBuilder.setContentIntent(resultPendingIntent);

SSDN
21-07-2018 150
 Step 4- Issue the notification

NotificationManager mNotificationManager =
(NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE); //
notificationID allows you to update the notification later on.
mNotificationManager.notify(notificationID, mBuilder.build());

SSDN
21-07-2018 151
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview“
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

SSDN
21-07-2018 152
String url = ed1.getText().toString();
wv1.getSettings().setLoadsImagesAutomatically(true);
wv1.getSettings().setJavaScriptEnabled(true);
wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv1.loadUrl(url);

SSDN
21-07-2018 153
private class MyBrowser extends WebViewClient
{ @Override
public boolean shouldOverrideUrlLoading(WebView view, String
url) {
view.loadUrl(url);
return true;
}
}

SSDN
21-07-2018 154
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sairamkrishna.myapplication" >
<uses-permission
android:name="android.permission.INTERNET" />

<application android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
SSDN
21-07-2018 155
• Alarm Manager
Fragment
• Navigation Drawer
• View Pager

SSDN
21-07-2018 156
By the help of Android AlarmManager in android, you
can schedule your application to run at a specific
time in the future. It works whether your phone is
running or not.

SSDN
21-07-2018 157
activity_main.xml

SSDN
21-07-2018 158
MainActivity.java

SSDN
21-07-2018 159
MyBroadcastReceiver .java

SSDN
21-07-2018 160
AndroidManifest.xml

SSDN
21-07-2018 161
AndroidManifest.xml

SSDN
21-07-2018 162
SSDN
21-07-2018 163
The navigation drawer is a UI panel that shows your app's main
navigation menu. It is hidden when not in use, but appears when
the user swipes a finger from the left edge of the screen or,
when at the top level of the app, the user touches the drawer
icon in the app bar.

SSDN
21-07-2018 164
The navigation drawer is a UI panel that shows your
app's main navigation menu. It is hidden when not in
use, but appears when the user swipes a finger from the
left edge of the screen or, when at the top level of the
app, the user touches the drawer icon in the app bar.

SSDN
21-07-2018 165
<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />

SSDN
</android.support.v4.widget.DrawerLayout> 166
21-07-2018
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view" />

SSDN
21-07-2018 167
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/nav_header" />

SSDN
21-07-2018 168
class MainActivity : AppCompatActivity() {

private lateinit var mDrawerLayout: DrawerLayout

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

mDrawerLayout = findViewById(R.id.drawer_layout)

val navigationView: NavigationView = findViewById(R.id.nav_view)


navigationView.setNavigationItemSelectedListener { menuItem ->
// set item as selected to persist highlight
menuItem.isChecked = true
// close drawer when item is tapped
mDrawerLayout.closeDrawers()

// Add code here to update the UI based on the item selected


// For example, swap UI fragments here

true
SSDN
} 21-07-2018 169
}
<RelativeLayout 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"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

SSDN
21-07-2018 170
ViewPager viewPager = (ViewPager)
findViewById(R.id.viewpager);

viewPager.setAdapter(new CustomPagerAdapter(this));

SSDN
21-07-2018 171
public enum ModelObject {
RED(R.string.red, R.layout.view_red), BLUE(R.string.blue,
R.layout.view_blue), GREEN(R.string.green,
R.layout.view_green);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId)
{
mTitleResId = titleResId; mLayoutResId = layoutResId; }
public int getTitleResId() {
return mTitleResId; }
public int getLayoutResId() {
return mLayoutResId;
}}
SSDN
21-07-2018 172
CustomAdapter.java

SSDN
21-07-2018 173
SSDN
21-07-2018 174
SSDN
21-07-2018 175

You might also like