Banti Kumar Banti Kumar - 4 years ago 79
Android Question

How to add multiple locations in single EditText in android

How to add multiple locations in single EditText in android.We want to create Tags of locations.

Answer Source

I have to written code to add multiple location in single EditText in android.

 void updateAddressLayout() {

    final ViewGroup locationGroup = (ViewGroup) getActivity().findViewById(R.id.locationsLayout);
    for (int i = locationGroup.getChildCount() - 1; i >= 0; i--) {
        locationGroup.removeViewAt(i);
    }
    final LayoutInflater inflater =getActivity().getLayoutInflater();
    for (int i=0; i < mylocations.size(); i++){
        final View locationView = inflater.inflate(R.layout.include_location_tag_chip, locationGroup, false);
        final TextView locationText = (TextView)locationView.findViewById(R.id.info_text);
        final ImageView delImage = (ImageView)locationView.findViewById(R.id.icon_del);
        final String primaryAddress = mylocations.get(i).getAddressLine(0) + " " + mylocations.get(i).getAddressLine(1) + " " + mylocations.get(i).getLocality() + " " + mylocations.get(i).getPostalCode();
        locationText.setText(primaryAddress);
        final int finalI = i;
        delImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("Alert!")
                        .setMessage("Are you sure you want to delete location ")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               mylocations.remove(finalI);
                                locationGroup.removeViewAt(finalI);
                            }
                        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
            }
        });
        locationGroup.addView(locationView);
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_GET_LOCATION) {
            mCurrentAddress = data.getParcelableExtra("LOC_ADDRESS");
            Toast.makeText(getActivity(),"You can add only three locations", Toast.LENGTH_LONG).show();
            if (mCurrentAddress.getAddressLine(0) != null || mCurrentAddress.getAddressLine(1) != null || mCurrentAddress.getLocality() != null || mCurrentAddress.getPostalCode() != null) {
                    if (mylocations.size() <3) {
                    mylocations.put(mylocations.size(), (Address) data.getParcelableExtra("LOC_ADDRESS"));
                }
                updateAddressLayout();
            }
            return;
        }
    }
    if (resultCode == Activity.RESULT_CANCELED && requestCode == REQUEST_GET_LOCATION) {
    }
    super.onActivityResult(requestCode, resultCode, data);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download