This example show how to use Google Charts to display pie chart on Android WebView. Google chart tools are powerful, simple to use, and free. It not only display a static graphic, but also provide user touch interactive operation, check the video on the bottom.
Display Google Charts (pie chart) displayed Android WebView
When User click on the "Show Chart" button, it will start another activity, ShowWebChartActivity.java, and pass user data. ShowWebChartActivity load a WebView with our HTML to display Google Charts with Javascript. We have to implement WebAppInterface, with methods of @JavascriptInterface, getNum1()...getNum5(). It will be called by Javascript inside HTML to retrieve user data.
package com.example.androidwebchart;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
@SuppressLint("SetJavaScriptEnabled")
public class ShowWebChartActivity extends ActionBarActivity {
WebView webView;
int num1, num2, num3, num4, num5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_webchart);
Intent intent = getIntent();
num1 = intent.getIntExtra("NUM1", 0);
num2 = intent.getIntExtra("NUM2", 0);
num3 = intent.getIntExtra("NUM3", 0);
num4 = intent.getIntExtra("NUM4", 0);
num5 = intent.getIntExtra("NUM5", 0);
webView = (WebView)findViewById(R.id.web);
webView.addJavascriptInterface(new WebAppInterface(), "Android");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/chart.html");
}
public class WebAppInterface {
@JavascriptInterface
public int getNum1() {
return num1;
}
@JavascriptInterface
public int getNum2() {
return num2;
}
@JavascriptInterface
public int getNum3() {
return num3;
}
@JavascriptInterface
public int getNum4() {
return num4;
}
@JavascriptInterface
public int getNum5() {
return num5;
}
}
}
/res/layout/layout_webchart.xml, layout of ShowWebChartActivity.
/assets/chart.html, our HTML to load and display Google Charts. Actually it is modified from Google Quick Start example of pie chart. The main difference is it retrieve user data by calling Android JavascriptInterface methods (getNum1()...getNum5()), instead of fixed data.
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'ITEM');
data.addColumn('number', 'VALUE');
data.addRows([
['Item 1', Android.getNum1()],
['Item 2', Android.getNum2()],
['Item 3', Android.getNum3()],
['Item 4', Android.getNum4()],
['Item 5', Android.getNum5()]
]);
// Set chart options
var options = {'title':'Android-er: Load Google Charts (pie chart) with WebView',
'width':600,
'height':600};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div" style="width:600; height:600"></div>
</body>
</html>
Finally, modify AndroidManifest.xml to add <activity> of ShowWebChartActivity, and <uses-permission> of "android.permission.INTERNET".
Here's a simple example of a page that displays a pie chart:
You can copy the snippet below to an .html file on your computer and open it in your browser to display
the pie chart shown above:
<html><head><!--Load the AJAX API--><scripttype="text/javascript"src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">// Load the Visualization API and the corechart package.
google.charts.load('current',{'packages':['corechart']});// Set a callback to run when the Google Visualization API is loaded.
google.charts.setOnLoadCallback(drawChart);// Callback that creates and populates a data table,// instantiates the pie chart, passes in the data and// draws it.function drawChart(){// Create the data table.var data =new google.visualization.DataTable();
data.addColumn('string','Topping');
data.addColumn('number','Slices');
data.addRows([['Mushrooms',3],['Onions',1],['Olives',1],['Zucchini',1],['Pepperoni',2]]);// Set chart optionsvar options ={'title':'How Much Pizza I Ate Last Night','width':400,'height':300};// Instantiate and draw our chart, passing in some options.var chart =new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);}</script></head><body><!--Div that will hold the pie chart--><divid="chart_div"></div></body></html>
How About a Bar Chart?
Convert the pie chart to a bar chart by replacing
google.visualization.PieChart with
google.visualization.BarChart in the code and reloading your browser. You
may notice that the "Slices" legend is truncated. To fix this, change width to 500
from 400, save the file, and reload your browser.
How to use $_SERVER[‘REMOTE_ADDR’] to get current user IP address and show on screen.
Detecting and saving current user ip is one of the most important things for every website because at the present times lots of people trying to access your website to unleash the wrong content on internet and if you have stored all visitors IP address then this will help you to track down the wrong persons. So this is also a security point. PHP gives us inbuilt $_SERVER methods to access the real time visitor ip address. So here is the complete step by step tutorial for How to get current real User/Client ip address in PHP and disply on screen.
How to change Max: 2,048KiB PhpMyAdmin db upload size and increase its size as user requirement in Windows. By default PhpMyAdmin can only able to import Max: 2,048KiB SQL database size which is not so cool, But PHP developers can increase its size as per theirs project requires and upload more then…
Choose UI theme between Original and Pmahomme inside phpmyadmin control panel. There are by default two type of UI themes available inside phpmyadmin control panel. Php web developer can select any theme between them and set by default UI theme. So here is the complete step by step tutorial for Change UI theme…
Solve retry to connect xampp error. Sometimes process of changing username or password into phpmyadmin xampp control panel by mistake you will change its some other settings and it will display you a error message ” MySQL said Cannot connect invalid settings ” and after then phpmyadmin user cannot starts phpmyadmin…
Drop multiple MySQL tables inside database by checkbox selected table selection. Checkbox automatic selection gives us the facility to choose between multiple tables selection so phpmyadmin user can select multiple tables at a single time and delete them combine without deleting one by one. So here is the complete step by…
Import MySQL database including tables with the use of SQL dump(Downloaded) file. Restoring MySQL database backup is one of the most important task after loosing your all data by some software problem, virus problem. But its very easy to get back your complete database by simply using your backup file…
Export your complete MySQL database into .sql file,XML,CSV,MS Excel,JSon,Latex,MediaWiki table,pdf,php array,text,Yaml format. Creating backup means copying your whole MySQL database into single file format and save some other place so if you loose your database for some kind of reason then you must be able to recover your whole database+tables.…
Completely (Truncate)empty all rows, columns(Entire table) inside MySQL table using xampp. Deleting all records from MySQL database are also called as truncate the whole table and removing all the records inside selected table. PhpMyAdmin gives us the ability to remove tables data with one button click selection tool. So here…
Manually submit records value inside MySQL database table through phpmyadmin control panel without php coding. PhpMyAdmin control panel gives us the facility to directly information submission to MySQL database table this facility are called as Direct Data insertion . With the use of this method php website developer can manually add records…
How to drop (Remove) table in mysql database in PhpMyAdmin example step by step. MySQL database tables can be easily delete through phpmyadmin control panel with simple one button click method. Deleting MySQL tables inside database makes it more easy to organize because web developer can delete all the un-used…
How to easily Drop complete MySQL database. Deleting MySQL database is the most important process of php online web development because when php developers starts creating php sample code projects then there are lots of database created each & every time so xampp phpmyadmin control panel loads slowly but after…
How to Manually add tables to existing database in PhpMyAdmin. Tables are most important part of a MySQL database because without tables database are no more important for its user because all the dynamic content also known as information stores into MySQL tables into rows & columns form. So here…
Create dynamically database in mysql using php in xampp localhost server step by step tutorial. Xampp localhost server gives us the ability to make MySQL database with on click method. Database performs most important role in development of php because php is dynamic server side scripting language and all the…
Test,run,execute php scripting program code files using Xampp localhost server on computer. Php files can be easily executable after installing Xampp localhost server on windows. So firstly read my tutorial about how to install Xampp sever on windows platform and install xampp server on your computer. After done installing xampp…
Complete installation guide for xampp server including Apache ,MySQL to develop php programs. Xampp is one of the best open source software freely distribute by apachefriends.org develop php web applications. Xampp provide us the server environment on our local system because php is completely server side scripting programming language and cannot run…