Friday, January 18, 2013

Custom InfoWindowAdapter with dynamic icon

Last exercise demonstrate how to "implement custom InfoWindowAdapter", with fixed icon defined in XML. We can generate the icon dynamically in run-time.

Custom InfoWindowAdapter with dynamic icon


To implement dynamic icon, modify layout of the info windows(custom_info_contents.xml) - remove the assigned android:src and assigne android:id="@+id/icon".
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:adjustViewBounds="true"/>
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12dp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="10dp"/>
    </LinearLayout>

</LinearLayout>


Modify getInfoContents(Marker marker) to load the icon in run-time.
  @Override
  public View getInfoContents(Marker marker) {
   TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
   tvTitle.setText(marker.getTitle());
   TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
   tvSnippet.setText(marker.getSnippet());
            
   ImageView ivIcon = ((ImageView)myContentsView.findViewById(R.id.icon));
   ivIcon.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_menu_gallery));
   
   return myContentsView;
  }



The series:
A simple example using Google Maps Android API v2, step by step.

4 comments:

Unknown said...

hello,
your blog is very interresting!!

a test all code on my S3, good
i have a probleme with this exemple
a this line
ImageView ivIcon = ((ImageView)myContentsView.findViewById(R.id.icon));
with 'imageview'

??
no class??

thank
Chris

Unknown said...

hello bis

i try to find script to do marker with xml data on web
....

very difficulte
a see some sript to parse xml with android
but to put this code with googlemap v2........
olalalala

thank for your help
Chris

Erik said...

hello Christophe Landrain,

Yes, it's a ImageView(with id=icon) defined in xml.

I don't understand what you means "no class"

andrew said...

how do i set the icon from URL? can you expalain it please?