lundi 20 juin 2016

Getting the Searched Address autofilled from GooglePlacesAutoCompleteActivity to ExtractEditText View

I am new to StackOverflow so apologies for any mistakes. The problem I am stuck on is whenever I click the ExtractEditTextView it takes me to the PlacesAutocomplete Activity and when I type the address it shows me the address on the searchbar and I am not able to get the address in my ExtractEditTextView filled automatically, so please help.

public class FindSamewayRidesActivity extends AppCompatActivity implements View.OnClickListener {

    int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;
    private static final int REQUEST_CODE_AUTOCOMPLETE = 1;
    private static final String TAG = null;
    private ExtractEditText mPlaceDetailsText;
    private TextView mPlaceAttribution;

    // Widget GUI
    ExtractEditText timetext, datetxt;

    // Variable for storing current date and time
    private int mYear, mMonth, mDay, mHour, mMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_sameway_rides);

        // Open the autocomplete activity when the ExtractEditText is clicked.
        ExtractEditText openExtractEditText = (ExtractEditText) findViewById(R.id.autoCompleteTextView);
        assert openExtractEditText != null;
        openExtractEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openAutocompleteActivity();
            }

        });

        openExtractEditText = (ExtractEditText) findViewById(R.id.autoCompleteTextView2);
        assert openExtractEditText != null;
        openExtractEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                openAutocompleteActivity();
            }
        });

        timetext = (ExtractEditText) findViewById(R.id.timetext);
        datetxt = (ExtractEditText) findViewById(R.id.datetxt);

        timetext.setOnClickListener(this);
        datetxt.setOnClickListener(this);
    }

    private void openAutocompleteActivity() {
        try {
            // The autocomplete activity requires Google Play Services to be available. The intent
            // builder checks this and throws an exception if it is not the case.
            Intent intent = new PlaceAutocomplete.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
                    .build(this);
            startActivityForResult(intent, REQUEST_CODE_AUTOCOMPLETE);
        } catch (GooglePlayServicesRepairableException e) {
            // Indicates that Google Play Services is either not installed or not up to date. Prompt
            // the user to correct the issue.
            GoogleApiAvailability.getInstance().getErrorDialog(this, e.getConnectionStatusCode(),
                    0 /* requestCode */).show();
        } catch (GooglePlayServicesNotAvailableException e) {
            // Indicates that Google Play Services is not available and the problem is not easily
            // resolvable.
            String message = "Google Play Services is not available: " +
                    GoogleApiAvailability.getInstance().getErrorString(e.errorCode);

            Log.e(TAG, message);
            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * Called after the autocomplete activity has finished to return its result.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        // Check that the result was from the autocomplete widget.
        if (requestCode == REQUEST_CODE_AUTOCOMPLETE) {
            if (resultCode == RESULT_OK) {
                // Get the user's selected place from the Intent.
                Place place = PlaceAutocomplete.getPlace(this, data);
                Log.i(TAG, "Place Selected: " + place.getName());

                // Format the place's details and display them in the TextView.
                mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(),
                        place.getId(), place.getAddress(), place.getPhoneNumber(),
                        place.getWebsiteUri()));

                // Display attributions if required.
                CharSequence attributions = place.getAttributions();
                if (!TextUtils.isEmpty(attributions)) {
                    mPlaceAttribution.setText(Html.fromHtml(attributions.toString()));
                } else {
                    mPlaceAttribution.setText("");
                }
            } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
                Status status = PlaceAutocomplete.getStatus(this, data);
                Log.e(TAG, "Error: Status = " + status.toString());
            } else if (resultCode == RESULT_CANCELED) {
                // Indicates that the activity closed before a selection was made. For example if
                // the user pressed the back button.
            }
        }
    }

    /**
     * Helper method to format information about a place nicely.
     */
    private static Spanned formatPlaceDetails(Resources res, CharSequence name, String id,
                                              CharSequence address, CharSequence phoneNumber, Uri websiteUri) {
        Log.e(TAG, res.getString(R.string.place_details, name, id, address, phoneNumber,
                websiteUri));
        return Html.fromHtml(res.getString(R.string.place_details, name, id, address, phoneNumber,
                websiteUri));

    }

This is xml.

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin">

        <android.inputmethodservice.ExtractEditText
            android:id="@+id/autoCompleteTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_toEndOf="@+id/imageView3"
            android:cursorVisible="false"
            android:hint="@string/your_starting_point"
            android:maxLines="1"
            android:singleLine="true"
            android:autoLink="all"/>


        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:contentDescription="@string/fromcircle"
            android:scaleX="0.6"
            android:scaleY="0.6"
            android:src="@mipmap/circle1" />

    </RelativeLayout>

Aucun commentaire:

Enregistrer un commentaire