Wednesday, August 7, 2013

Android Google map marker onclick listener

theMap.setOnMarkerClickListener(new OnMarkerClickListener()
        {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                //if(arg0.getTitle().equals("MyHome")) // if marker source is clicked
                     Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
                return true;
            }

        }); 

Wednesday, July 31, 2013

location to address in android

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

Wednesday, July 24, 2013

Android: save camera image to file-folder

In menifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

In activity class
 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.triage_form);
     
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            //File file = new File(Environment.getExternalStorageDirectory() + "/photousman.jpg");
                //Uri outputFileUri = Uri.fromFile(file);
           
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
               // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }



protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String filename = myfilename.png;
    File sd = new File(Environment.getExternalStorageDirectory()+ "/triage");
    boolean success = true;
    if (!sd.exists()) {
       success = sd.mkdir();
    }
    File dest = new File(sd, filename);
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
           
            try {
                FileOutputStream out = new FileOutputStream(dest);
                photo.compress(Bitmap.CompressFormat.PNG, 90, out);
                out.flush();
                out.close();
           } catch (Exception e) {
                e.printStackTrace();
           }
           
        }
    } 

Android- Add camera to activity

Taken from the link

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }
        });
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}


Note that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it.
Here is the layout that the above activity uses. It is simply a LinearLayout containing a Button with id button1 and an ImageView with id imageview1:
<?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"
    >
    <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/photo"></Button>
    <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_width="wrap_content"></ImageView>

</LinearLayout>

And one final detail, be sure to add:
<uses-feature android:name="android.hardware.camera"></uses-feature> 
and if camera is optional to your app functionality. make sure to set require to false in the permission. like this
<uses-feature android:name="android.hardware.camera" android:required="false"></uses-feature>
to your manifest.xml.

Copy and rename Android Project in Eclipse


  1. you just copy your project into new one (ctrl+c & ctrl+v).
  2. you change package-name in your manifest
  3. After that you must rename your main package with refactor. Don't forget to select.
  4. Just change your application name in manifest
    application android:icon="@drawable/icon" android:label="new name"

Google Map, add marker on click

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub
            lstLatLngs.add(point);
            map.addMarker(new MarkerOptions().position(point));
        }
    });

Thursday, May 16, 2013

Bypass Android strict mode policy on networks


Solution
if (android.os.Build.VERSION.SDK_INT > 9) {
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
     StrictMode.setThreadPolicy(policy);
   }

Wednesday, February 27, 2013

Sending data from android to webserver using Post

Finally got the method form this  link, hopefully it will work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     try {
        HttpClient client = new DefaultHttpClient();  
        String postURL = "http://somepostaddress.com";
        HttpPost post = new HttpPost(postURL); 
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("user""kris"));
            params.add(new BasicNameValuePair("pass""xyz"));
            UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
            post.setEntity(ent);
            HttpResponse responsePOST = client.execute(post);  
            HttpEntity resEntity = responsePOST.getEntity();  
            if (resEntity != null) {    
                Log.i("RESPONSE",EntityUtils.toString(resEntity));
            }
    } catch (Exception e) {
        e.printStackTrace();
    }