0

I'm new Android learner and here is my problem.

I'm working with android API 15+. My goal is to build a custom AutoCompleteTextView in the ActionBar. Custom because I would have a button in the AutoCompleteTextView which would allow users to use their voices instead of typing some word.

How can i do this? Should i use some Adapter? Create a custom widget? I did not find a way to do that.

Siddharth
  • 9,349
  • 16
  • 86
  • 148

1 Answers1

0

I have just been able to complete this. Its really simple. You need a AsyncTask since Android 4+ does not allow network stuff on the UI thread.

Step 1

So I created a private AsyncTask class in my Activity class.

public class AutoCompleteHints extends AsyncTask<String, Void, StringBuilder> {
    private boolean whichPoint;
    private HttpURLConnection conn = null;
    private StringBuilder jsonResults = new StringBuilder();

    public AutoCompleteHints(boolean whichPoint) {
        this.whichPoint = whichPoint;
    }

    @Override
    protected StringBuilder doInBackground(String... str) {
        try {
            StringBuilder sb = new StringBuilder(
                    "http://maps.googleapis.com/maps/api/geocode/json?" + "address="
                            + URLEncoder.encode(str[0], "utf-8") + "&sensor=true");

            URL url = new URL(sb.toString());
            Log.d("Taxeeta", url.toString() + " Called");
            conn = (HttpURLConnection) url.openConnection();
            InputStreamReader in = new InputStreamReader(conn.getInputStream());

            // Load the results into a StringBuilder
            int read;
            char[] buff = new char[1024];
            while ((read = in.read(buff)) != -1) {
                jsonResults.append(buff, 0, read);
            }
        } catch (MalformedURLException e) {
            Log.e("Taxeeta", "Error processing Places API URL", e);
        } catch (IOException e) {
            Log.e("Taxeeta", "Error connecting to Places API", e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
        return jsonResults;
    }

    @Override
    protected void onPostExecute(final StringBuilder result) {
        Double latitude, longitude;
        try {
            // Create a JSON object hierarchy from the results
            JSONObject jsonObj = new JSONObject(result.toString());
            JSONObject location = ((JSONObject) jsonObj.getJSONArray("results").get(0))
                    .getJSONObject("geometry").getJSONObject("location");
            latitude = location.optDouble("lat");
            longitude = location.optDouble("lng");
            Log.d("Taxeeta", "Received Latitude " + latitude + ": Longitude" + longitude);
            if (whichPoint == PICKUP) {
                source = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
                startLocation.placeMarker(source);
                Log.d("Taxeeta", "Source Done");
            } else if (whichPoint == DROP) {
                destination = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
                endLocation.placeMarker(destination);
                Log.d("Taxeeta", "Destination Done");
            } else
                Log.e("Taxeeta", "Something messed up here.");
            // TODO : Path from source to destination
            adjustZoomAndSpan();
        } catch (JSONException e) {
            Log.e("Taxeeta", "Cannot process JSON results", e);
        }
    }

}

Step 2

Called the AsyncTask onItemClick

In onCreate

fromAutoComplete = new AutoComplete(this, R.layout.fromautocomplete);
fromAutoComplete.setNotifyOnChange(true);
fromAddress = (AutoCompleteTextView) findViewById(R.id.fromAddress);
fromAddress.setAdapter(fromAutoComplete);
fromAddress.setOnItemClickListener(this);

For onItemClick on the autocompletetextView

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Log.e("Taxeeta", parent.getId() + ":" + view.getId() + ":" + id);

    ArrayList<String> resultList = null;
    String str = (String) parent.getItemAtPosition(position);

    if (view.getId() == R.id.fromautocomplete) 
        new AutoCompleteHints(PICKUP).execute(str) ;
    else if (view.getId() == R.id.toautocomplete) 
        new AutoCompleteHints(DROP).execute(str) ;

}
Siddharth
  • 9,349
  • 16
  • 86
  • 148