StyledAttributes from theme not properly loaded

308 views Asked by At

I added a custom attribute to a textview called font. Which automatically loads a custom font.

Example xml:

<?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.egeniq.widget.TextView
    android:layout_width="match_parent"
    android:layout_height="44dp"
    app:font="MyriadPro"
    android:text="@string/login_welcome" />
</RelativeLayout>

The custom widget does the following in its constructors:

    TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.TextView);
    String customFont = styledAttrs.getString(font);
    // snip. The error occurs here

The R.styleable.TextView is declared as such:

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

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

    <!-- TextView -->
    <declare-styleable name="TextView">
        <attr name="font" />
    </declare-styleable>

</resources>

When everything is defined as above it works. the variable String customFont is set to the proper name (MyriadPro).

When trying to auto-apply this value trough a style the customFont string is empty.

style is declared as such:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.BicycleBuddy" parent="android:Theme.Light.NoTitleBar">
        <item name="android:textViewStyle">@style/Widget.TextView</item>
    </style>

    <!-- Override defaults -->
    <style name="Widget.TextView" parent="@android:style/Widget.TextView">
        <item name="font">MyriadPro</item>
    </style>

</resources>

The theme is included in the manifest:

<application
    android:name=".Application"
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.BicycleBuddy" >
</application>

The <item name="font"></item> gives no errors. The custom widget is loaded and hits the breakpoint. However like said above. if used like this the 'customFont' property is null after reading it.

Screenshots of the stack: The first (where customFont is not null) is where the XML itself includes the app:font="" property. The second, is where it does not include this property, but where it should be loaded from the style

Custom font not null. XML includes <code>app:font=""</code> Custom font is null. XML does not include <code>app:font=""</code>

The reason the View element is different is because these are simply 2 different textviews. with the exact same propertys. only 1 includes app:font="" and the other does not. Breakpoints were hit 1 after the other in the order that I would expect.

As you see, virtually everything is equal. Even the ID behind styledAttrs is the same. Yet if it do not explicitly declare the font property in the XML it isn't loaded.

I hope someone directly sees what connection I am missing. but I do realise this is a rather vague issue.

Please note that com.egeniq.* is a custom library attached to my project. The styles.xml is declared in the 'main project', while attrs.xml is from the library project

0

There are 0 answers