Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android MapView inside AlertDialog

I am trying to implement a MapView in AlertDialog but the MapView is null every time. I call, on button click, the method hotelBooking which is inside an Activity. What am i missing here?

public void hotelBooking(final String hotelname){
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
    alertDialog.setTitle("Hotel Info");
    alertDialog.setIcon(R.drawable.action_hotels);
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View promptView = layoutInflater.inflate(R.layout.traveler_hotel_registration, null);
    alertDialog.setView(promptView);

    MapView mMapView = (MapView) alertDialog.findViewById(R.id.mapView2);
    MapsInitializer.initialize(this);

    mMapView.onCreate(alertDialog.onSaveInstanceState());
    mMapView.onResume();


    mMapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final GoogleMap googleMap) {
            LatLng posisiabsen = new LatLng(40.626401, 22.948352); ////your lat lng
            googleMap.addMarker(new MarkerOptions().position(posisiabsen).title(hotelname));
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(posisiabsen));
            googleMap.getUiSettings().setZoomControlsEnabled(true);
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
        }
    });

    final RatingBar rb = (RatingBar) promptView.findViewById(R.id.ratingBar);
    rb.setRating(3);

    final TextView hoteltitle= (TextView)promptView.findViewById(R.id.HotelInfoTitle);
    hoteltitle.setText("Hotel " + hotelname);

    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Favorite", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int which)
        {
            Toast.makeText(getApplicationContext(), "Hotel has been saved to favorites!",
                    Toast.LENGTH_LONG).show();

        }

    });
    alertDialog.show();
}
like image 689
Kzaf Avatar asked Dec 08 '25 11:12

Kzaf


1 Answers

 MapView mMapView = (MapView) promptView.findViewById(R.id.mapView2);

Try promtView.findViewById instead of alertDialog.findViewById

like image 97
blackkara Avatar answered Dec 11 '25 01:12

blackkara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!