Thursday, September 26, 2013

Get Address from reverse geocoding android

Get Address from reverse geocoding android 

what is reverse geo coding?
Reverse geocoding is the process of back (reverse) coding of a point location (latitude, longitude) to a readable address or place name. This permits the identification of nearby street addresses, places, and/or areal subdivisions such as neighbourhoods, county, state, or country.

Activity Source Code
package com.example.reversegeocoding;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              if (android.os.Build.VERSION.SDK_INT > 9) {
                     StrictMode.ThreadPolicy stricMode = new StrictMode.ThreadPolicy.Builder()
                                  .permitAll().build();
                     StrictMode.setThreadPolicy(stricMode);
              }
              TextView address_textview =(TextView)findViewById(R.id.address);
              address_textview.setText(getAddress());
       }
       public String getAddress() {
       String address1 = "";
       String  address2 = "";
       String  city = "";
       String state = "";
       String country = "";
       String county = "";
       String  PIN = "";
        String full_address = "";
        try {

            JSONObject jsonObj = getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" +"12.976457" + ","
                    + "77.618194" + "&sensor=true");
            String Status = jsonObj.getString("status");
            if (Status.equalsIgnoreCase("OK")) {
                JSONArray Results = jsonObj.getJSONArray("results");
                JSONObject zero = Results.getJSONObject(0);
                JSONArray address_components = zero.getJSONArray("address_components");

                for (int i = 0; i < address_components.length(); i++) {
                    JSONObject zero2 = address_components.getJSONObject(i);
                    String long_name = zero2.getString("long_name");
                    JSONArray mtypes = zero2.getJSONArray("types");
                    String Type = mtypes.getString(0);

                    if (TextUtils.isEmpty(long_name) == false || !long_name.equals(null) || long_name.length() > 0 || long_name != "") {
                        if (Type.equalsIgnoreCase("street_number")) {
                            address1 = long_name + " ";
                        } else if (Type.equalsIgnoreCase("route")) {
                            address1 = address1 + long_name;
                        } else if (Type.equalsIgnoreCase("sublocality")) {
                            address2 = long_name;
                        } else if (Type.equalsIgnoreCase("locality")) {
                            // Address2 = Address2 + long_name + ", ";
                            city = long_name;
                        } else if (Type.equalsIgnoreCase("administrative_area_level_2")) {
                            county = long_name;
                        } else if (Type.equalsIgnoreCase("administrative_area_level_1")) {
                            state = long_name;
                        } else if (Type.equalsIgnoreCase("country")) {
                            country = long_name;
                        } else if (Type.equalsIgnoreCase("postal_code")) {
                            PIN = long_name;
                        }
                       
                    }

                    full_address = address1 +","+address2+","+city+","+state+","+country+","+PIN;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
              return full_address;

    }
        public static JSONObject getJSONfromURL(String url) {

               // initialize
               InputStream is = null;
               String result = "";
               JSONObject jObject = null;

               // http post
               try {
                   HttpClient httpclient = new DefaultHttpClient();
                   HttpPost httppost = new HttpPost(url);
                   HttpResponse response = httpclient.execute(httppost);
                   HttpEntity entity = response.getEntity();
                   is = entity.getContent();

               } catch (Exception e) {
                   Log.e("log_tag", "Error in http connection " + e.toString());
               }

               // convert response to string
               try {
                   BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                   StringBuilder sb = new StringBuilder();
                   String line = null;
                   while ((line = reader.readLine()) != null) {
                       sb.append(line + "\n");
                   }
                   is.close();
                   result = sb.toString();
               } catch (Exception e) {
                   Log.e("log_tag", "Error converting result " + e.toString());
               }

               // try parse the string to a JSON object
               try {
                   jObject = new JSONObject(result);
               } catch (JSONException e) {
                   Log.e("log_tag", "Error parsing data " + e.toString());
               }

               return jObject;
           }

}

 XML Source Code

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:text="@string/hello_world" />

</RelativeLayout>



Thursday, September 12, 2013

Remove and Undo Listview Item with slide in Android

Remove and Undo Listview Item with slide in Android


This post about how to Undo removed item from listview like Gmail.
Screen Shot 


Source Code 

Activity Code :

package com.example.undo;

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyListActivity extends ListActivity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              getListView().setDivider(null);
       }

       protected ArrayAdapter<String> createListAdapter() {
              return new MyListAdapter(this, getItems());
       }

       public static ArrayList<String> getItems() {
              ArrayList<String> items = new ArrayList<String>();
              for (int i = 0; i < 1000; i++) {
                     items.add(String.valueOf(i));
              }
              return items;
       }

       private static class MyListAdapter extends ArrayAdapter<String> {

              private Context mContext;

              public MyListAdapter(Context context, ArrayList<String> items) {
                     super(items);
                     mContext = context;
              }

              @Override
              public long getItemId(int position) {
                     return getItem(position).hashCode();
              }

              @Override
              public View getView(int position, View convertView, ViewGroup parent) {
                     TextView tv = (TextView) convertView;
                     if (tv == null) {
                           tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.list_row, parent, false);
                     }
                     tv.setText("This is row number " + getItem(position));
                     return tv;
              }
       }
}

****************************************************************************************

*********************************************************************




Wednesday, September 4, 2013

Android disable blinging cursor on Edittext view.

Android disable blinking cursor on Edittext view.

This post about how to disable blinking cursor on edittext view.
editText.setCursorVisible(false);

Monday, September 2, 2013

Android add Vertical scroll for TextView

Android add Vertical scroll for TextView
This post about how to add vertical scroll bar for Textview
Check below ex:
TextView textView = new TextView(this);
textView.setMovementMethod(new ScrollingMovementMethod());

textView.setText("Hello");

Sunday, September 1, 2013

java.lang.IllegalArgumentException: Illegal character in scheme at index 0

java.lang.IllegalArgumentException: Illegal character in scheme at index 0

Problem
When i give space infront http . I got this error.
09-30 18:00:32.568: W/System.err(19507): java.lang.IllegalArgumentException: Illegal character in scheme at index 0:   http:******************";

Ex: String url = " http:******************";

Solution

Simply removed space from infron of "http"

Ex : String url = "http:******************";


Check out this may be help you

Related Posts Plugin for WordPress, Blogger...