Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Wednesday, December 16, 2009

Getting Current Location on Android SDK, requestLocationUpdates

Sorry that I've been MIA for past few month, didn't do any development and I was more focused on life. I am back to android development now and obviously I ran into few problems.

I've spent entire day trying to figure out how to get current location and yet all these forum posts were only giving half of the answer, so i decided to blog about it.

Step 1:
The permission is required on AndroidManifest.xml
please make sure

is there.

Step 2:
Listener is required due to the fact the last known location seems to be null all the time on the emulator. And somehow you need an empty class to accommodate that, I don't know why but it's just the way it is, and you can't declare new LocationListener() for some reason.

public class CurrentLocationListener implements LocationListener{

public void onLocationChanged(Location argLocation) {

}
public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

public void onStatusChanged(String provider, int status, Bundle arg2) {
}
}


Step 3:
Declare Location Manager and Listener in order to get current latitude and longitude
in whichever class you decide to write them in.


LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

LocationListener myLocationListener = new CurrentLocationListener();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, myLocationListener);

Location currentLocation = locationManager.getLastKnownLocation("gps");

String Latitude = String.valueOf(currentLocation.getLatitude());
String Longitude = String.valueOf(currentLocation.getLongitude());


This worked for me up to this date, if it has any problems feel free to make comment on it.

I'll post about how to get data from wcf next time.

Wednesday, May 13, 2009

Android SDK's Guide, Image from Gallary to be Clickable

Android's Online Resource is very limited, it took me a day before I can figure out how to make those pictures in the photo gallery clickable. I am still a newbie at android development, but I have never spend this much time trying to figure out something that's this simple.

On the android's website it has Hello Gridview guide, which shows you the photo gallary. If you want further clicks from those photos following code is to be added.

All the XML has already set up in the tutorial.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.photogridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
public void onItemClick(AdapterView parent, View v, int position, long id){
GoDoWhatever();
}}});
}

protected void GoDoWhatever()
{
//Do Whatever you want to do after pictures in the gallery are being clicked
}

Hopefully this helps

Wednesday, May 6, 2009

Android's Button Onclick Event:Button.OnClickListener()

I came from Asp.net background, and I really didn't have any idea on how to develop mobile phone applications. I didn't see any documentation on how get an event for OnClick on Android SDK, this most simple feature is can not be automatically generated by Eclipse, you have do it on your own.

in the \res\layout\main.xml Create the button

<button
id="@+id/TestButton"
layout_width="fill_parent"
layout_height="wrap_content"
layout_marginleft="30dip"
layout_marginright="30dip"
text="Test Button">

And you can reference it like this from code behind

---------------------------------------------------------
package com.example.helloandroid;

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

public class HelloAndroid extends Activity {
private Button btnFromGallery;

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

protected void initControls()
{
btnFromGallery = (Button)findViewById(R.id.TestButton);

btnFromGallery.setOnClickListener(new Button.OnClickListener() { public void onClick (View v){ DoWork(); }});
}

protected void DoWork()
{
//Do whatever you need to do here
}
}
-------------------------------------------------------------------

This may look simple but it was very hard to find it online, I hope google would create more examples in the future.

Conversion to Dalvik format failed with error 1

I started google android development today, after downloading SDK and loading it into eclipse just like how google guide “hello android” has listed.

“no classfiles specified“
“Conversion to Dalvik format failed with error 1“
pops up as I try to run it for the very first time.

How you fix it is
Go to Project -> Clean and it should solve the problem.

Good luck on your “Hello Android Application”