Thursday, September 29, 2011

Implement slide-in and slide-out animation



package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {
 
 ImageView image1, image2, image3;
 Animation animationSlideInLeft, animationSlideOutRight;
 ImageView curSlidingImage;
 
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       image1 = (ImageView)findViewById(R.id.image1);
       image2 = (ImageView)findViewById(R.id.image2);
       image3 = (ImageView)findViewById(R.id.image3);
      
       animationSlideInLeft = AnimationUtils.loadAnimation(this,
         android.R.anim.slide_in_left);
       animationSlideOutRight = AnimationUtils.loadAnimation(this,
         android.R.anim.slide_out_right);
       animationSlideInLeft.setDuration(1000);
       animationSlideOutRight.setDuration(1000);
       animationSlideInLeft.setAnimationListener(animationSlideInLeftListener);
       animationSlideOutRight.setAnimationListener(animationSlideOutRightListener);

       curSlidingImage = image1;
       image1.startAnimation(animationSlideInLeft);
       image1.setVisibility(View.VISIBLE);

   }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  image1.clearAnimation();
  image2.clearAnimation();
  image3.clearAnimation();
 }
  
 AnimationListener animationSlideInLeftListener
 = new AnimationListener(){

  @Override
  public void onAnimationEnd(Animation animation) {
   // TODO Auto-generated method stub
   
   if(curSlidingImage == image1){
    image1.startAnimation(animationSlideOutRight);
   }else if(curSlidingImage == image2){
    image2.startAnimation(animationSlideOutRight);
   }else if(curSlidingImage == image3){
    image3.startAnimation(animationSlideOutRight);
   } 
  }

  @Override
  public void onAnimationRepeat(Animation animation) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onAnimationStart(Animation animation) {
   // TODO Auto-generated method stub
   
  }};
   
 AnimationListener animationSlideOutRightListener
 = new AnimationListener(){
   @Override
  public void onAnimationEnd(Animation animation) {
   // TODO Auto-generated method stub
    if(curSlidingImage == image1){
     curSlidingImage = image2;
     image2.startAnimation(animationSlideInLeft);
     image1.setVisibility(View.INVISIBLE);
     image2.setVisibility(View.VISIBLE);
     image3.setVisibility(View.INVISIBLE);
    }else if(curSlidingImage == image2){
     curSlidingImage = image3;
     image3.startAnimation(animationSlideInLeft);
     image1.setVisibility(View.INVISIBLE);
     image2.setVisibility(View.INVISIBLE);
     image3.setVisibility(View.VISIBLE);
    }else if(curSlidingImage == image3){
     curSlidingImage = image1;
     image1.startAnimation(animationSlideInLeft);
     image1.setVisibility(View.VISIBLE);
     image2.setVisibility(View.INVISIBLE);
     image3.setVisibility(View.INVISIBLE);
    }
  }

  @Override
  public void onAnimationRepeat(Animation animation) {
   // TODO Auto-generated method stub
   
  }
  @Override
  public void onAnimationStart(Animation animation) {
   // TODO Auto-generated method stub
    
  }};
}


<?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/image1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:src="@drawable/icon"
   android:visibility="invisible"
   />
<ImageView
   android:id="@+id/image2"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:src="@drawable/icon"
   android:visibility="invisible"
   />
<ImageView
   android:id="@+id/image3"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:src="@drawable/icon"
   android:visibility="invisible"
   />
</LinearLayout>




Download the files.



Ref:
- Implement simple Fade-in Animation
- Handle Animation event, AnimationListener
- Android pre-defined Animation Resources
- Implement slide top-down vertical animation
Animation of sliding horizontal and vertical

Wednesday, September 28, 2011

Android pre-defined Animation Resources

In the last exercise "Implement simple Fade-in Animation" and "Handle Animation event, AnimationListener", we create our own animation (fadein.xml and fadeout.xml). Actually, Android come with some pre-defined Animation Resources include android.R.anim.fade_in and android.R.anim.fade_in. Such that we can omit the creation of /res/anim/fadein.xml and /res/anim/fadeout.xml.

This exercise have the almost same effect as in the last exercise "Handle Animation event, AnimationListener".

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"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>


package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {

ImageView image;
Animation animationFadeIn, animationFadeOut;

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

animationFadeIn = AnimationUtils.loadAnimation(this,
android.R.anim.fade_in);
animationFadeOut = AnimationUtils.loadAnimation(this,
android.R.anim.fade_out);
animationFadeIn.setAnimationListener(animationInListener);
animationFadeOut.setAnimationListener(animationOutListener);
image.startAnimation(animationFadeIn);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
image.clearAnimation();
}

