Custom Attributes not Working in a Custom theme

459 Views Asked by At

I have created a custom theme considering following custom attributes:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="GlideColour" format="color"/>
    <attr name="BackgroundColour" format="reference|color"/>
    <attr name="SuggestionColour" format="color"/>
    <attr name="KeyBackgroundColour" format="color"/>
    <attr name="KeyBorderColour" format="color"/>
    <attr name="SpaceBackgroundColour" format="color"/>
    <attr name="SpaceTextColour" format="color"/>
    <attr name="TextColour" format="color"/>
    <attr name="BackEnterColour" format="color"/>
    <attr name="PopUpDrawerColour" format="color"/>
    <attr name="DrawerSelectColour" format="color"/>
    <attr name="UtilityTextColour" format="color"/>
    <attr name="KeyboardBackground" format="reference"/>
    <attr name="KeyStyle" format="reference"/>
    <attr name="SpaceStyle" format="reference"/>
    <attr name="UtilityStyle" format="reference"/>
    <attr name="EnterStyle" format="reference"/>
    <attr name="BackStyle" format="reference"/>
    <attr name="KeyboardStyle" format="reference"/>
    <attr name="CapsStyle" format="reference"/>
    <attr name="DrawerButtonStyle" format="reference"/>
</resources>

Here's the theme I've made:

<style name="Standard" parent="Theme.AppCompat.Light.DarkActionBar">
         <item name="colorPrimary">@color/colorPrimary</item>
         <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
         <item name="colorAccent">@color/colorAccent</item>

         <item name="KeyboardBackground">@color/standard_background_color</item>
         <item name="GlideColour">@color/standard_glide_color</item>
         <item name="BackgroundColour">@color/standard_background_color</item>
         <item name="SuggestionColour">@color/standard_suggestion_color</item>
         <item name="SpaceBackgroundColour">@color/standard_space_background_color</item>
         <item name="SpaceTextColour">@color/standard_space_text_color</item>
         <item name="TextColour">@color/standard_text_color</item>
         <item name="BackEnterColour">@color/standard_back_space_color</item>
         <item name="UtilityTextColour">@color/standard_utility_color</item>
         <item name="KeyStyle">@style/standard_key_button</item>
         <item name="SpaceStyle">@style/standard_space_button</item>
         <item name="UtilityStyle">@style/standard_utility_style</item>
         <item name="EnterStyle">@style/standard_enter_style</item>
         <item name="BackStyle">@style/standard_back_style</item>
         <item name="KeyboardStyle">@style/standard_keyboard_style</item>
         <item name="CapsStyle">@style/standard_caps_style</item>
         <item name="DrawerButtonStyle">@style/standard_drawer_button_style</item>
     </style>

But when a apply a custom style attribute from this theme, it doesn't get added to the view, for example :

<com.nisarg.nboard.SwipeLayout android:layout_width="match_parent"
    android:layout_height="@dimen/keyboard_height"
    style="?KeyboardStyle"
    android:id="@+id/Rel"
    xmlns:android="http://schemas.android.com/apk/res/android">

Gives me this: enter image description here

Where it should give me this:

enter image description here

That is the referenced style is not being added to the element at all. I don't know why this is not working. What is the problem here?

Here is the style referenced

<style name="standard_keyboard_style" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="android:background">@color/standard_background_color</item>
    </style>

The problem i found out is in referencing, i can't use "attr/ or else it throws an error.

1

There are 1 best solutions below

2
Danish Ansari On

Try declaring your attributes as styleable

Like this:

<declare-styleable name="SwipeLayout">
    <attr name="GlideColour" format="color"/>
    <!-- other attributes go here-->
</declare-styleable>

Update

You can use same custom attribute for multiple views

Like this:

<resources>
    <attr name="GlideColour" format="color"/>
    <!-- other attributes go here-->
    
    <declare-styleable name="SwipeLayout">
        <attr name="GlideColour" />
    </declare-styleable>

    <declare-styleable name="SomeOtherView">
        <attr name="GlideColour" />
    </declare-styleable>
    
    <declare-styleable name="ThirdCustomView">
        <attr name="GlideColour" />
    </declare-styleable>

</resources>