Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:07:38+00:00 2026-06-14T18:07:38+00:00

Suppose I am making some new views with styleable attributes. I declare them thusly

  • 0

Suppose I am making some new views with styleable attributes. I declare them thusly (this is how the documentation says to do it:

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

    <declare-styleable name="TriangleView">
        <attr name="direction">
            <enum name="NE" value="0" />
            <enum name="NW" value="1" />
            <enum name="SW" value="2" />
            <enum name="SE" value="3" />
        </attr>
    </declare-styleable>

    <declare-styleable name="BannerView">
        <attr name="direction">
            <enum name="NE" value="0" />
            <enum name="NW" value="1" />
            <enum name="SW" value="2" />
            <enum name="SE" value="3" />
        </attr>
        <attr name="thickness" format="dimension" />
    </declare-styleable>
</resources>

However, this won’t work because all attributes are apparently in the same namespace, and I get the error Error: Attribute "direction" has already been defined.

So apparently I have to move the apparently duplicated attributes outside the <declare-styleable> like this:

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


    <attr name="direction">
        <enum name="NE" value="0" />
        <enum name="NW" value="1" />
        <enum name="SW" value="2" />
        <enum name="SE" value="3" />
    </attr>

    <declare-styleable name="BannerView">
        <attr name="thickness" format="dimension" />
    </declare-styleable>
</resources>

But this poses two questions:

  1. If this works, what exactly is the point of <declare-styleable>?
  2. What if I want the attribute to behave differently in different views? For example if BannerView‘s direction can only be up or down.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:07:39+00:00Added an answer on June 14, 2026 at 6:07 pm

    What exactly is the point of <declare-styleable>?

    <declare-stylable> tags let you declare attributes for your custom views that you can then set for those views in xml. There are really 3 parts to using the attribute:

    1. Declare an <attr> inside of a <declare-stylable> tag.
    2. Define a custom namespace in your xml layout pointing to your app package name (ex. app). Use the custom attribute in your layout (ex. app:direction="NW").
    3. In your custom view, override the constructors with an AttributeSet parameter, get a TypedArray and read the custom attributes, if any, from it and then within the constructor tell the view how to use those attributes appropriately.

    What if I want the attribute to behave differently in different views?
    For example if BannerView’s direction can only be up or down.

    Try something like this:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <attr name="direction">
            <enum name="NE" value="0" />
            <enum name="NW" value="1" />
            <enum name="SW" value="2" />
            <enum name="SE" value="3" />
        </attr>
    
        <declare-styleable name="TriangleView">
            <attr name="direction" />
        </declare-styleable>
    
        <declare-styleable name="BannerView">
            <attr name="direction" />
            <attr name="thickness" format="dimension" />
        </declare-styleable>
    </resources>
    

    When you build your xml layout for TriangleView or BannerView, you can use the app:direction="NW" example for both. In the constructors with AttributeSet in TriangleView or BannerView, the attributes will have the same format as the original, but what you do with that value is dependent on your implementation of the constructors in each respective view (can be the same or different for both).

    If you want attributes to be defined differenly (ie. different “format” or “enum”) for different views, then you have to create different attributes with different names.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To give some background, I am making a UINavigationControlled-based blog type app (I suppose
I'm new to rails and making some sort of noob mistake: I frequently need
This may be a philosophical question. Suppose you're making an AJAX request to a
Suppose I'm making a JDBC call and I had fetched data from a database
I m making an rmi client server based program which is suppose to pass
Suppose I have a supertype table with many subtype tables. I'm interested in making
Here is the question's plot: suppose I have some abstract classes for objects, let's
I am using Django and am making some long running processes that I am
Questions Updated instead of making a new question... I really want to provide a
Just wondering if anyone could help me with this. I'm new to actionscript, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.