ameencarpenter
ameencarpenter

Reputation: 2609

Custom attribute not found for my custom view in android studio

I'm trying to declare custom views in android studio for my custom view. but android studio keeps showing me this error

AAPT: error: attribute bubbleSize (aka com.first.myapplication:bubbleSize) not found.

Here's my code:

My Custom view, just the declaration:

package com.first.myapplication

import android.content.Context
import android.view.View

class Bubble(context: Context): View(context) {

}

attr.xml file where i'm defining my custom attributes:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="Bubble">
        <attr name="bubbleSize" format="enum">
            <enum name="small" value="10"/>
            <enum name="big" value="25"/>
        </attr>
    </declare-styleable>
</resources>

activity_main layout where i'm using my custom view and custom attributes:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.first.myapplication.Bubble
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:bubbleSize="small"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 3

Views: 2072

Answers (1)

Shalu T D
Shalu T D

Reputation: 4039

Please move your attr.xml file from xml folder to the res->values folder and change the XML as below:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <declare-styleable name="Bubble">
        <attr name="bubbleSize" format="enum">
            <enum name="small" value="0"/>
            <enum name="big" value="1"/>
        </attr>
    </declare-styleable>
</resources>

Upvotes: 1

Related Questions