0

I'm trying to add custom view to google map Marker:

  1. I inflate a layout. <- this is the problem
  2. Then transform it to Bitmap.
  3. Generate Marker.


So I have layout home_marker_container.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="@dimen/home_map_marker_size"
    android:layout_height="@dimen/home_map_marker_size"
    android:background="@color/black"
    android:id="@+id/frame">


    <TextView
        android:layout_width="12dp"
        android:layout_height="12dp"
        android:text="5"
        android:textSize="10dp"
        android:background="@color/white"
        android:layout_centerInParent="true"
        android:gravity="center" />

</RelativeLayout>

and I inflate it with

ViewGroup home_marker_container = (ViewGroup) layoutInflater.inflate(R.layout.home_marker_container, null, false);


Then transform View to Bitmap:

So I add it to marker with Google Maps Android API Utility Library via IconGenerator which basically transforms View to Bitmap:

iconGenerator.setContentView(home_marker_container);
iconGenerator.setContentPadding(horizontal_padding, vertical_padding, horizontal_padding, vertical_padding);
Bitmap iconBitmap = iconGenerator.makeIcon();

And after that I realized that inflating does not work as I expected because I give null to ViewGroup root parameter in inflate(int resource, ViewGroup root, boolean attachToRoot) and thus

android:layout_width="@dimen/home_map_marker_size"
android:layout_height="@dimen/home_map_marker_size"

is not taken from xml. Why and how to fix it?

Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103
  • 1
    Have a look at http://www.nasc.fr/android/android-using-layout-as-custom-marker-on-google-map-api/ – harrane Mar 04 '15 at 16:02
  • @Barbelo thanks, however the code from link doesn't seem to follow Android layout inflating rules, your hints (now deleted by SO, why?) and the link got me to answer. – Marian Paździoch Mar 05 '15 at 08:25
  • I deleted it because it doesnt look to be related to the Google Maps Marker – harrane Mar 05 '15 at 09:39

1 Answers1

-1

As there's no root, according to docs, root is returned from inflate so I set size to the root manually with:

View markerView;
markerView = layoutInflater.inflate(R.layout.marker1, null);
markerView.setLayoutParams(new ViewGroup.LayoutParams(80, 80));;
Marian Paździoch
  • 8,813
  • 10
  • 58
  • 103