Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to custom-declare XML namespace in styles.xml?

I'm trying to put the custom XML namespace in the styles.xml and inherit it in the layout. I don't know how to declare the custom XML namespace in the styles.xml as I do in layout xml (e.g. xmlns:app="http://schemas.android.com/tools").

How do I use custom XML namespace in the styles.xml?

What I have:

  1. The font asset, ReallyCoolFont.ttf is saved in the asset/fonts.

  2. my_layout.xml:

    <TextView
        <!-- more attributes here -->
        app:customFont="fonts/ReallyCoolFont.ttf" 
        <!-- more attributes here -->
    </TextView>
    
  3. styles.xml:

    <style name="CoolTextView">
        <!-- more items here -->
        <!-- more items here -->
    </style>
    

What I'd like to have:

  1. my_layout.xml:

    <TextView
        <!-- more attributes here -->
        style="@style/CoolTextView
        <!-- more attributes here -->
    </TextView>
    
  2. styles.xml:

    <style name="CoolTextView">
        <!-- more items here -->
        <item name="app:customFont">ReallyCoolFont.ttf</item>
        <!-- more items here -->
    </style>
    

Error I get:

Error:(1403, 21) No resource found that matches the given name: attr     'app:customFont'.
like image 372
melvynkim Avatar asked Mar 30 '15 19:03

melvynkim


People also ask

Where is Styles XML in Android?

Defining Styles This XML file resides under res/values/ directory of your project and will have <resources> as the root node which is mandatory for the style file.

What is the correct way to declaring an XML namespace?

XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is namespace in Android XML?

An XML Namespace has the properties: Namespace URI: Namespace name expressed as a URI to which the prefix is bound. prefix: syntactically, this is the part of the attribute name following the XMLConstants. XMLNS_ATTRIBUTE ("xmlns") in the Namespace declaration.

How do I create a style folder in Android?

Right click on res folder, choose New --> Android resource file, set the same name for the new file "styles", in Available qualifiers: choose the last item "Version" and finally set "Platform API level" 21. Show activity on this post.


2 Answers

You need to define an attribute for your fonts in attr.xml file in res folder:

<attr name="myfonts" format="string" />

And you need to define custom style for your TextView and here we use our defined attribute(myfonts):

<declare-styleable name="MyCustomStyle">
    <attr name="myfonts" />
</declare-styleable>

Then styles can be declared:

<style name="CoolTextView">
    <item name="myfonts">ReallyCoolFont.ttf</item>
</style>

summary of what you have so far:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <attr name="myfonts" format="string">
    </attr>

    <declare-styleable name="MyCustomStyle">
        <attr name="myfonts" />
    </declare-styleable> 

    <style name="CoolTextView">
        <item name="myfonts">ReallyCoolFont.ttf</item>
    </style>

</resources> 

4)Now your layout would be:

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

    <com.example.MyCustomTextView
        android:id="@+id/result"
        style="@style/CoolTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="HELLO WORLD!"
        android:textSize="24dp"
        android:gravity="center" >
    </com.example.MyCustomTextView>

</RelativeLayout>

5)and your MyCustomTextView is:

public class MyCustomTextView extends TextView {
    

    private static final String TAG = "TextView";
    
    public MyCustomTextView(Context context) {
        super(context);
    }

    public MyCustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        settingFont(context, attrs);
    }

    public MyCustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        settingFont(context, attrs);
    }


    private void settingFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.MyCustomStyle);
        String customFont = a.getString(R.styleable.MyCustomStyle_myfonts);
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), customFont);  
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
            a.recycle();
            return;
        }

        setTypeface(tf);  
        a.recycle();
    }


}

I assumed you put the font in asset not in asset/fonts directory.

also I highly recommend read this.

like image 122
mmlooloo Avatar answered Oct 19 '22 19:10

mmlooloo


You don't need to add any prefix to reference your custom attributes in the style resource files. Doing it like this will work just fine:

<style name="CoolTextView">
    <item name="customFont">ReallyCoolFont.ttf</item>
</style>
like image 11
ivagarz Avatar answered Oct 19 '22 18:10

ivagarz