1

Hi I am learning how to create libraries for android application. I am starting with custom textview and set custom font on it.

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

Here I want to take fontFamily value in this format @font/font_name. Developers need to create font folder in the res folder and put the fonts there. What do I need to do for this.? Thanks in advance.

I have created custom attribute using below approach.

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="Typekitable">
        <attr name="fontFamily" format="string" />
    </declare-styleable>
</resources>
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • You can't create your own resource types, like `font`. You could, though, have the font files in `assets/`, and keep the attribute as a `string`, or possibly have them in `raw/`, and make the attribute an `integer`. – Mike M. Mar 25 '17 at 06:59
  • @MikeM. Yup I am agreed we can not. I am not doing this in applications. I am creating an library and want to allow developers this methods to set custom font on `textview` in android. – N Sharma Mar 25 '17 at 07:09
  • 1
    You cannot put any folder under `res/` that's not for a defined resource type. – Mike M. Mar 25 '17 at 07:15
  • @MikeM. Google introduced this recently same kinda https://developer.android.com/preview/features/working-with-fonts.html how they are doing – N Sharma Mar 25 '17 at 07:17
  • Ah, nifty. I hadn't seen that yet. Anyhoo, how they did that is, they defined a new resource type that's now apparently available starting with Android O. They build the framework, so, ya know, they can add anything they want. You, though, can't define your own resource types, so a `font` resource isn't possible before O. In O, I'd imagine you could create a custom attribute with a `font` type just like you would any other attribute. – Mike M. Mar 25 '17 at 07:23
  • Hey, just to keep ya updated, it looks like the [support library, as of 26.0.0](https://developer.android.com/topic/libraries/support-library/revisions.html), will allow you to use font resources on prior versions through AppCompat. I shoulda thought of that. Anyhoo, just an FYI. – Mike M. May 20 '17 at 04:03

1 Answers1

0

You can use this :

1- create enum like this :

<attr name="fontFamily" format="enum" >
    <enum name="tahoma" value="1"/>
</attr>

2-

<declare-styleable name="MyTextView">
    <attr name="fontFamily"/>
</declare-styleable>

3 -

public class MyTextView extends TextView {


    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyTextView, defStyle, 0);
        int fontOrdinal = a.getInt(R.styleable.MyTextView_fontFamily, 0);
        //....
        a.recycle();

    }

}

when use :

<MyTextView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:fontFamily="tahoma"/> // you can use ctrl+space

By default, you can not create folder custom in Res. To do this, refer to the links below :

With two res directories per build variant, Gradle stopped tracking changes in resource files

pic

Community
  • 1
  • 1
Ahmad Aghazadeh
  • 16,571
  • 12
  • 101
  • 98
  • Ahmad, I think it is a bit different than what I am looking for. Anyways let me rephrase my words. I am creating an android library through which I want to allow developers to create `font` directory in the `res` directory and keep TTF files there. Then Developers can use in this way "@font/lobster" -- here `font` is the directory and `lobster` is the TTF file which is in `font` directory. – N Sharma Mar 25 '17 at 07:08