AnimationListener animationInListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeOut);
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}};

AnimationListener animationOutListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeIn);
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}};
}


Download the files.

next:
- Implement slide-in and slide-out animation



Tuesday, September 27, 2011

FREE eBook: Windows Phone 7 Guide for Symbian Qt Application Developers

FREE eBook: Windows Phone 7 Guide for Symbian Qt Application Developers
If you have been developing Qt applications for Symbian^3, Symbian Anna, or Symbian Belle and are interested in building your applications for Windows Phone, this guide is for you.

The guide covers what you need to know to add Windows Phone development to your skill set, while leveraging what you have already learned while developing Symbian Qt applications.
  • Chapter 1: Introducing Windows Phone Platform to Symbian^3 Qt Application Developers
  • Chapter 2: Windows Phone Application Design Guidelines
  • Chapter 3: Windows Phone Developer and Designer Tools
  • Chapter 4: C# programming
  • Chapter 5: Introducing Windows Phone Application Life Cycle
  • Chapter 6: Porting Applications to Windows Phone
  • Chapter 7: Windows Phone Example Applications
  • Chapter 8: Using the API Mapping Tool


Details: Windows Phone 7 Guide for Symbian Qt Application Developers

Handle Animation event, AnimationListener.



Create animation XML files.
/res/anim/fadein.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000">
</alpha>
</set>


/res/anim/fadeout.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="1000">
</alpha>
</set>


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"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
</LinearLayout>


package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {

ImageView image;
Animation animationFadeIn, animationFadeOut;

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

animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fadeout);
animationFadeIn.setAnimationListener(animationInListener);
animationFadeOut.setAnimationListener(animationOutListener);
image.startAnimation(animationFadeIn);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
image.clearAnimation();
}

AnimationListener animationInListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeOut);
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}};

AnimationListener animationOutListener
= new AnimationListener(){

@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
image.startAnimation(animationFadeIn);
}

@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}};
}


Download the files.


next:
- Android pre-defined Animation Resources

Monday, September 26, 2011

Implement simple Fade-in Animation



Create /res/anim/fadein.xml to define the fade-in animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="5000">
</alpha>
</set>


main.xml 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"
/>
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
android:src="@drawable/icon"
/>
</LinearLayout>


package com.exercise.AndroidAnimation;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class AndroidAnimationActivity extends Activity {

ImageView image;

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

Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
image.startAnimation(animationFadeIn);
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
image.clearAnimation();
}
}


Download the files.



next:
- Handle Animation event, AnimationListener.
- Android pre-defined Animation Resources

Saturday, September 24, 2011

Nexus One updated Gingerbread 2.3.6 (CAUTION: Don't update!)

Nexus One run Gingerbread 2.3.6
If you still have a Nexus One and can't wait OTA update, it's easy to Google "Nexus One 2.3.6" manual update :)




CAUTION!


After Updated 2.3.6 on my Nexus One, The Portable Wi-Fi hotspot cannot work!
- Everytime the Wi-Fi hotspot enabled, the data(3G) disconnected :(


Thursday, September 22, 2011

Multi Choice ListView

Multi Choice ListView



package com.exercise.AndroidMultiChoiceList;

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

public class AndroidMultiChoiceListActivity extends Activity {
 
 ListView choiceList;
 String[] choice = { "Choice A", 
   "Choice B", "Choice C", "Choice D", "Choice E"};
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        choiceList = (ListView)findViewById(R.id.list);
        
        ArrayAdapter<String> adapter
        = new ArrayAdapter<String>(this, 
          android.R.layout.simple_list_item_multiple_choice, 
          choice);
        choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        choiceList.setAdapter(adapter);
 
    }
}


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


Related:
- Implement multi select ListView with custom layout, using custom ArrayAdapter.

Related ListFragment:
ListFragment with multiple choice

Monday, September 19, 2011

Monitor the Proximity Sensor

Proximity sensor measures the distance that some object is from the device. It is often used to detect the presence of a person’s face next to the device.

Proximity Sensor

Some proximity sensors only support a binary near or far measurement. In this case, the sensor should report its maximum range value in the far state and a lesser value in the near state.

