Trying to use an EditText inside an infoWindow in android studio (Java)

47 views Asked by At

I've recently been trying to place an EditText inside an InfoWindow so that the user can click on the InfoWindow and annotate the field with whatever text they want. I'm aware that InfoWindows are just rendered images and not live, so I've attempted to implement a OnInfoWindowClickListener onto the InfoWindow to bring up the keyboard and edit the EditText directly, but that has also failed to work.

Here is the code for the method that handles the maps and the InfoWindows. I've removed any unnecessary parts for ease of reading:

@Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        gMap = googleMap;

        gMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Nullable
            @Override
            public View getInfoContents(@NonNull Marker marker) {
                View iwv = getLayoutInflater().inflate(R.layout.infowindow, null);
                return iwv;
            }

            @Nullable
            @Override
            public View getInfoWindow(@NonNull Marker marker) {
                return null;
            }
        });

       
        gMap.setOnMapClickListener(new GoogleMap.OnMapClickListener()
        {
            @Override
            public void onMapClick(@NonNull LatLng point) {
                
                if(flag) { //toggle the ability to turn markers on or off
                    gMap.addMarker(new MarkerOptions()
                            .position(point)).showInfoWindow();
                }
            }
        });

        gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(@NonNull Marker marker) {
                View v = li.inflate(R.layout.infowindow, null);
                infoWindowText = v.findViewById(R.id.iw_et);
                handleKeyboard(infoWindowText);
            }
        });
    }

 public void handleKeyboard(View view)
    {
        if (view.requestFocus()) {

            InputMethodManager imm = getSystemService(InputMethodManager.class);
            imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
        }
    }

I'm also aware that imm.toggleSoftInput has been depreciated and that the issue could therefor be caused by it and not the clicklistener, however trying to use imm.ShowSoftInput instead fails to bring the keyboard up at all.

Is what I'm trying to do simply impossible, or am I just using the click listener wrong? Many thanks in advance.

1

There are 1 answers

1
CommonsWare On BEST ANSWER
        gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(@NonNull Marker marker) {
                View v = li.inflate(R.layout.infowindow, null);
                infoWindowText = v.findViewById(R.id.iw_et);
                handleKeyboard(infoWindowText);
            }
        });

When the user clicks on the info window, you:

  • Create a completely new EditText, as part of inflating a completely new R.layout.infowindow layout
  • Ask a keyboard to display for that EditText

However, that EditText is nowhere on the screen. You basically created the Java objects for it, but that's it.

Contrast that with:

            public View getInfoContents(@NonNull Marker marker) {
                View iwv = getLayoutInflater().inflate(R.layout.infowindow, null);
                return iwv;
            }

This works because you are returning the inflated layout, and the calling code does something useful with it (in this case, rendering that layout to a bitmap). However, even in that case, the EditText itself never appears on the screen, only an image of it.

Is what I'm trying to do simply impossible

If you have hundreds of hours to spend, you could:

  • Attempt to derive exactly where on the screen the info window resides and the effective bounds of your original info window content
  • Arrange to display your newly-inflated info window content at those same coordinates, via some container floating over the info window at the right spot into which you inflate the new info window content
  • After the user does something (not sure what), get rid of that floating container plus trigger the map to reload the real info window (or do whatever else you need to do with the data entered by the user)

Doing that initially might only take tens of hours, but you are not the developer of Google Maps. Not only will there be many versions of Google Maps out on user devices, but those versions will change over time. You might need to tailor this logic for different versions of Google Maps to get it working correctly, both today and over time. You also have variations of hardware to take into account, varying soft keyboards, and the like.

Personally, if my graphic designer requested this functionality, I would slap them with a trout, then negotiate something less risky (e.g., tapping on the info window display a bottom sheet where the user can do the data entry).