Sep 28, 2011

Generate 3D Pie Chart using Google Chart Tools

By using Google Chart Tools, we can generate 3D pie chart easily.

Generate 3D Pie Chart using Google Chart Tools

package com.Android3dPieChart;

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.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class Android3dPieChartActivity extends Activity {

final static String urlGoogleChart
= "http://chart.apis.google.com/chart";
final static String urlp3Api
= "?cht=p3&chs=400x150&chl=A|B|C&chd=t:";

EditText inputA, inputB, inputC;
Button generate;
ImageView pieChart;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputA = (EditText)findViewById(R.id.adata);
inputB = (EditText)findViewById(R.id.bdata);
inputC = (EditText)findViewById(R.id.cdata);
generate = (Button)findViewById(R.id.generate);
pieChart = (ImageView)findViewById(R.id.pie);
generate.setOnClickListener(generateOnClickListener);
}

Button.OnClickListener generateOnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String A = inputA.getText().toString();
String B = inputB.getText().toString();
String C = inputC.getText().toString();
String urlRqs3DPie = urlGoogleChart
+ urlp3Api
+ A + "," + B + "," + C;

Bitmap bm3DPie = loadChart(urlRqs3DPie);
if(bm3DPie == null){
Toast.makeText(Android3dPieChartActivity.this,
"Problem in loading 3D Pie Chart",
Toast.LENGTH_LONG).show();
}else{
pieChart.setImageBitmap(bm3DPie);
}
}};

private Bitmap loadChart(String urlRqs){
Bitmap bm = null;
InputStream inputStream = null;

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

return bm;
}

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"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A "
/>
<EditText
android:id="@+id/adata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B "
/>
<EditText
android:id="@+id/bdata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C "
/>
<EditText
android:id="@+id/cdata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<Button
android:id="@+id/generate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate 3D Pie Chart"
/>
<ImageView
android:id="@+id/pie"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


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

Related Post:
- Generate QR Code using Google Chart Tools APIs

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

Sep 7, 2011

Custom adapter on both GridView and Gallery

It's a example using the same custom adapter on both GridView and Gallery. It can be noticed that adapter used on both GridView and Gallery are basically the same.



Custom adapter on both GridView and Gallery



/res/layout/gridlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>




layout file , main.xml
<?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"
/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="50dp"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>




main java code
package com.AndroidGridView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;

public class AndroidGridViewActivity extends Activity {

public class MyAdapter extends BaseAdapter {

final int NumberOfItem = 30;
private Bitmap[] bitmap = new Bitmap[NumberOfItem];

private Context context;
private LayoutInflater layoutInflater;

MyAdapter(Context c){
context = c;
layoutInflater = LayoutInflater.from(context);

//init dummy bitmap,
//using R.drawable.icon for all items
for(int i = 0; i < NumberOfItem; i++){
bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return bitmap.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return bitmap[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View grid;
if(convertView==null){
grid = new View(context);
grid = layoutInflater.inflate(R.layout.gridlayout, null);
}else{
grid = (View)convertView;
}

ImageView imageView = (ImageView)grid.findViewById(R.id.image);
imageView.setImageBitmap(bitmap[position]);

return grid;
}

}

GridView gridView;
Gallery gallery;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.grid);
gallery = (Gallery)findViewById(R.id.gallery);

MyAdapter adapter = new MyAdapter(this);
gridView.setAdapter(adapter);
gallery.setAdapter(adapter);

}

}





Sep 5, 2011

Custom GridView II, with ImageView and TextView

Modify from last post Custom GridView, to implement custom GridView, with bitmap and text in each grid.



GridView with ImageView and TextView




/res/layout/gridlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>





layout file , main.xml
<?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"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>





main java code
package com.AndroidGridView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class AndroidGridViewActivity extends Activity {

public class MyAdapter extends BaseAdapter {

final int NumberOfItem = 30;
private Bitmap[] bitmap = new Bitmap[NumberOfItem];

private Context context;
private LayoutInflater layoutInflater;

MyAdapter(Context c){
context = c;
layoutInflater = LayoutInflater.from(context);

//init dummy bitmap,
//using R.drawable.icon for all items
for(int i = 0; i < NumberOfItem; i++){
bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return bitmap.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return bitmap[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View grid;
if(convertView==null){
grid = new View(context);
grid = layoutInflater.inflate(R.layout.gridlayout, null);
}else{
grid = (View)convertView;
}

ImageView imageView = (ImageView)grid.findViewById(R.id.image);
imageView.setImageBitmap(bitmap[position]);
TextView textView = (TextView)grid.findViewById(R.id.text);
textView.setText(String.valueOf(position));

return grid;
}

}

GridView gridView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.grid);

MyAdapter adapter = new MyAdapter(this);
gridView.setAdapter(adapter);
}

}




Sep 4, 2011

Custom GridView

In order to modify GridView to suit our requirement, we can implement our own adapter extends BaseAdapter.



Custom GridView




Create /res/layout/gridlayout.xml, the layout of individual grid
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>




main.xml, The ayout, it 's same as in the last post Simple GridView example.
<?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"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>




package com.AndroidGridView;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class AndroidGridViewActivity extends Activity {

public class MyAdapter extends BaseAdapter {

final int NumberOfItem = 30;
private Bitmap[] bitmap = new Bitmap[NumberOfItem];

private Context context;
private LayoutInflater layoutInflater;

MyAdapter(Context c){
context = c;
layoutInflater = LayoutInflater.from(context);

//init dummy bitmap,
//using R.drawable.icon for all items
for(int i = 0; i < NumberOfItem; i++){
bitmap[i] = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
}
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return bitmap.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return bitmap[position];
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub

View grid;
if(convertView==null){
grid = new View(context);
grid = layoutInflater.inflate(R.layout.gridlayout, null);
}else{
grid = (View)convertView;
}

ImageView imageView = (ImageView)grid.findViewById(R.id.image);
imageView.setImageBitmap(bitmap[position]);

return grid;
}

}

GridView gridView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.grid);

MyAdapter adapter = new MyAdapter(this);
gridView.setAdapter(adapter);
}

}





next:
- Custom GridView II, with ImageView and TextView



Sep 3, 2011

Simple GridView example

GridView




<?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"
/>
<GridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:columnWidth="100px"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>





package com.AndroidGridView;


import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;

public class AndroidGridViewActivity extends Activity {

GridView gridView;

String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView)findViewById(R.id.grid);

ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listContent);

gridView.setAdapter(adapter);
}

}





Related Post:

- A simple ListView using android.R.layout.simple_list_item_1 layout

- Custom GridView




Sep 2, 2011

ListView with multiple choice

ListView with multiple choice


package com.AndroidListView;


import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidListViewActivity extends Activity {

ListView myList;
Button getChoice;

String[] listContent = {
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView)findViewById(R.id.list);
getChoice = (Button)findViewById(R.id.getchoice);

ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice,
listContent);

myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
myList.setAdapter(adapter);

getChoice.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String selected = "";

int cntChoice = myList.getCount();
SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();
for(int i = 0; i < cntChoice; i++){
if(sparseBooleanArray.get(i)) {
selected += myList.getItemAtPosition(i).toString() + "\n";

}
}

Toast.makeText(AndroidListViewActivity.this,
selected,
Toast.LENGTH_LONG).show();
}});

}
}


<?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"
/>
<Button
android:id="@+id/getchoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Get Choice"
/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>





Infolinks In Text Ads