androidandroid-layouttextviewandroid-textattributes

How to text-justify in android textview and at the same time use custom font style?


public class FontTextView extends TextView {

    public FontTextView(Context context) {
        super(context);
        Typeface face = Typeface.createFromAsset(context.getAssets(),"palatino.ttf");
        this.setTypeface(face);
    }

    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Typeface face = Typeface.createFromAsset(context.getAssets(),"palatino.ttf");
        this.setTypeface(face);
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Typeface face = Typeface.createFromAsset(context.getAssets(),"palatino.ttf");
        this.setTypeface(face);
    }

    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

}

I have a textview like this to using the custom font. The problem is , I would to make it justify as well. I have tried using the html code like this :

testText.setText(Html.fromHtml("<html><head><div class=\"haodi_jianjie\";style=\"text-align:justify;text-justify:distribute-all-lines;color:white;background-color:black;\">asdsadsadsadsadsadsadsafsdvvbcvbcvb dfsdfsdgfgdfg  gdfghththgfhgfhgfhgfhfghgfhgfhgf sdfsdfsdfsdfsdfsdfsfwqewqewq</div></head></html>"));

but it doesn't work. I prefer not using web-view as the transparent background is buggy for the webview but I need the background to be transparent.

Thanks a lot.


Solution

  • We have created a simple class for this. Requires NO WEBVIEW and SUPPORTS SPANNABLES. You can also provide your own custom fonts!

    LIBRARY: https://github.com/bluejamesbond/TextJustify-Android

    Img