0

I am making an app that uses maps api, and I want to create a custom popup activity when the user creates a marker (not the generic yes/no dialogue). What I want to do is:

  1. when a user longclicks on map it opens a new activity instead of dialogue
  2. this activity displays lat and lang, has a field to enter text, and yes/no buttons
  3. if a user clicks yes the marker is pinned if no it is not
  4. when a user clicks on the created marker it displays lat,lng and the entered text

My current code:

    //Add marker on long click
    mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

    @Override
    public void onMapLongClick(LatLng arg0) {

        Intent intent = new Intent(getActivity(), CreateRestautantActivity.class);
        startActivity(intent);

        marker = mMap.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker))
                .position(
                        new LatLng(arg0.latitude,
                                arg0.longitude))
                .visible(true));

      }
    });

Any help is appreciated :)

Banana
  • 2,435
  • 7
  • 34
  • 60
  • why not you are using custom dialogue rather opening activity , if you create separate activity for that map reloads again when you come back again map !! so i would suggest custom dialogue better than this. – Jhaman Das Apr 10 '16 at 13:56
  • @Jhaman Das I plan to expand this popup - add a date/time option, tag some friends, enable chat, etc, so i thought it would be better to do this as an activity. – Banana Apr 10 '16 at 15:30

2 Answers2

1

You should use a DialogFragment. This allows you to customize the appearance and functionality of a dialog.

Android blog post on how to use them: http://android-developers.blogspot.com/2012/05/using-dialogfragments.html

Reference: https://developer.android.com/reference/android/app/DialogFragment.html

Several other tutorials/examples: https://www.google.com/search?q=dialogfragment

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
nasch
  • 5,330
  • 6
  • 31
  • 52
  • I didn't want to use the dialogue because I plan to make this popup a lot more complex - add a date/time option, tag some friends (from a popup dialogue), enable chat, etc, so i thought it would be better to do this as an activity. – Banana Apr 10 '16 at 15:25
  • 1
    Anything you can do in an activity you can do in a `DialogFragment`. I think this will be a lot simpler than trying to manage navigation between two activities. – nasch Apr 10 '16 at 21:10
  • My biggest concern was that I would have to open contacts dialogue from DialogueFragment (when I add an option to select the friends to tag on a marker). It seemed like something that should not be done. Thanks for the explanation, it really helps :) – Banana Apr 11 '16 at 10:31
1

Ok, maybe I didn't got all the details, but in such case I'd do the following:

1. Use OnMapLongClickListener. Then in onMapLongClick() you can do whatever you want. You have LatLng object with latitude and longitude - you can put them in a Bundle or pass them in other way to new Activity.
You can refer this post for example.
Create a Marker and store it as a field in your Activity.

2. It's easy, I suppose, to make xml for new Activity and use it at setContentView(). Take your arguments passed in previous step and put them in lat and lng fields. Put OnClickListener on your buttons.

3. You can start your new Activity with StartActivityForResult method and handle results from your new Activity on return. There is also a lot of information about that on stackoverflow. For example this.
Then, in onActivityResult() you can make some actions to your Marker (remove it, for example).

4. Use OnMarkerClickListener - and in onMarkerClick(Marker marker) from that Marker you can call getPosition() - and take lat and lng from it.

If you need more details, let me know ;)

Community
  • 1
  • 1
Goltsev Eugene
  • 3,325
  • 6
  • 25
  • 48
  • This looks like the thing I was looking for, thank you for this. I have added my current code in my question. It is what I did before asking this question. I am a beginner in software development so it is not a lot. I will let you know if I get stuck. Thanks again :) – Banana Apr 10 '16 at 15:37
  • 1
    U welcome ;) Also please consider for future using fragment (or DialogFragment, as @nasch mentioned). It can be complicated as much as you need. It may be difficult for the first time to use it, but using Fragments instead of Activities finally gives your application more flexibility. – Goltsev Eugene Apr 10 '16 at 15:42
  • But for first time, it will be good also to make it with Activity ) – Goltsev Eugene Apr 10 '16 at 15:42
  • I will likely do that. As I said I was afraid to use it because I would call another dialog from it with my contacts list. But as you said, since it can be complex I can use it anyways. I guess that's why I haven't found any examples for activity :) – Banana Apr 10 '16 at 15:50