package com.exercise.AndroidProximitySensor;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidProximitySensorActivity extends Activity {
/** Called when the activity is first created. */

TextView ProximitySensor, ProximityMax, ProximityReading;

SensorManager mySensorManager;
Sensor myProximitySensor;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ProximitySensor = (TextView)findViewById(R.id.proximitySensor);
ProximityMax = (TextView)findViewById(R.id.proximityMax);
ProximityReading = (TextView)findViewById(R.id.proximityReading);

mySensorManager = (SensorManager)getSystemService(
Context.SENSOR_SERVICE);
myProximitySensor = mySensorManager.getDefaultSensor(
Sensor.TYPE_PROXIMITY);

if (myProximitySensor == null){
ProximitySensor.setText("No Proximity Sensor!");
}else{
ProximitySensor.setText(myProximitySensor.getName());
ProximityMax.setText("Maximum Range: "
+ String.valueOf(myProximitySensor.getMaximumRange()));
mySensorManager.registerListener(proximitySensorEventListener,
myProximitySensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
}

SensorEventListener proximitySensorEventListener
= new SensorEventListener(){

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub

}

@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub

if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
ProximityReading.setText("Proximity Sensor Reading:"
+ String.valueOf(event.values[0]));
}
}
};
}


<?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/proximitySensor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/proximityMax"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/proximityReading"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Download the files.

Friday, September 16, 2011

Query and Parse Google+ JSON

There is a post in The official Google Code blog, discuss about Getting started on the Google+ API. It's a exercise to query and parse Google+ JSON using Google+ API.



Query and Parse Google+ JSON


To use Google+ API, you have to Apply your own Google+ API key.



package com.exercise.AndroidGooglePlus;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidGooglePlusActivity extends Activity {

TextView item_kind;
TextView item_id;
TextView item_displayName;
TextView item_tagline;
TextView item_gender;
TextView item_aboutMe;
TextView item_url;

final static String GooglePlus_url
= "https://www.googleapis.com/plus/v1/people/108189587050871927619?key=<your own Google+ API key>";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
item_kind = (TextView)findViewById(R.id.item_kind);
item_id = (TextView)findViewById(R.id.item_id);
item_displayName = (TextView)findViewById(R.id.item_displayName);
item_tagline = (TextView)findViewById(R.id.item_tagline);
item_gender = (TextView)findViewById(R.id.item_gender);
item_aboutMe = (TextView)findViewById(R.id.item_aboutMe);
item_url = (TextView)findViewById(R.id.item_url);

String googlePlusResult = QueryGooglePlus();
ParseGooglePlusJSON(googlePlusResult);

}

String QueryGooglePlus(){
String qResult = null;

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(GooglePlus_url);

try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();

if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();

String stringReadLine = null;

while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}

qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(AndroidGooglePlusActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(AndroidGooglePlusActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}

return qResult;
}

