Home » Android » Android custom attributes not showing

Android custom attributes not showing

Posted by: admin June 15, 2020 Leave a comment


I’ve seen a lot of posts with people wanting to know how to get custom attributes for a custom component but that is not my question. I have created a custom component and I’m trying to add attributes but when I add the namespace at the top of my xml file it only finds two random custom attributes “paddingEnd” and “paddingStart”.

    <declare-styleable name="menu_item_attrs">
        <attr name="imageId" format="integer" />
        <attr name="menuText" format="string" />

This is attrs.xml file.

public MenuListItem(Context context, AttributeSet set) {
    super(context, set);

    TypedArray a = context.obtainStyledAttributes(set, R.styleable.menu_item_attrs);
    if (a == null) {
    CharSequence s = a.getString(R.styleable.menu_item_attrs_menuText);
    if (s != null) {
        // do something

This is the constructor in my custom class.



This is my component in use. I want to add values to “imageId” and “menuText” but they aren’t available. The only things that show are padding related as shown below.

enter image description here

Any ideas guys?

How to&Answers:

Found the solution.

As it happens you need to have the styleable name the same as your class name.

    <declare-styleable name="MenuListItem">
        <attr name="my_custom_attribute" format="integer" />

Many many posts on this subject are wrong in that case as I can see plenty with the styles name being completely different.

Hopefully this can stop someone else falling into this trap.



My solution was:
In the attr.xml file, you may have declared your custom attribute as follows

<declare-styleable name="IDEditText">
<attr name="customFont" format="string"/>

Here, the name field of declare-styleable must be the name of the custom view. Then it will show in the suggestions.

In mycase my custom view name is IDEditText, hence the declare-styleable name=”IDEditText”