Sep 27, 2011

Generate QR Code using Google Chart Tools APIs

Google Chart Tools provide a perfect way to visualize data on your website. From simple line charts to complex hierarchical tree maps, the chart galley provides a large number of well-designed chart types. Populating your data is easy using the provided client- and server-side tools.

It's a example of Android app to generate QR Code using Google Chart Tools APIs.

Generate QR Code using Google Chart Tools APIs

package com.AndroidQRCode;

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.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidQRCodeActivity extends Activity {

final static String urlGoogleChart = "http://chart.apis.google.com/chart";
final static String urlQRApi = "?chs=400x400&cht=qr&chl=";
final static String urlMySite = "http://android-coding.blogspot.com/";

ImageView QRCode;
TextView MySite;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
QRCode = (ImageView)findViewById(R.id.qrimage);
MySite = (TextView)findViewById(R.id.mysite);
MySite.setText(urlMySite);

Bitmap bm = loadQRCode();
if(bm == null){
Toast.makeText(AndroidQRCodeActivity.this,
"Problem in loading QR Code1",
Toast.LENGTH_LONG).show();
}else{
QRCode.setImageBitmap(bm);
}

}

private Bitmap loadQRCode(){
Bitmap bmQR = null;
InputStream inputStream = null;

try {
inputStream = OpenHttpConnection(urlGoogleChart + urlQRApi + urlMySite);
bmQR = BitmapFactory.decodeStream(inputStream);
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bmQR;
}

private 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;
}

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:id="@+id/qrimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mysite"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Permission: "android.permission.INTERNET" is needed.

Related Post:
- Generate 3D Pie Chart using Google Chart Tools

Sep 19, 2011

Detection of Gesture

Detection of Gesture


To detect Gesture, you have to prepare your own gestures library. Refer to last post to create your gestures library using Gestures Builder, and copy to /res/raw.

package com.GestureMonitor;

import java.util.ArrayList;

import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.TextView;

public class GestureMonitorActivity extends Activity {

GestureLibrary gestureLibrary = null;
GestureOverlayView gestureOverlayView;
TextView gestureResult;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gestureResult = (TextView)findViewById(R.id.gestureresult);
gestureOverlayView = (GestureOverlayView)findViewById(R.id.gestures);

gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
gestureLibrary.load();

gestureOverlayView.addOnGesturePerformedListener(gesturePerformedListener);
}

OnGesturePerformedListener gesturePerformedListener
= new OnGesturePerformedListener(){

@Override
public void onGesturePerformed(GestureOverlayView view, Gesture gesture) {
// TODO Auto-generated method stub
ArrayList<Prediction> prediction = gestureLibrary.recognize(gesture);
if(prediction.size() > 0){
gestureResult.setText(prediction.get(0).name);
}

}};
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/gestureresult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"/>
</LinearLayout>

Sep 18, 2011

Gestures Builder: create your gestures library

Gesture is a prerecorded touch screen motion sequency. If user performs gesture as the prerecorded gesture, your app can recogized as a command.



Gestures API (android.gesture) was introduced in Android 1.6, lets you store, load, draw and recognize gestures. In order to use Gestures API, you need a prerecorded gestures library. The Android 1.6 SDK (or later) comes with a new application pre-installed on the emulator, called Gestures Builder. You can use this application to create a set of pre-defined gestures for your own application.



You can also find the source code of Gestures Builder in SDK samples, such that you can create it on true device to create your own gestures library.



Create GesturesBuilder

  • In Eclipse, click File -> New - Project... to create a New Android Project.
  • Select "Create project from existing sample", select Build Target higher than 1.6, select GesturesBuilder in Samples, and click Finish.
  • Build and Run the App




GesturesBuilder



After you Add gesture and DONE, a Toast will show you where is the gestures file saved, /mnt/sdcard/gestures (root of sd card) in my case. You can copy the gestures file to /res/raw folder of you gesture-enabled app as your gestures library.

next Post:
- Detection of Gesture

Sep 14, 2011

ScaleGestureDetector

android.view.ScaleGestureDetector (Since API Level 8, Android 2.2) detects transformation gestures involving more than one pointer ("multitouch") using the supplied MotionEvents. The ScaleGestureDetector.OnScaleGestureListener callback will notify users when a particular gesture event has occurred. This class should only be used with MotionEvents reported via touch.



ScaleGestureDetector
Normally, it can be used to detect pinch zoom.



To use this class:
  • Create an instance of the ScaleGestureDetector for your View
  • In the onTouchEvent(MotionEvent) method ensure you call onTouchEvent(MotionEvent). The methods defined in your callback will be executed when the events occur.


example:
package com.AndroidScaleGestureDetector;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.widget.TextView;

public class AndroidScaleGestureDetectorActivity extends Activity {

TextView textGestureAction;
private ScaleGestureDetector scaleGestureDetector;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textGestureAction = (TextView)findViewById(R.id.GestureAction);
scaleGestureDetector = new ScaleGestureDetector(this,
new MySimpleOnScaleGestureListener());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
scaleGestureDetector.onTouchEvent(event);
return true;
}

public class MySimpleOnScaleGestureListener extends
SimpleOnScaleGestureListener {

@Override
public boolean onScale(ScaleGestureDetector detector) {
// TODO Auto-generated method stub

float scaleFactor = detector.getScaleFactor();
if(scaleFactor > 1){
textGestureAction.setText("Scale Out: " + String.valueOf(scaleFactor));
}else{
textGestureAction.setText("Scale In: " + String.valueOf(scaleFactor));
}

return true;
}
}
}





<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:id="@+id/GestureAction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

Sep 13, 2011

GestureOverlayView

android.gesture.GestureOverlayView is transparent overlay for gesture input that can be placed on top of other widgets or contain other widgets.

GestureOverlayView

Modify main.xml to add GestureOverlayView on layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gestureStrokeType="multiple"
android:eventsInterceptionEnabled="true"/>
</LinearLayout>


Related:
- Detection of Gesture

Infolinks In Text Ads