void ParseGooglePlusJSON(String json){
if(json != null){
try {
JSONObject JsonObject = new JSONObject(json);
item_kind.setText("kind: " + JsonObject.getString("kind"));
item_id.setText("id: " + JsonObject.getString("id"));
item_displayName.setText("displayName: " + JsonObject.getString("displayName"));
item_tagline.setText("tagline: " + JsonObject.getString("tagline"));
item_gender.setText("gender: " + JsonObject.getString("gender"));
item_aboutMe.setText("aboutMe: " + Html.fromHtml(JsonObject.getString("aboutMe")));
item_url.setText("url: " + JsonObject.getString("url"));

Toast.makeText(AndroidGooglePlusActivity.this,
"finished",
Toast.LENGTH_LONG).show();

} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(AndroidGooglePlusActivity.this,
e.toString(),
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"
/>
<TextView
android:id="@+id/item_kind"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_displayName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_tagline"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_gender"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_aboutMe"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/item_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


note: have to modify AndroidManifest.xml to grant permission of "android.permission.INTERNET".

Download the files.

Related article:
- Example code to search Google Plus public posts



Apply your own Google+ API key

The official Google Code blog have a new post about Getting started on the Google+ API. Google+ gives users full control over their information, supporting everything from intimate conversations with family to public showcases and debates. This initial API release is focused on public data only — it lets you read information that people have shared publicly on Google+.



In order to read information from Google+, you have to apply your API Key. Visit APIs Console, you have to login your Google account to create your API key.




  • Click on the API project on the left and select Create...
    Apply your own Google+ API key

  • Enter the name of your project and click Create project.
    Apply your own Google+ API key

  • Click to enable Google+ API, you will be asked to review and accept the terms of service.
    Apply your own Google+ API key

  • Click API Access, your API Key will be found.
    Apply your own Google+ API key



Now you can copy the link with your own API key at browser to read the JSON returned from Google+.

https://www.googleapis.com/plus/v1/people/108189587050871927619?key=<your API key>
Google+ API key




next:

- Query and Parse Google+ JSON



Thursday, September 15, 2011

MicroChip's PIC24F Accessory Development Starter Kit for Android


The Microchip PIC24F Accessory Development Start Kit for Android™ is a standalone board used for evaluating and developing electronic accessories for Google’s Android operating system for smartphones and tablets.






link: PIC24F Accessory Development Starter Kit for Android









Intel Developer Forum 2011 - Keynote Paul Otellini



remark: Andy Rubin join the keynote to announce Android for Intel Architecture, at ~40:21.


Wednesday, September 14, 2011

Windows Developer Preview and free developer tools are available now!

Get the Windows Developer Preview and free developer tools to start building apps now.



The following versions are available for FREE to download:
  • Windows Developer Preview with developer tools English, 64-bit (x64)
  • Windows Developer Preview English, 64-bit (x64)
  • Windows Developer Preview English, 32-bit (x86)



Link:

- http://msdn.microsoft.com/en-us/windows/apps/br229516



In the Windows Developer Preview with developer tools English, 64-bit (x64), the following are included:
  • 64-bit Windows Developer Preview
  • Windows SDK for Metro style apps
  • Microsoft Visual Studio 11 Express for Windows Developer Preview

  • Microsoft Expression Blend 5 Developer Preview
  • 28 Metro style apps including the BUILD Conference app




Tuesday, September 13, 2011

Scroll View - scrollBy() and scrollTo()

The method scrollBy() and scrollTo() can be called t scroll a view, by the amount of pixels to scroll and specify the position to scroll.

Scroll View - scrollBy() and scrollTo()


package com.exercise.AndroidScrollBy;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;

public class AndroidScrollByActivity extends Activity {

Button buttonScrollUp, buttonScrollDown, buttonScrollToTop;
ScrollView myView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonScrollUp = (Button)findViewById(R.id.scrollup);
buttonScrollDown = (Button)findViewById(R.id.scrolldown);
buttonScrollToTop = (Button)findViewById(R.id.scrolltotop);
myView = (ScrollView)findViewById(R.id.myview);

buttonScrollUp.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.scrollBy(0, +20);
}});

buttonScrollDown.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.scrollBy(0, -20);
}});

buttonScrollToTop.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.scrollTo(0, 0);
}});
}
}




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"
/>
<Button
android:id="@+id/scrollup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scroll Up"
/>
<Button
android:id="@+id/scrolldown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scroll Down"
/>
<Button
android:id="@+id/scrolltotop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Scroll To Top"
/>
<ScrollView
android:id="@+id/myview"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#A0A0A0"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/icon"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40dp"
android:text="This\nView\nCan\nBe\nScroled\nBy Buttons!"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Dummy Button"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>



Monday, September 12, 2011

Intel® Atom™ Processor E6xx

Intel® Atom™ Processor E6xx Port of Choice Animation Video

With the breadth of software including abundance of OSes, optimized drivers, boot loaders and other tools, the Intel Atom processor E6xx is truly the Port of Choice.

Load HTML coded data (returned from HttpPost) into WebView

In last article "Example of HttpPost on Android", the response returned from HttpPost is HTML coded String, actually it's a webpage.

Load HTML coded data into WebView



It can be loaded into WebView using WebView.loadData(String data, String mimeType, String encoding).

- data: A String of data in the given encoding. The date must be URI-escaped -- '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.

- mimeType: The MIMEType of the data. i.e. text/html, image/jpeg

- encoding: The encoding of the data. i.e. utf-8, base64



package com.exercise.AndroidHttpPost;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;

public class AndroidHttpPostActivity extends Activity {

WebView webView;

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

BufferedReader bufferedReader = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://search.yahoo.com/search");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("p", "Android"));


try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);

HttpResponse response= httpClient.execute(request);

bufferedReader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
}
bufferedReader.close();

Toast.makeText(AndroidHttpPostActivity.this,
"Finished",
Toast.LENGTH_LONG).show();

String webData = stringBuffer.toString();

