Search code examples
androidgoogle-mapsbitmapdrawable

How to set Marker icon using fontawesome


Drawable iconmarker = FontIconDrawable.inflate(context, R.xml.ic_red_marker);

Bitmap bitmap = ((BitmapDrawable) iconmarker).getBitmap();


driver_marker = googleMap.addMarker(new MarkerOptions()
                    .position(latLng)
                    .icon(BitmapDescriptorFactory.fromBitmap(bitmap)));*/

ic_red_marker.xml file

<?xml version="1.0" encoding="utf-8"?>
 ----  font-icon
    xmlns:android="http://schemas.android.com/apk/res-auto"
    android:text="@string/icon_marker"
    android:textSize="@dimen/icon_size"
    android:textColor="@android:color/white"/>

font-awesome code

<string name="icon_marker">&#xf041;</string>

ERROR

line- Bitmap bitmap = ((BitmapDrawable) iconmarker).getBitmap();

Caused by: java.lang.ClassCastException: com.shamanland.fonticon.FontIconDrawable cannot be cast to android.graphics.drawable.BitmapDrawable


Solution

  • You can set IconDrawable as below :

    IconDrawable drawable =  new IconDrawable(this, IconValue.fa_home).colorRes(R.color.ab_item)
                            .actionBarSize())
    

    Now you can set IconDrawable to Imageview like below :

    imageview.setImageDrawable(drawable);
    

    And now if you want bitmap then try

    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    

    For this you need to add android-iconify jar to your libs folder.

    Reference :

    1) https://github.com/JoanZapata/android-iconify

    2) http://www.java2s.com/Code/Jar/a/Downloadandroidiconifysample101jar.htm

    Hope it will help you.