Can I prevent my app from taking the font of the operating system in android studio?

4

What happens is that if the user changes the source of his device, he also changes the source of my app. Can you avoid this?

This is the textview that I want to put, but when I simulate it in my phone, the font appears different, and this is because in the operating system the style of the font is "choco cooky"

<TextView
        android:id="@+id/txtNombre"
        android:layout_width="match_parent"
        android:layout_height="@dimen/height_nombre_margin"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="Jhon Alexander"
        android:textColor="#f4f4f4"
        android:textSize="@dimen/textSize_nombre_margin"
        android:textStyle="bold"/>
    
asked by jhon revelo 25.10.2018 в 19:17
source

2 answers

3

To give a personalized font to your app follow these steps:

A. Configurations regarding the source

  • If there is no directory named res in the font of your app, you must create it. You can right click on res , then select New and then Directory , giving it the name of font to the directory

  • You place in that directory the file (s) that contain your source (s). Be careful, you must use this convention to name them or it will give you an error: nombre_detalle , for example: roboto_regular.ttf

  • In the newly created font directory, you now create a source resource file. You can do it by right-clicking on the font directory, then selecting New and then selecting Font resource file . When I ask for the name, you write it. Enter a consistent name with your font type. If we are working with Roboto, you can call it roboto.xml

    Within that file you can put several references to your source. Be careful, all references that you put there must exist as source files. For example, if you put the code below, you should have these three files in font : roboto_regular.ttf, roboto_italic.ttf, roboto_black.ttf .

  • Code to put in roboto.xml , which would be our sample file:

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/roboto_regular" />
        <font
            android:fontStyle="italic"
            android:fontWeight="400"
            android:font="@font/roboto_italic" />
        <font
            android:fontStyle="normal"
            android:fontWeight="700"
            android:font="@font/roboto_black" />
    
    </font-family>
    

    B. Setting in TextView

    Now you only indicate in the TextView that you want to use a certain type of source, adding the attribute fontFamily , for example:

    <TextView
            android:id="@+id/txtNombre"
            android:layout_width="match_parent"
            android:layout_height="@dimen/height_nombre_margin"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="Jhon Alexander"
            android:textColor="#f4f4f4"
            android:fontFamily="@font/roboto_regular"
            android:textSize="@dimen/textSize_nombre_margin"
            android:textStyle="bold"/>
    

    Or you can do it by programming:

    mTextView =  findViewById(R.id.tuTextView);
    mTypeFace = ResourcesCompat.getFont(this, R.font.roboto_regular);
    mTextView.setTypeface(mTypeFace);
    

    When you put this @font/roboto_regular in the XML of TextView , or put this by programming R.font.roboto_regular , you are indicating that you want the source with the definitions indicated in file created in the stage A3 above. That is, the source roboto_regular , with a style normal and a weight% 400 .

    Et voilà, to enjoy your source Roboto:)

    I leave you an image of how the font folder would be in resources:

        
    answered by 25.10.2018 / 19:36
    source
    1

    You can see the official documentation from Android

    In the res directory you can create the directory font and add source files .tff and .otf

    then you can create a xml

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/lobster_regular" />
        <font
            android:fontStyle="italic"
            android:fontWeight="400"
            android:font="@font/lobster_italic" />
    </font-family>
    

    and in your view you can set the property android:fontFamily

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/lobster"/>
    

    or in style

    <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/lobster</item>
    </style>
    

    can also be programmed

    val typeface = resources.getFont(R.font.myfont)
    textView.typeface = typeface
    

    there is a support library

      

    Support Library 26.0 provides support for the function   Sources in XML on devices running Android 4.1 (API level   16) and higher.

    val typeface = ResourcesCompat.getFont(context, R.font.myfont)
    
        
    answered by 25.10.2018 в 19:31