Showing posts with label android. Show all posts
Showing posts with label android. Show all posts

Thursday, January 13, 2011

Consuming REST Web Service using Android

In this tutorial, we will find out how to create an Android application to consume REST Web Service.

We will create android application which will consume web service in this post .
Our application should look like this.

Create a new Android Application named "Note Android".
Modify AndroidManifest and add INTERNET permission.

Next, create the view for posting new note : main.xml



And the view for viewing all of our notes. There is no component here. Only a layout because we will create the component programmatically



Next, create our view for posting note : DatabaseAndroid.java



And the code for parsing xml retrieved from web service and present each element on a TextView : ViewDatabase.java



Run our application and try creating new notes.

Have fun!

Wednesday, January 12, 2011

Create List without extending ListActivity

Creating list view in Android is an easy task.
All we have to do is extending ListActivity, implement the methods and we are good to go.
But there are times when it is not an option, because we need to extend another class.
Let say....MapActivity? So we need to create a two tabbed view for user to be able to change from map view to list view. Our intended application may look like this.

When user click on an entry in list view, user will be redirected to the entry location in map view.
In this tutorial, we will create only a ListView then, we will add Tabs and MapView Tab.
We will need to create our implementation of ArrayAdapter and override its getView() method to accomodate our own view. We will create a title, a description and an icon for each row.

Dont forget to add uses library and internet permission in AndroidManifest.xml

First, we need to create a model class. Let say Restaurant.java



After that, we will create our view: main.xml



Next, create our layout for each row in our list view: row.xml



Next step, our main application view: DoubleTabView.java



Execute the application and we will see our generic list view application



Next, we will create a tab bar for user to choose List View or Map View and we will add the feature to navigate user to the map view whenever he/she click on a row in the List View

First, we should change our main.xml. We add TabHost, TabWidget and MapView



Next, we add the tab name programatically, then we add navigateToLocation method to navigate and zoom to the restaurant location



Build and run.
Our final application should look like this
Click one restaurant, and the application will navigate and zoom to the location

That's it! Have fun!





Create a Radius Around a Point in Google Map

Question : How do I create a 9000 meters buffer radius from a point and display placemarks that intersect that buffer.
Answer : Google Map API for android has a class that reads and draw your current location in the map.
That class is "MyLocationOverlay". The class has a "draw" method that will be called everytime user pan or zoom on the map.
So to draw a circle radius of your current location, we need to extends this class and override its draw method.

Then, we create our view, one map view, one textbox and one button


After that, we need to create our main class. TukangAndroidApp.java

Dont forget to edit AndroidManifest.xml to add permissions and google map library


Run your application.
Tips : Set the GPS location from the DDMS first before you press Search, otherwise you will encounter NullPointerException
If all goes well, your application will be like this

Notice in TukangAndroidApp.java, our displayResult method didnt do anything, so we will implement the method to return some placemarks that intersect our buffer.
First, we should extends ItemizedOverlay class to create our placemark


Next, we will implement the displayResult method.
Our last TukangAndroidApp.java should look like this

Run the application, and you can see that our application will work well.







Have fun!