webData = webData.replace("#", "%23");
webData = webData.replace("%", "%25");
webData = webData.replace("\\", "%27");
webData = webData.replace("?", "%3f");

webView.loadData(webData,
"text/html",
"UTF-8");

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidHttpPostActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidHttpPostActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}finally{
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}




<?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"
/>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>




note: have to modify AndroidManifest.xml to grant permission of "android.permission.INTERNET".



Download the files.


Example of HttpPost on Android

It's a simple example using HttpPost request to "http://search.yahoo.com/search", with name/value of "p" and "Android".



Example of HttpPost


package com.exercise.AndroidHttpPost;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidHttpPostActivity extends Activity {

TextView result;

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

BufferedReader bufferedReader = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost("http://search.yahoo.com/search");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("p", "Android"));


try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(postParameters);
request.setEntity(entity);

HttpResponse response= httpClient.execute(request);

bufferedReader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
}
bufferedReader.close();

result.setText(stringBuffer.toString());

Toast.makeText(AndroidHttpPostActivity.this,
"Finished",
Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidHttpPostActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(AndroidHttpPostActivity.this,
e.toString(),
Toast.LENGTH_LONG).show();
}finally{
if (bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}
}




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"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</ScrollView>
</LinearLayout>




note: have to modify AndroidManifest.xml to grant permission of "android.permission.INTERNET"



Download the files.



next: Load HTML coded data (returned from HttpPost) into WebView


Sunday, September 11, 2011

onSaveInstanceState() and onRestoreInstanceState()

As mentioned in the article "Activity will be re-started when screen orientation changed", the activity will be destroyed and re-created once screen orientation changed.



We can call onSaveInstanceState() to retrieve per-instance state from an activity before being killed so that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both).



example:

onSaveInstanceState() and onRestoreInstanceState()


package com.exercise.AndroidSavedInstanceState;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidSavedInstanceStateActivity extends Activity {

EditText edit;
Button update;
TextView text;

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

edit = (EditText)findViewById(R.id.edit);
update = (Button)findViewById(R.id.update);
text = (TextView)findViewById(R.id.text);

update.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText(edit.getText().toString());
}});
}

@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("TEXT", (String)text.getText());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
text.setText(savedInstanceState.getString("TEXT"));
}

}




<?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"
/>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Update"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Without onSaveInstanceState() and onRestoreInstanceState(), TextView text will be cleared everytime the screen orientation changed.

Notice that if the activity finished completely, the state will not be saved.

Next:
- More on onSaveInstanceState() and onRestoreInstanceState()



Friday, September 9, 2011

Overview of Web Apps on Android


There are essentially two ways to deliver an application on Android: as a client-side application (developed using the Android SDK and installed on user devices as an .apk) or as a web application (developed using web standards and accessed through a web browser—there's nothing to install on user devices).

Google have a document providing the overview of Web Apps on Android.

Thursday, September 8, 2011

Use Handler and Runnable to generate a periodic event

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.



There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.



It's a example using Handler and Runnable to generate a periodic event in 500ms.



Use Handler and Runnable to generate a periodic event



package com.exercise.AndroidHandlerRunnable;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class AndroidHandlerRunnableActivity extends Activity {

private int cnt = 0;
Button Start, Stop;
TextView counter;
private Handler handler = new Handler();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
counter = (TextView)findViewById(R.id.counter);
Start = (Button)findViewById(R.id.Start);
Start.setEnabled(true);
Stop = (Button)findViewById(R.id.Stop);
Stop.setEnabled(false);

Start.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.post(timedTask);
Start.setEnabled(false);
Stop.setEnabled(true);
}});

Stop.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.removeCallbacks(timedTask);
Start.setEnabled(true);
Stop.setEnabled(false);
}});
}

private Runnable timedTask = new Runnable(){

@Override
public void run() {
// TODO Auto-generated method stub
cnt++;
counter.setText(String.valueOf(cnt));
handler.postDelayed(timedTask, 500);
}};
}





<?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/Start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start"
/>
<Button
android:id="@+id/Stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
/>
<TextView
android:id="@+id/counter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>




Tuesday, September 6, 2011

Theme

Theme is some style elements can be applied across an entire activity, or application.



Android OS provide a number of pre-build Theme for you in Android SDK; for example: you can open the file below, you can find a number of pre-build theme start with "Theme.".



/android-sdk-linux_x86/platforms/android-8/data/res/values/themes.xml



