Search code examples
androidgoogle-mapslistviewgoogle-maps-api-2customdialog

Showing Custom ListView Dialog as the infoWindow of Map Marker is tapped


I was able to link the infoWindow of the Map Marker to a TextView activity. When infoWindow is tapped, a textView activity appears. So, I want to try it to link to a ListView Custom Dialog. So far I'm not getting any progress because every time the I try to tap the infoWindow, the program terminates. I found no warnings in my code though. Probably the codes are still lacking. Will someone help me? I'm just doing self study here.

I have this from the MainActivity, this is for the click event of the infoWindow

    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener(){

    @Override
    public void onInfoWindowClick(Marker adminmarker){
    Intent intent = new Intent("com.android.cmumap.ADMIN");
    startActivity(intent);
    }
    });

I have this for the AdminActivity, this is for the Custom Dialog

    package com.android.cmumap;

    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;

    public class AdminActivity extends DialogFragment{

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
    builder.setTitle(R.string.layers)
           .setItems(R.array.layer_options, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    return builder.create();
    }
    }

And I have this for the adminactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/layers"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/layers" />

<ListView android:id="@+id/layer_options"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:divider="#000000"
          android:dividerHeight="1dp"
          android:paddingLeft="1dp" />
</LinearLayout>

Please tell me what is lacking.. I'm not able to do this on my own..


Solution

  • in your code you are exted dialogfragment so it is not an activity. you should extend Activity for that.

    public class AdminActivity extends Activity{
    
            public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this);
            builder.setTitle(R.string.layers)
                   .setItems(R.array.layer_options, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int which) {
                       // The 'which' argument contains the index position
                       // of the selected item
                   }
            });
            return builder.create();
            }
            }
    

    and in your main acitivity do this

    map.setOnInfoWindowClickListener(new OnInfoWindowClickListener(){
    
        @Override
        public void onInfoWindowClick(Marker adminmarker){
        Intent intent = new Intent(MainActivity.this,AdminActivity.class);
        startActivity(intent);
        }
        });
    

    and in you AdminActivity class just override OnCreateMethod and call onCreateDialog function from that (so your admin activity has two method onCreateDialog and onCreate).

       @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
         Dialog dialog=onCreateDialog(savedInstanceState)
            dialog.show();
        }