Thursday, March 21, 2013

Android Using Google Chart API'S

we can use the chart api's of google with simple example shown here. make a project and have  a main activity 

1)MainActivity.java



import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
EditText etTitle, etAB, etAC, etBC, etABC;
Button btnshow;

String ddd = "http://chart.apis.google.com/chart?chf=bg,lg,0,EFEFEF,0,BBBBBB,1&chs=300x225&cht=v&chco=FF6342,ADDE63,63C6DE&chdl=A|B|C&chd=t:100,80,60,30,30,30,10";
String google_chart_api = "http://chart.apis.google.com/chart?chf=bg,lg,0,EFEFEF,0,BBBBBB,1&chs=300x225&cht=v&chco=FF6342,ADDE63,63C6DE&chdl=A|B|C&chd=t:100,80,60,";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

etAB = (EditText) findViewById(R.id.editText1);
etAC = (EditText) findViewById(R.id.editText2);
etBC = (EditText) findViewById(R.id.editText3);
etABC = (EditText) findViewById(R.id.editText4);
etTitle = (EditText) findViewById(R.id.editText5);

btnshow = (Button) findViewById(R.id.button1);
btnshow.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

String strAB = etAB.getText().toString().trim();
String strAC = etAC.getText().toString().trim();
String strBC = etBC.getText().toString().trim();
String strABC = etABC.getText().toString().trim();
String strTitle = etTitle.getText().toString().trim();

if (TextUtils.isEmpty(strAB) && TextUtils.isEmpty(strAC)
&& TextUtils.isEmpty(strBC)
&& TextUtils.isEmpty(strABC)
&& TextUtils.isEmpty(strTitle)) {
showToast("All Fields Are Mandatory!!!");
} else if (!(MyUtility.isNetworkAvailable(MainActivity.this))) {
showToast("No Internet Connection!!!");
} else {

StringBuilder sb = new StringBuilder(google_chart_api);
sb.append(strAB);
sb.append(",");
sb.append(strAC);
sb.append(",");
sb.append(strBC);
sb.append(",");
sb.append(strABC);
sb.append("&chtt=");
sb.append(strTitle.replaceAll("\\s",""));

new MyAsyncTask().execute(sb.toString());

}

}
});

}

protected void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}

class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
ProgressDialog pDialog = new ProgressDialog(MainActivity.this);

@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Getting Diagram...Please wait...");
pDialog.show();
}

@Override
protected Bitmap doInBackground(String... params) {
Bitmap bm3DPie = MyUtility.loadChart(params[0]);
return bm3DPie;
}

@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);

if(null!=pDialog&&pDialog.isShowing()){
pDialog.dismiss();
}

if (result == null) {
showToast("Problem in loading 3D Pie Chart");
} else {

final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);

Button btn = (Button) dialog.findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
dialog.dismiss();

}
});

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

image.setImageBitmap(result);

dialog.show();
}
}
}
}


2)MyUtility .java

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class MyUtility {
public static Bitmap loadChart(String urlRqs) {
Bitmap bm = null;
InputStream inputStream = null;

try {
inputStream = OpenHttpConnection(urlRqs);
bm = BitmapFactory.decodeStream(inputStream);
if(null!=inputStream)
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return bm;
}

public static InputStream OpenHttpConnection(String strURL)
throws IOException {
InputStream is = null;
URL url = new URL(strURL);
URLConnection urlConnection = url.openConnection();

try {
HttpURLConnection httpConn = (HttpURLConnection) urlConnection;
httpConn.setRequestMethod("GET");
httpConn.connect();

if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = httpConn.getInputStream();
}
} catch (Exception ex) {
}

return is;
}

// Check Internet Connection!!!

public static boolean isNetworkAvailable(Activity act) {
ConnectivityManager connectivity = (ConnectivityManager) act
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
return false;
} else {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}
}


3) main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#d3d3d3"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textmain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="VennDiagram Example"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textmain"
        android:layout_marginTop="30dp"
        android:text="Size A intersects B "
        android:textColor="@android:color/black" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:numeric="integer"
        android:text="6" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="30dp"
        android:text="Size A intersects C "
        android:textColor="@android:color/black" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toRightOf="@+id/textView2"
        android:numeric="integer" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="30dp"
        android:text="Size B intersects C "
        android:textColor="@android:color/black" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:numeric="integer" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="30dp"
        android:text="Size A intersects B intersects C"
        android:textColor="@android:color/black" />

    <EditText
        android:id="@+id/editText4"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_toRightOf="@+id/textView4"
        android:numeric="integer" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="30dp"
        android:text="Title of Diagram"
        android:textColor="@android:color/black" />

    <EditText
        android:id="@+id/editText5"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignBottom="@+id/textView5"
        android:layout_toRightOf="@+id/textView5" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:text=" Show Me VennDiagram " />

</RelativeLayout>

4)dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical" >

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

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:text="Ok" />

</RelativeLayout>

the screen shots


Downlaod the code from DemoGooglechart api use


No comments:

Post a Comment