Display a custom font or View in the preview section of Android Studio XML

Is there a way to view custom fonts / views in the preview section of Android Studio?

I used font-awesome as a custom font to show the microphone icon in my application. Everything is working fine. But, as we all know, the preview section cannot load custom views.

Is there any plugin or hack custom views in the preview window during encoding?

This is what I load into my application:

enter image description here

This is what I see in the preview section:

enter image description here

+4
source share
2 answers

To make FontAwesome icons visible in the Android Studio XML Designer, you can.

  • Create custom view
  • Apply font in constructor
  • Add custom attr if you want to install font from xml

Here is the full demo code in gist

Demo img with code from comment:

all code in 1 picture

Important parts: (almost the same as declaring an Android user interface UI using XML , but with a little customization)

TextViewWithFont.java - Custom view class

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

public class TextViewWithFont extends TextView {

    public TextViewWithFont(Context context) {
        super(context);
        init(context, null, 0);
    }
    public TextViewWithFont(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }
    public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }
    private void init(Context context, AttributeSet attrs, int defStyle) {
        // Load attributes
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.TextViewPlusFont, 0, 0);
        try {
            String fontInAssets = ta.getString(R.styleable.TextViewPlusFont_customFont);
            setTypeface(Typefaces.get(context, "fonts/"+ fontInAssets));
        } finally {
            ta.recycle();
        }
    }
}

res / values ​​/attrs.xml - need to use app:customFont="fontawesome-webfont.ttf"xml in our layout.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlusFont">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

Typefaces.java. - (Cache for fonts)

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import java.util.Hashtable;

public class Typefaces {
    private static final String TAG = "Typefaces";

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),
                            assetPath);
                    cache.put(assetPath, t);
                    Log.e(TAG, "Loaded '" + assetPath);
                } catch (Exception e) {
                    Log.e(TAG, "Could not get typeface '" + assetPath
                            + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}

activity_main.xml - TextViewWithFont

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.example.TextViewWithFont
        xmlns:app="http://schemas.android.com/apk/res/com.example"
        app:customFont="fontawesome-webfont.ttf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="\uf1e2"
        android:textSize="60dp"/>
</LinearLayout>
+9

, . , draw/onDraw/dispatchDraw, , , , editMode ..

, Android Studio , . , .

, Android Studio. , .

+3

Source: https://habr.com/ru/post/1622919/


All Articles