Mobile Design – Android
Copyright © 2017-2021 Marco Papa and Sunny Patel 1
Outline
• Android – background, architecture and features
• Android Studio basics
• Basic concepts – Resources, Activity, Services, etc.
• Event handling
• HTTP networking and JSON parser
• Running application on an Android device/Emulator
• References
Copyright © 2017-2021 Marco Papa and Sunny Patel 2
Background
• Android is an open source and Linux-based Operating System for smartphones.
• Android Inc. was founded in Palo Alto, California in October 2003 by Andy Rubin, Rich Miner, Nick
Sears, and Chris White.
• Android was pitched as a handset operating system that would rival Symbian and Microsoft Windows
Mobile.
• In July 2005,Google acquired Android Inc. for at least $50 million. Its key employees, including Rubin,
Miner and White, joined Google as part of the acquisition
• On November 5, 2007, the Open Handset Alliance, a consortium of technology companies including
Google, device manufacturers such as HTC, Motorola and Samsung, wireless carriers such as Sprint
and T-Mobile, and chipset makers such as Qualcomm and Texas Instruments, unveiled itself, with a
goal to develop “the first truly open and comprehensive platform for mobile devices”.
• Developers need to develop only once for Android and the applications run across all Android devices.
• The first beta version of the Android Software Development Kit (SDK) was released by Google in 2007
whereas the first commercial version, Android 1.0, was released in September 2008.
• The first commercially available smartphone running Android was the HTC Dream, also known as T-
Mobile G1, announced on September 23, 2008.
Copyright © 2017-2021 Marco Papa and Sunny Patel 3
Features
• Open Source
• Larger Developer Community
• High Marketing
• Rich Development Environment
• Inter App Integration Feature
• High Security
• Reduced Cost of Development
• Higher Success Ratio
Copyright © 2017-2021 Marco Papa and Sunny Patel 4
Categories of Applications
Copyright © 2017-2021 Marco Papa and Sunny Patel 5
Music News Multimedia Sports
Lifestyle Food &
Drinks
Books Weather
Reference Navigation Social
Media
History
Android Version Code name API level
1.0 APPLE PIE 1
1.1 BANANA BREAD 2
1.5 CUPCAKE 3
1.6 DONUT 4
2.0, 2.1 ECLAIR 5, 6, 7
2.2 FROYO 8
2.3 GINGERBREAD 9 and 10
3.0, 3.1, 3.2 HONEYCOMB 12 and 13
4.0 ICE CREAM SANDWICH 14, 15
4.1, 4.2, 4.3 JELLYBEAN 16, 17 and 18
4.4 KITKAT 19, 20
5.0, 5.1 LOLLIPOP 21, 22
6.0 MARSHMALLOW 23
7.0, 7.1 NOUGAT 24, 25
8.0, 8.1 OREO 26, 27
9.0 PIE 28
10.0 Android 10 29
11.0 Android 11 30
Copyright © 2017-2021 Marco Papa and Sunny Patel 6
Android 12
• Just released in October 2021
• See: https://www.android.com/android-12/
Copyright © 2017-2021 Marco Papa and Sunny Patel 7
Architecture
Android operating
system is a stack of
software
components which
is roughly divided
into five sections
and four main layers
as shown below in
the architecture
diagram.
Copyright © 2017-2021 Marco Papa and Sunny Patel 8
Architecture
Application
Framework
Runtime
Libraries
Linux Kernel
Linux Kernel
Linux Kernel
Hardware drivers
Hardware/software interface
Copyright © 2017-2021 Marco Papa and Sunny Patel 9
Architecture
Application
Framework
Runtime
Libraries
Linux Kernel
Libraries
android.app
android.content
android.database
android.opengl
android.os
android.text
android.view
android.widget
android.webkit
Copyright © 2017-2021 Marco Papa and Sunny Patel 10
Architecture
Application
Framework
Runtime
Libraries
Linux Kernel
Runtime
Dalvik Virtual Machine
Core libraries
Copyright © 2017-2021 Marco Papa and Sunny Patel 11
Architecture
Application
Framework
Runtime
Libraries
Linux Kernel
Application Framework
Activity Manager
Content Providers
Resource Manager
Notifications Manager
View System
Copyright © 2017-2021 Marco Papa and Sunny Patel 12
Android Studio Basics
• Setup
• Create a new project
• Get familiar with Android Studio
• Design UI in XML using drag and drop in xml resource layout editor
• Control UI using Activity
• Change Activity properties in Manifest file
• Run your app in the emulator
• Download at https://developer.android.com/studio/
Copyright © 2017-2021 Marco Papa and Sunny Patel 13
New Version Numbering
• Changed the version numbering system for Android Studio to more closely align with
IntelliJ IDEA, the IDE that Android Studio is based on.
• In the previous numbering system, latest release would have been numbered as Android
Studio 4.3 or version 4.3.0.1. With the new numbering system, it is now Android Studio
– Arctic Fox | 2020.3.1, or version 2020.3.1.
• Latest version Android Studio Arctic Fox | 2020.3.1 Patch 3 (October 2021)
Copyright © 2017-2021 Marco Papa and Sunny Patel 14
https://www.jetbrains.com/idea/
Setup
You can start Android application development on either of the following operating
systems −
• Microsoft Windows
• Mac OS X
• Linux
• Chrome OS (new!)
Following is the list of software’s you will need before you start your Android
application programming:
• Java Oracle JDK5 or later version
• Android Studio 4.X (supports all Java 7 language features plus some Java 8)
(latest version 4.3.0 as of October 2021) . Renumbered as 2020.3.1
• Android Studio 2020.3.1 bundles Kotlin
Copyright © 2017-2021 Marco Papa and Sunny Patel 15
Setup
• You can download the latest version of Java JDK from Oracle’s Java site − Java SE
Downloads: https://www.oracle.com/technetwork/java/javase/downloads/index.html
(latest is Java 16)
• Install and configure the setup. (JDK 7, 8, 11, 14 are recommended, see Java SE
Archives)
• Finally set PATH and JAVA_HOME environment variables to refer to the directory that
contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
• If you are running Windows and installed the JDK in C:\jdk1.8.0_102, you would have to
put the following line in your C:\autoexec.bat file.
set PATH=C:\jdk1.8.0_102\bin;%PATH%
set JAVA_HOME=C:\jdk1.8.0_102
Copyright © 2017-2021 Marco Papa and Sunny Patel 16
http://www.oracle.com/technetwork/java/javase/downloads/index.html
https://www.oracle.com/technetwork/java/javase/downloads/index.html
Setup
• Alternatively, you could also right-click on My Computer, select Properties,
then Advanced, then Environment Variables. Then, you would update the PATH value
and press the OK button.
• On Linux, if the SDK is installed in /usr/local/jdk1.8.0_102 and you use the C shell, you
would put the following code into your .cshrc file.
setenv PATH /usr/local/jdk1.8.0_102/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.8.0_102
• Alternatively, if you use Android Studio (recommended), then it will automatically
download all required components.
Copyright © 2017-2021 Marco Papa and Sunny Patel 17
Android Studio – Create a new project
Copyright © 2017-2021 Marco Papa and Sunny Patel 18
Android Studio – Create a new project (cont’d)
Copyright © 2017-2021 Marco Papa and Sunny Patel 19
Get familiar with Android Studio
Toolbar Navigation bar Editor window
Tool window bar
Tool windows
Status bar Copyright © 2017-2021 Marco Papa and Sunny Patel 20
Resources
• Types of resources: anim, color, drawable, layout, menu, raw, values, xml.
• Alternately, you can create a new directory in res/ named in the
form
• To access res/drawable/myimage.png and set an ImageView you will use
following code −
ImageView imageView = (ImageView)findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
Copyright © 2017-2021 Marco Papa and Sunny Patel 21
Activity
• An activity represents a single screen with a user interface.
• Android system initiates its program within an Activity starting with a call
to onCreate() callback method.
Life-cycle of an Activity:
1. onCreate(): This is the first callback and called when the activity is first
created.
2. onStart(): This callback is called when the activity becomes visible to the
user.
3. onResume(): This is called when the user starts interacting with the app.
Copyright © 2017-2021 Marco Papa and Sunny Patel 22
Activity (cont’d)
4. onPause(): This callback is called when the
current activity is being paused and the
previous activity is being resumed. During
this time, it does not receive user input or
execute any code.
5. onStop(): This callback is called when the
activity is no longer visible.
6. onDestroy(): This callback is called before
the activity is destroyed by the system.
7. onRestart(): This callback is called when
the activity restarts after getting stopped.
Copyright © 2017-2021 Marco Papa and Sunny Patel 23
Services
A service runs in the background to perform a
task without user interaction and keeps working
even if application is destroyed.
A service can essentially take two states −
1. Started: A service is started when an activity
starts it by calling startService().
2. Bound: A service is bound when an
application component binds to it by
calling bindService().
Copyright © 2017-2021 Marco Papa and Sunny Patel 24
Broadcast Receivers
• A Broadcast Receiver reacts to the system generated messages or the messages
generated from other applications. These messages are called events or intents.
• The following steps are needed to use and respond to events using Broadcast
Receiver:
1. Creating the Broadcast Receiver: A receiver can be created in an Activity by
extending the abstract class BroadcastReceiver.
1. Registering Broadcast Receiver: A receiver can be registered in Android
Manifest file.
Copyright © 2017-2021 Marco Papa and Sunny Patel 25
Content Providers
• Often data from one application is needed in another application. A content provider
supplies data on demand.
• Using content providers, you can have centralized data and other applications can
access it whenever needed. It behaves more like a database which can be queried,
updated, inserted into like any other DB.
Copyright © 2017-2021 Marco Papa and Sunny Patel 26
Fragments
• A Fragment is often used for a more modular design- it may or may not be a part of an
Activity. It can also be considered as a sub-activity.
• A fragment can run without a user interface as well.
• A fragment has its own layout and its own behavior with its own life cycle callbacks.
• It is possible to dynamically add fragments to a running activity.
• A fragment once created, can be used in multiple activities.
• A fragment’s life cycle is closely knitted with its parent activity’s life cycle. When the
activity is paused or stopped, fragments also replicate the same status.
• Usually, tablets have multiple fragments running side by side, in one activity (example
on next slide)
Copyright © 2017-2021 Marco Papa and Sunny Patel 27
Life cycle of a Fragment
Copyright © 2017-2021 Marco Papa and Sunny Patel 28
Intents & Intent Filters
• An Android Intent is a messaging object you can use to request an action from
another app component. The 3 main use cases are:
• Starting an Activity
• Starting a Service
• Delivering a broadcast
The primary information contained in an Intent Object is the following:
• Component name: The name of the component to start.
• Action: A string that specifies the generic action to perform (such
as view or pick). This is mandatory part of the Intent object.
• Data: The URI (a Uri object) that references the data to be acted on and/or the
MIME type of that data.
Copyright © 2017-2021 Marco Papa and Sunny Patel 29
https://developer.android.com/reference/android/net/Uri.html
Intents & Intent Filters (cont’d)
• Category: A string containing additional information about the kind of component
that should handle the intent. The category is an optional part of Intent object.
• Extras: This will be in key-value pairs for additional information that should be
delivered to the component handling the intent.
• Flags: Flags function as metadata for the intent. The flags may instruct the
Android system how to launch an activity and how to treat it after it’s launched.
Example:
Intent i = new Intent(FirstActivity.this,
SecondActivity.class);
startActivity(i); // Starts SecondActivity
Copyright © 2017-2021 Marco Papa and Sunny Patel 30
UI Layouts
A layout, usually defined in XML, describes the structure and placement of UI
components for an Activity. You can declare a layout in two ways:
• Declare UI elements in XML. Android provides a straightforward XML
vocabulary that corresponds to the View classes and subclasses, such as those for
widgets and layouts.
• Instantiate layout elements at runtime. Your application can create View and
ViewGroup objects (and manipulate their properties) programmatically.
Next slide: Example code for defining a TextView and a Button in Linear Layout.
Copyright © 2017-2021 Marco Papa and Sunny Patel 31
UI Layouts (cont’d)
Loading the XML resource in Java:
Copyright © 2017-2021 Marco Papa and Sunny Patel 32
UI Layouts (cont’d)
Common Layouts
Copyright © 2017-2021 Marco Papa and Sunny Patel 33
Input Controls
Input Controls controls are the interactive components in your app’s user interface.
Android provides a wide variety of controls you can use in your UI, such as buttons,
text fields, seek bars, check box, zoom buttons, toggle buttons, and many more.
Common controls include:
1. TextView: This control is used to display text to the user.
2. EditText: This is a predefined subclass of TextView that includes rich editing
capabilities.
3. Button: A push-button that can be pressed, or clicked, by the user to perform an
action.
4. AutoCompleteView: Similar to EditText, except that it shows a list of
completion suggestions automatically while the user is typing.
Copyright © 2017-2021 Marco Papa and Sunny Patel 34
Input Controls (cont’d)
5. ImageButton: This shows a button with an image (instead of text) that can be
pressed or clicked by the user.
6. CheckBox: An on/off switch that can be toggled by the user. You should use check
box when presenting users with a group of selectable options that are not mutually
exclusive.
7. RadioButton: The RadioButton has two states: either checked or unchecked.
8. ProgressBar: The ProgressBar view provides visual feedback about some ongoing
tasks, such as when you are performing a task in the background.
9. Spinner: A drop-down list that allows users to select one value from a set.
10. TimePicker: The TimePicker view enables users to select a time of the day, in
either 24-hour mode or AM/PM mode.
11. DatePicker: The DatePicker view enables users to select a date of the day.
Copyright © 2017-2021 Marco Papa and Sunny Patel 35
Event Handling
Events are the way a User interacts with the Android system like button presses or
screen touch etc.
The Android framework maintains an event queue as first-in, first-out (FIFO) basis.
You can capture these events in your program and take appropriate action as per
requirements.
There are three concepts related to Android Event Management.
• Event Listeners − An event listener is an interface in the View class that contains
a single callback method.
• Event Listeners Registration − Event Registration is the process by which an
Event Handler gets registered with an Event Listener so that the handler is called
when the Event Listener fires the event.
Copyright © 2017-2021 Marco Papa and Sunny Patel 36
Event Handling (cont’d)
• Event Handlers − When an event happens and we have registered an event
listener for the event, the event listener calls the Event Handlers, which is
the method that actually handles the event.
Two ways of Handling events:
1. Using Event Listeners
Copyright © 2017-2021 Marco Papa and Sunny Patel 37
Event Handling (cont’d)
2. Using Event Handler (after declaring method name in the XML)
Copyright © 2017-2021 Marco Papa and Sunny Patel 38
Design UI in XML
Copyright © 2017-2021 Marco Papa and Sunny Patel 39
Calculator Demo – Control UI & Logic using Activity
setContentView() sets the first UI –
activity_main.xml
Steps to produce a Grid UI:
1. Define an ArrayAdapter which would
contain a list of items.
2. findViewById(R.id.gridView1) is used to
connect UI elements with ID – gridView1
with the Java code.
3. Set the adapter in the Grid view using the
code gridView.setAdapter(ad);
4. gridView.setOnItemClickListener() is the
click listener for the grid items of the
calculator.
Copyright © 2017-2021 Marco Papa and Sunny Patel 40
Control UI & Logic using Activity
Logic for Calculator:
• While consecutive numbers are pressed, treat
them as part of a single number by
multiplying the term by 10 every time.
• While “+” is clicked, keep updating the UI
with the current number.
• When “=” is clicked, it marks the end of an
addition. Add up all the numbers and display
in the result bar.
• Add all the queries to a HashMap to display
it in the History section.
• History is temporary.
Copyright © 2017-2021 Marco Papa and Sunny Patel 41
Change Activity properties in Manifest file
1. Every Activity that is created
must be defined in the Manifest
file under application tag.
2. The action for the intent filter
indicates that this activity
serves as the entry point for the
application.
3. The category for the intent-
filter indicates that the
application can be launched
from the device’s launcher icon.
4. Parent activity can be shown
for the up-arrow navigation.
Copyright © 2017-2021 Marco Papa and Sunny Patel 42
Run your app in the Emulator
To run your app in your own Android device, tap on Build Number from Settings several
times. This will enable developer options in your phone. Then enable USB debugging.
To run your app in the Emulator, make sure you have correct SDK & tools installed. Verify
using the following steps:
1. Click on the Project Structure icon and then click on the “app” section under modules.
1. Under properties, check the Compile SDK Version and Build Tools Version. These should
be same.
2. Under Flavors, Min Sdk Version should be lower than the Compile SDK Version.
AVD Manager Project Structure SDK ManagerRun the app
Copyright © 2017-2021 Marco Papa and Sunny Patel 43
Run your app in the Emulator (cont’d)
2. Click on Tools -> SDK Manager icon and then Appearance & Behavior-
>Android SDK from the left menu.
1. Under SDK Platforms, make sure the Compile Android version you selected in the
last step is installed.
2. Under SDK tools, make sure Android Emulator, Android SDK Platform Tools,
Android SDK Tools & Intel x86 Emulator Accelerator are all installed.
3. Click on Tools -> AVD Manager and then click on Create Virtual Device to fire
up a new Emulator.
1. Under Phone category, click on the profile (mobile type) that you wish to start.
Click Next.
2. Under Recommended section, make sure the Release is already downloaded. If not,
download it. Click Next.
3. Check the AVD Name. That will be the name of the device shown every time you
start Emulator. Click Finish.
4. Now you will see the same device under Your Virtual Devices. Click on the green
play button on the right to start the Emulator.
Copyright © 2017-2021 Marco Papa and Sunny Patel 44
Calculator Demo
Copyright © 2017-2021 Marco Papa and Sunny Patel 45
Working with 3rd party libraries
• There are many times when we need to depend on 3rd party libraries to achieve
functionality we want from our app.
• Using 3rd party libraries helps save time as we don’t have to reinvent the wheel
when somebody else has done it for you. This also saves a lot on debugging time.
• We often use Gradle to add the libraries as dependencies to our project (although
it is possible to manually download and include the library, but this is not
preferred).
• Gradle is a flexible tool that allows us to configure how our project is complied,
built and packaged into our final app.
• Apart from managing dependencies, Gradle is also used to manage different build
variants (such as a debug version with logging enabled vs a release version) or
project flavors (such as free vs paid versions of the app).
• Latest version is Android Gradle plugin 7.0.0
• Release Docs: https://developer.android.com/studio/releases/gradle-plugin#7-0-0
Copyright © 2017-2021 Marco Papa and Sunny Patel 46
Using Gradle for dependency management
• It is fairly simple to add third party libraries to our project via Gradle.
• We need to add the following in the build.gradle file for our app module.
• We can find the above details on the webpage of the library we want to use.
• The build system will automatically find these libraries, download them and
include them in your project while building your project. You need not manually
download the libraries into your project.
• Note: In many examples online you may find ‘compile’ instead of
‘implementation’. Compile was recently deprecated in favor of implementation,
but both should work in most cases.
Copyright © 2017-2021 Marco Papa and Sunny Patel
47
Common 3rd party libraries
• In most projects, the following libraries are useful with simplifying common tasks:
• Volley
• Picasso
• Glide
• CircularScoreView
• Gson
• MPAndroidChart
Copyright © 2017-2021 Marco Papa and Sunny Patel 48
Volley – HTTP Networking and JSON Parsing
• Volley is an HTTP library that makes networking for Android apps easier and
most importantly, faster. Volley is available on GitHub.
https://github.com/google/volley
Two ways to include Volley in your project:
1. The easiest way to add Volley to your project is to add the following
dependency to your app’s build.gradle file:
1. You can also clone the Volley repository and set it as a library project:
1. Git clone the repository by typing the following at the command line:
git clone https://github.com/google/volley
2. Import the downloaded source into your app project as an Android library module.
Copyright © 2017-2021 Marco Papa and Sunny Patel 49
https://github.com/google/volley
https://github.com/google/volley
https://github.com/google/volley
Volley – HTTP Networking and JSON Parsing (cont’d)
To use Volley, you must add the android.permission.INTERNET permission to your
app’s manifest. Without this, your app won’t be able to connect to the network.
Copyright © 2017-2021 Marco Papa and Sunny Patel 50
Volley at work
Copyright © 2017-2021 Marco Papa and Sunny Patel 51
Picasso – Image downloading and caching
• Today’s apps are rich in images that enhance the user interface. But images in
apps add many complications.
• Images are large in size and take time to download and then render. If we wait for
all images to load before showing anything, it can lead to a bad user experience.
• Sometimes the user may exit the screen before the image finishes downloading.
We need to intelligently cancel the image download in this case.
• The same images are often shown multiple times and need to be cached to
improve performance.
• Often we need to transform our images after downloading (like cropping and
resizing).
• We also need to handle cases where the image download fails and placeholder
images are needed while the actual image loads.
• Picasso has simple functions to take care of this, so that we focus on developing
our app.
Copyright © 2017-2021 Marco Papa and Sunny Patel 52
Picasso – Image downloading and caching
• We can easily add Picasso to our project using Gradle.
implementation ‘com.squareup.picasso:picasso:2.71828’
• We can then import the Picasso class into the .java file where we wish to Picasso
to load images.
import com.squareup.picasso.Picasso;
• For most cases, this one line of code is all we need to load our image:
Picasso.with(context).load(“http://ourdomainname/image.png”)
.into(imageView);
This takes care of asynchronously downloading the image, caching and auto-
canceling download when no longer needed.
Copyright © 2017-2021 Marco Papa and Sunny Patel 53
Picasso – Image downloading and caching
• Simple methods also exist to resize, rotate and crop the image.
Picasso.with(context).load(“http://ourdomainname/image.png”)
.resize(width, height)
.rotate(degrees)
.centerCrop()
.into(imageView);
• Picasso also provides methods for placeholder images while our image loads and
for the case where our image download failed.
Picasso.with(context).load(“http://ourdomainname/image.png”)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.into(imageView);
Copyright © 2017-2021 Marco Papa and Sunny Patel 54
Glide – Image downloading and caching
• Another library like Picasso for downloading and caching images and GIFs.
• Installation:
dependencies {
implementation ‘com.github.bumptech.glide:glide:4.9.0’
annotationProcessor ‘com.github.bumptech.glide:compiler:4.9.0’
}
Usage:
Glide.with(myFragment).load(url).centerCrop().
placeholder(R.drawable.loading_spinner).into(myImageView);
• Compatible with Volley HTTP calls
• More info on: https://github.com/bumptech/glide
55Copyright © 2017-2021 Marco Papa and Sunny Patel
https://github.com/bumptech/glide
Gson – serializing and desterilizing Java objects
• Gson is a library that is used to convert Java objects into their JSON
representation and vice-versa.
• With this you can easily convert your Java objects to their JSON string
representations. This is very useful when you need to send data between activities
or when you need to store an object for it to persist the next time you use the app.
• Another use case is for communicating with APIs that send/receive JSON data.
• This saves us from writing code to parse our JSON into Java objects and vice
versa. We only need to define the model of the data that we expect to receive and
the library will take care of the parsing.
Copyright © 2017-2021 Marco Papa and Sunny Patel 57
Gson – serializing and desterilizing Java objects
• We can easily add Gson to our project using Gradle.
implementation ‘com.google.code.gson:gson:2.8.2’
• Lets assume we get this JSON object from our API:
[
{
“name”: “John Doe”,
“email_id”:
},
{
“name”: “Jane Doe”,
“email_id”:
}
]
Copyright © 2017-2021 Marco Papa and Sunny Patel 58
Gson – serializing and desterilizing Java objects
• Let’s use the following class to store data received from our API:
//this class defines the model of our JSON data
public class Contact {
//we can use the same name as the field in the JSON object
//no special annotation is needed in this case
String name;
//We can also use our names using the
//@SerializedName annotation
@SerializedName(“email_id”) //name of field in JSON object
String emailID; //our object name
}
Copyright © 2017-2021 Marco Papa and Sunny Patel 59
Gson – serializing and desterilizing Java objects
• In our onResponse() method for our Volley HTTP request, we can directly parse the
response into an array of Java objects
JsonObjectRequest jsonObjectRequest = new
JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener
@Override
public void onResponse(String response) {
Contact[] contacts = (Contacts[])
gson.fromJson(response, Contact[].class);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//…
}
});
• Like so, we have directly parsed our JSON response.
Copyright © 2017-2021 Marco Papa and Sunny Patel 60
MPAndroidChart – Open Source graphing Library
• “A powerful and easy to use chart library for Android”. Open Source, can be found here
• Used to provide various charts like LineChart, BarChart, PieChart
• Create a List of Entry objects where each Entry is X vs Y values. Convert each
EntryList to a DataSet and call the charting method.
• Dependency and usage:
dependencies {
implementation ‘com.github.PhilJay:MPAndroidChart:v3.1.0’
}
List
List
for (int i = 0; i < daily.length(); i++) {
Float fmin = …;// Minimum temperature from json
Float fmax = ; // Maximum temperature from json
entries1.add(new Entry(i, fmin));
entries2.add(new Entry(i, fmax));
}
Copyright © 2017-2021 Marco Papa and Sunny Patel 61
https://github.com/PhilJay/MPAndroidChart
MPAndroidChart – Usage
//Create a LineDataSet for each LineChart and change properties
LineDataSet dataSet1 = new LineDataSet(entries1, "Minimum Temperature");
dataSet1.setColor(Color.parseColor("#BB86FC"));
LineDataSet dataSet2 = new LineDataSet(entries2, "Maximum Temperature");
dataSet2.setColor(Color.parseColor("#FAAB1A"));
//Render the chart
List
dataSets.add(dataSet1);
dataSets.add(dataSet2);
LineData data = new LineData(dataSets);
LineChart chart = (LineChart) view.findViewById(R.id.chart);
chart.setData(data);
chart.invalidate(); // refresh
To change the style use:
Chart.getLegend().set__() and chart.getAxis___().set___() methods
Copyright © 2017-2021 Marco Papa and Sunny Patel 62
References
• https://developer.android.com/studio/intro/index.html
• https://www.tutorialspoint.com/android
• https://www.javatpoint.com/android-tutorial
• http://socialcompare.com/en/comparison/android-versions-comparison
• http://square.github.io/picasso/
• https://github.com/google/gson
• https://kylewbanks.com/blog/tutorial-parsing-json-on-android-using-gson-and-
volley
Copyright © 2017-2021 Marco Papa and Sunny Patel 63
https://developer.android.com/studio/intro/index.html
https://www.tutorialspoint.com/android
https://www.javatpoint.com/android-tutorial
http://square.github.io/picasso/
https://github.com/google/gson
https://kylewbanks.com/blog/tutorial-parsing-json-on-android-using-gson-and-volley