Change text source from android APP

Asked

Viewed 16,126 times

6

I’m developing an Android APP on Android Developer Tools. I would like to know how to change the text source that is standard from Android to the source Museo.otf, and how to apply to all Textviews. thank you in advance

3 answers

10

There are a number of steps to be taken for the correct implementation of non-standard fonts in an application, as well as some considerations.

Below is a comprehensive explanation of the implementation of fonts in an Android application:

  1. File location

    To properly store the files of the fonts you are going to use, they should be within the following path in your project:

    /assets/fonts
    

    You should already have the /assets, missing create the sub-folder /fonts.

  2. File formats

    There are two supported formats TTF and OTF, being the first most recommended for compatibility reasons, although both work.

    Note:
    The file and its extension must be lowercase:

    minha_fonte.ttf (correcto)
    minha_fonte.TTF (incorrecto)
    minha_Fonte.ttf (incorrecto)
    Museo.otf       (incorrecto) terá que ficar museo.otf
    
  3. Create XML to name custom properties

    Android SDK allows you to name custom properties for your widgets, but for this you need to create an XML in the location below with the name attrs.xml:

    /res/values
    

    The XML to be created will have the following code:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <declare-styleable name="MyTextView">
        <attr name="minhaFonte" format="string" />
      </declare-styleable>
    </resources>
    
  4. Layout

    We can now draw up the layout which must be present in the file activity_main.xml at the location indicated below:

    /res/layout/activity_main.xml
    

    In that file we apply the following code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:customfontdemo="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" 
      android:gravity="center">
    
     <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:padding="12dp"
        android:text="Fonte padrão do Android" />
    
      <com.authorwjf.customfontdemo.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="32sp"
        android:padding="12dp"
        customfontdemo:minhaFonte="minha_fonte.ttf"
        android:text="A minha fonte no Android" />
    
    </LinearLayout>
    

    As you can see, we’re already making custom property fontName.

  5. Class

    Create a class below with the name MyTextView that will extend the text view default, the file should be in the following location:

    /src
    

    Named after the class we’re creating:

    /src/MyTextView.java
    

    Containing the following code:

    package com.authorwjf.customfontdemo;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
    import android.widget.TextView;
    
    public class MyTextView extends TextView {
    
      public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
      }
    
      public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
      }
    
      public MyTextView(Context context) {
        super(context);
        init(null);
      }
    
      private void init(AttributeSet attrs) {
        if (attrs!=null) {
          TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
          String minhaFonte = a.getString(R.styleable.MyTextView_minhaFonte);
    
          if (minhaFonte!=null) {
            Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+minhaFonte);
            setTypeface(myTypeface);
          }
    
          a.recycle();
        }
      }
    }
    

    Here we are collecting the name of the custom attribute source and applying the typeface.

  6. Mainactivity.java

    Given that the text view is in an independent environment, we do not need to make modifications to the file MainActivity.java located in the folder below:

    /src/MainActivity.java
    

    The same of which shall have the following code:

    package com.authorwjf.customfontdemo;
    
    import android.os.Bundle;
    import android.app.Activity;
    
    public class MainActivity extends Activity {
    
      @Override
        protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
        }
    }
    

We have thus completed an application on Android that makes use of a font other than the system standard.

Link to download files.


Credits

Credits this tutorial step by step to the William J. Francis on the website Techrepublic in the following topic:

Pro tip: Extend Android’s Textview to use custom fonts (English)

0

0

I will use as an example the source orange_juice.

First of all: Create the Assets folder in app/src/main/Assets... If you have difficulty locating, change the view of your project:

inserir a descrição da imagem aqui

Paste the font you want into the folder Assets (file . ttf or . otf).

In the onCreate method put the following:

public class MainActivity extends AppCompatActivity {

    private TextView tv01;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv01=(TextView)findViewById(R.id.tv01);
        Typeface font = Typeface.createFromAsset(getAssets(), "orange_juice.ttf");
        tv01.setTypeface(font);

    }
}

The code is very simple in reality.

Notice that the class Typeface has the method createFromAsset. It takes as parameter the default folder of the Assets and the name of the source file to be used.

It is worth noting that when talking about customizable fonts, always keep in mind that they may not work properly, especially if it does not have all the special characters that the application may require.

inserir a descrição da imagem aqui

SOURCE => http://www.mobits.com.br/2011/5/20/personalizando-fontes-no-android

END!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.