Same-named attributes in attrs.xml for custom view
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Android custom views, attribute names defined in attrs.xml are not scoped only to one view in the way many developers first expect. The attribute itself is effectively global within the resource namespace, while declare-styleable groups decide which views reference it. That means same-named attributes should usually be declared once and then reused, not redefined separately with conflicting meanings.
Define the Attribute Once
A common pattern is to declare the attribute itself once, then include it in one or more declare-styleable blocks.
This means both custom views can read labelColor, and the attribute has one consistent definition.
Why Redefining the Same Name Causes Trouble
If you try to define the same attribute name again with a different format or meaning, resource merging becomes confusing and can fail.
Problematic pattern:
Even if the intent is "one for one view, another for another view," Android does not treat those as safely separate concepts. The name is the same, so the definition collides.
That is the key mental model: declare-styleable groups attributes, but the attribute resource name itself is shared.
Read the Attribute from a Custom View
Once the attribute is declared, the custom view can read it in the usual way.
The important part is that R.styleable.PrimaryBadgeView_labelColor refers to the shared attribute within that specific styleable group.
Reuse Names Only When Meaning Matches
Using the same attribute name across multiple views is a good idea when the meaning is genuinely the same. For example, labelColor meaning "text or label color" can be shared across several components.
It becomes a bad idea when the same name would mean different things in different views. In that case, choose distinct names instead of overloading one attribute concept.
Good reuse:
- '
labelColormeans the same visual role in several custom views'
Bad reuse:
- '
statemeans one thing in one widget and something unrelated in another'
Shared names work best when they represent shared semantics, not just a desire to keep names short.
Inheritance and Composition Make This More Important
The same-name question often appears when one custom view extends another or when several custom views are composed into a small design system. Reusing one attribute definition keeps the XML API consistent across related components.
That consistency is valuable, but only if the attribute contract stays stable. Once meanings diverge, separate names are clearer than clever reuse.
Keep Formats Consistent
If multiple views reference the same custom attribute, its format should remain consistent. Do not make one view expect a color and another expect a dimension for the same attribute name. That creates a confusing API and can break resource processing.
The attribute name, type, and meaning should all line up.
Common Pitfalls
- Declaring the same attribute name multiple times with conflicting formats.
- Assuming
declare-styleablecreates a private attribute namespace for each view. - Reusing one attribute name for different meanings just because the text label is convenient.
- Forgetting to call
recycle()on the obtained typed array. - Designing a custom XML API around short names instead of clear, stable semantics.
Summary
- In
attrs.xml, the attribute resource name is effectively global within the namespace. - Declare a same-named custom attribute once and reuse it across multiple
declare-styleableblocks when the meaning is the same. - Do not redefine one attribute name with conflicting formats or semantics.
- Read the shared attribute through the styleable group for the specific view.
- Reuse names for shared concepts, not for unrelated settings that only happen to need a label.
Related reading
- Same Navigation Drawer in different Activities
- Sandbox bash72986 deny1 file-write-data /Users/XXX/ios/Pods/resources-to-copy-XXXShareExtension.txt
- Save An Image To Application Documents Folder From UIView On IOS
- Save and Load from KeyChain Swift
- Save ArrayList to SharedPreferences
- Save custom objects into NSUserDefaults
- Save custom objects into NSUserDefaults
- Save images in NSUserDefaults?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.