Help with a custom View attributes inside a Android Library Project

I have a custom PieTimer View in an Android Library Project


public class PieTimerView extends View {

I also have a XML attributes file which I use in my PieTimer

  • How to get the coarse location using Wifi or GSM or GPS, whichever is available?
  • Why did RecognitionListener stop working in JellyBean?
  • How do I specify different layouts for portrait and landscape orientations?
  • PhoneGap + Google Maps API v3 : Not displaying (at all) on Android
  • Error while importing android project (“Gradle location is incorrect” or “Cause: unexpected end of block data”)
  • Unable to open debugger port : “Socket closed”
  •     TypedArray a = context.obtainStyledAttributes(attrs,

    The XML styleable file looks like this

    <?xml version="1.0" encoding="utf-8"?>
         <declare-styleable name="PieTimerView">
            <attr name="max_size" format="dimension" />
            <attr name="start_angle" format="string" />
            <attr name="start_arc" format="string" />
            <attr name="ok_color" format="color" />
            <attr name="warning_color" format="color" />
            <attr name="critical_color" format="color" />
            <attr name="background_color" format="color" />

    I have the PieTimer in a layout file for a project that uses the library, like this

        android:layout_height="fill_parent" xmlns:android=""
        android:id="@+id/root" android:layout_gravity="center_horizontal"
        android:layout_height="wrap_content" android:id="@+id/clock"

    Previously I was using the xmlns:app as the namespace for attributes like app:ok_color but when I made my project a library project and has a Free and Full version implementing the library, I found it no longer worked so I added the pie namespace.

    The PieTimerView displays but the attributed do not work, they use the default (this was not occurring before) so there is some namespace conflict hapenning here.

    What is the correct way of doing this?

  • Android - Storing/retrieving strings with shared preferences
  • Update source activity before exit transition
  • Android CollapsingToolbarLayout with custom View
  • Is it possible to get the HTML code from WebView
  • MQTT vs. XMPP Which Should I Choose?
  • Facebook - “Cannot query users by their username” solution
  • 6 Solutions collect form web for “Help with a custom View attributes inside a Android Library Project”

    I know this post is super old, but if anyone comes looking, this problem has been fixed in ADT revision 17+ . Any services or Activities declare the namespace as:


    res-auto will be replaced at build time with the actual project package, so make sure you set up your attribute names to avoid collisions if at all possible.


    After two hours of work with my coworker, we figure out how to make this work :
    Library : com.library with a class MyWidget

    attrs.xml :

    <declare-styleable name="MyWidget">

    and put your attr

    Instead of having your layout (main.xml for example) with your widget declared in it, juste create a my_widget.xml in the layout foler of your library.

    <?xml version="1.0" encoding="utf-8"?>
        android:layout_width="fill_parent" android:layout_height="fill_parent"

    for including it in your layout, juste put

    <include layout="@layout/my_widget"></include>

    App : com.myapp

    You juste have to duplicate my_widget.xml here, and change the namespace :

    <?xml version="1.0" encoding="utf-8"?>
        xmlns:widget="" <---- VERY IMPORTANT
        android:layout_width="fill_parent" android:layout_height="fill_parent"

    And normally it will work !

    There is a known bug and its described here: bug #9656. I have also described why this happens in my blog post.

    For all APIs this is the receipt ( if the library is linked as JAR , but should work also as android library project reference)

    Library project:

    <manifest xmlns:android=""

    Library project:

    <?xml version="1.0" encoding="utf-8"?> 
    <declare-styleable name="MyCustStyle" >
          <attr name="MyAttr1" format="dimension" />  

    App project
    MainLayout.xml (or the layout where u use the custom control)

    <RelativeLayout xmlns:android=""

    In App Code if you want to access resources from library use (ex. array “MyCustomArray” defined in the arrays.xml fromthe lib) use:


    maybe the package( you specified is a wrong place. you can check the package attribute of the minifest element in the AndroidMinifest.xml file. they should be identical.

    This solution worked for me:

    It doesn’t use the TypedArray and requires the view to have the library’s project package name hardcoded in it.

    It also does not use the styleable values on the programmatic level. You have to hardcode the attribute strings into the view as well.

    You should however declare those same attribute names styleable for Lint to not show an error when you write the XML part.

    A link to the original post on

    It definitely beats copying the files and changing the namespaces but it still isn’t as elegant as should be. Enjoy.

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.