0

I'm trying to set custom typeface for map markers' cluster icon using SquareTextView, what is a class of android-maps-utils-amap library extending TextView. In DefaultClusterRenderer I'm using this code to set custom typeface but no effect. So please help me to understand what I need to do to change the typeface of SquareTextView

private SquareTextView makeSquareTextView(Context context) {
    SquareTextView squareTextView = new SquareTextView(context);            
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), "whatever.ttf");
    squareTextView.setTypeface(typeface);
}

And this is the source code of SquareTextView:

package com.amap.api.maps2d.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class SquareTextView extends TextView {
    private int mOffsetTop = 0;
    private int mOffsetLeft = 0;

    public SquareTextView(Context context) {
        super(context);
    }

    public SquareTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        int dimension = Math.max(width, height);
        if (width > height) {
            mOffsetTop = width - height;
            mOffsetLeft = 0;
        } else {
            mOffsetTop = 0;
            mOffsetLeft = height - width;
        }
        setMeasuredDimension(dimension, dimension);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
        super.draw(canvas);
    }
}
Kar4
  • 25
  • 8
  • Are you sure you have `Roboto-Italic.ttf` file in assets directory, and not in any sub directory? Also, you can try using `Calligraphy` and skip all this setTypeface nonsense if you want to set Application wide typeface – Sourabh Mar 27 '16 at 15:11

2 Answers2

0

I have my own implementation for TextView and I use it for setting typeface like the following, and it worked for me:

public class MyNewTextView extends TextView {
private int mOffsetTop = 0;
private int mOffsetLeft = 0;

public MyNewTextView(Context context) {
    super(context);
}

public MyNewTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public MyNewTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public void setTypeface(Typeface tf, int style) {
     super.setTypeface(Typeface.createFromAsset(getContext().getAssets(),   "fonts/whatever.ttf"), style);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int width = getMeasuredWidth();
    int height = getMeasuredHeight();
    int dimension = Math.max(width, height);
    if (width > height) {
        mOffsetTop = width - height;
        mOffsetLeft = 0;
    } else {
        mOffsetTop = 0;
        mOffsetLeft = height - width;
    }
    setMeasuredDimension(dimension, dimension);
}

@Override
public void draw(Canvas canvas) {
    canvas.translate(mOffsetLeft / 2, mOffsetTop / 2);
    super.draw(canvas);
}
}

So try to override setTypeface() for SquareTextView

Amt87
  • 5,493
  • 4
  • 32
  • 52
  • But SquareTextView is a library class, so I can't edit it, and in my case I can't use TextView other than the SquareTextView – Kar4 Mar 27 '16 at 14:44
  • What I see in the implementation of SquareTextView is that you can copy onMeasure() and onDraw() implementation to your OwnTextView, Check My edit please. is it OK? – Amt87 Mar 27 '16 at 14:48
  • 1
    Thank you, I'm using it to change the font of the cluster textview so I need to use it In DefaultClusterRenderer where cant use a class extending TextView, but even if I extend SquareTextView (like in the 2-nd answer) in any case it does not work again – Kar4 Mar 27 '16 at 17:15
0

Make your own implementation of SquareTextView Try this:

public class MySquareTextView extends SquareTextView {

    public MySquareTextView (Context context) {
        super(context);
        setTypeface();
    }

    public MySquareTextView (Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface();
    }

    public MySquareTextView (Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface();
    }


    public void setTypeface(){
        setTypeface(Typeface.createFromAsset(getAssets(), "Roboto-Italic.ttf"));
    }
}
dieter_h
  • 2,707
  • 1
  • 13
  • 19
  • There is no difference if squareTextView.setTypeface(typeface); not working it means that mySquareTextView.setTypeface(typeface); would not work too, because in fact in both cases it uses the same thing – Kar4 Mar 27 '16 at 16:07
  • Did you try? I have the same code in my application and it's working. If it's not working than your `font` path is wrong. – dieter_h Mar 27 '16 at 16:11
  • Yes I did, and no result and path is ok, are you using it to change the typeface of markers' cluster icon text? – Kar4 Mar 27 '16 at 16:15
  • I use it to change `TypeFace` in `TextView`. – dieter_h Mar 27 '16 at 16:18
  • in simple TextView it works for my case too, but with cluster icon it doesn't – Kar4 Mar 27 '16 at 17:06