such as:
  • Theme.NoTitleBar

  • Theme.NoTitleBar.Fullscreen

  • Theme.Light

  • Theme.Light.NoTitleBar

  • Theme.Light.NoTitleBar.Fullscreen

  • ...



To apply Theme on activity (or on whole application), you can add attribute of "android:theme=..." in AndroidManifest.xml, under <activity> (or <application>).



example of android:style/Theme.Panel:

android:style/Theme.Panel

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidTheme"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:theme="@android:style/Theme.Panel">
<activity android:name=".AndroidThemeActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
</manifest>




Sunday, September 4, 2011

Set styling text on TextView

styling text



Define your string resource (stylingtext) in /res/values/strings.xml, with styling markup.

<?xml version="1.0" encoding="utf-8"?>

<resources>
<string name="hello">Hello World, AndroidStylingTextActivity!</string>
<string name="app_name">AndroidStylingText</string>
<string name="stylingtext">
Styling Text:\n
<i>Italic</i> <b>Bold</b> <monospace>Monospace</monospace>\n
<strike>Strike</strike> <sup>Superscript</sup> <sub>Subscript</sub> <u>Underline</u>\n
<big><big><big>Big x 3 </big>Big x 2 </big>Big</big> <small>Small <small>Small x 2</small></small>
</string>
</resources>






Then you can refer the string resource in your 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"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stylingtext"
/>
</LinearLayout>





Saturday, September 3, 2011

Apply style on Text

Apply style on Text

To create our own style, create a XML file /res/values/style.xml.

<?xml version="1.0" encoding="utf-8"?>

<resources>
<style name="mystyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#A0A0A0</item>
<item name="android:typeface">monospace</item>
</style>
<style name="mystyle.20dp">
<item name="android:textSize">20dp</item>
</style>
<style name="mystyle.30dp">
<item name="android:textSize">30dp</item>
</style>
<style name="mystyle.20dp.bold">
<item name="android:textStyle">bold</item>
</style>
</resources>




Then, we can refer it in our layout, main.xml. Android also pre-defined some style for using.

<?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
style="@style/mystyle"
android:text="mystyle"
/>
<TextView
style="@style/mystyle.20dp"
android:text="mystyle.20dp"
/>
<TextView
style="@style/mystyle.20dp.bold"
android:text="mystyle.20dp.bold"
/>
<TextView
style="@style/mystyle.30dp"
android:text="mystyle.30dp"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance"
android:text="Android build-in style: TextAppearance"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/background_light"
style="@android:style/TextAppearance.Inverse"
android:text="Android build-in style: TextAppearance.Inverse"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Small"
android:text="Android build-in style: TextAppearance.Small"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Medium"
android:text="Android build-in style: TextAppearance.Medium"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@android:style/TextAppearance.Large"
android:text="Android build-in style: TextAppearance.Large"
/>
</LinearLayout>




Bubble Sort

BubbleSort


package com.exercise.AndroidBubbleSort;


import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidBubbleSortActivity extends Activity {

TextView src, result;
int[] data = new int[10];

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

Random random = new Random();
String stringSrc = "Src = ";
for (int i = 0; i < data.length; i++){
data[i] = random.nextInt(100);
stringSrc += data[i] + " ";
}

src.setText(stringSrc);

bubblesort();

String stringResult = "Result = ";
for (int i = 0; i < data.length; i++){
stringResult += data[i] + " ";
}

result.setText(stringResult);

}

void bubblesort() {
int min;

for (int i = 0; i < data.length - 1; i++){
min = i;
for (int j = i + 1; j < data.length; j++){
if (data[j] < data[min]){
min = j;
}
}
if (i != min){
int tmp = data[i];
data[i] = data[min];
data[min] = tmp;
}
}
}

}






Friday, September 2, 2011

android:onClick - define callback method when a View clicked, in XML layout.

A callback method can be defined in XML layout file, using "android:onClick". It define the name of the method in this View's context to invoke when the view is clicked. This name must correspond to a public method that takes exactly one parameter of type View.




android:onClick - define callback method when a View clicked, in XML 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"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:onClick="onClick"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 2"
android:onClick="onClick"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button 3"
android:onClick="onClick"
/>
</LinearLayout>





package com.exercise.AndroidOnClick;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class AndroidOnClickActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void onClick(View view){
Toast.makeText(AndroidOnClickActivity.this,
"onClick:\n" + view.toString(),
Toast.LENGTH_LONG).show();
}
}