No resource identifier found for attribute in my Custom View

453 Views Asked by At

If I keep and use only the attributes that start with 'android:',everything is working well. When I add the custom name for an attribute (in this case is <attr name="detailsBackground"/>) nothing in the view works anymore. The following things happen:

  • all the R's in the class are red
  • after I try to build I get this message:

Error:(10) No resource identifier found for attribute 'detailsBackground' in package 'com.company.package'

This is what I have in attrs.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CurrentMonthPanels">
        <attr name="android:background"/>
        <attr name="android:src"/>
        <attr name="android:text"/>
        <attr name="detailsBackground"/>
    </declare-styleable>
</resources>

This is my custom layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:id="@+id/mainBackground"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:background="@drawable/full_panel_red"
        android:padding="10dp">

        <ImageView
            android:id="@+id/iconImage"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:src="@mipmap/expenses_icon" />

        <LinearLayout
            android:layout_width="350dp"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:gravity="end"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/my_white"
                android:text="160000"
                android:textSize="50sp"
                android:gravity="end"/>

            <TextView
                android:id="@+id/textDescription"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="end"
                android:textColor="@color/my_white"
                android:textSize="20sp"
                android:text="Expenses this month (USD)" />

        </LinearLayout>

    </RelativeLayout>
    <RelativeLayout
        android:id="@+id/detailsLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/full_panel_red_transparent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_margin="5dp"
            android:text="@string/quick_details"
            android:textColor="@color/my_red"
            android:textSize="17dp"
            android:gravity="center"
            android:padding="10dp"/>

        <ImageView
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@mipmap/arrow_right"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:tint="@color/my_red"/>

    </RelativeLayout>

</LinearLayout>

And this is my CustomMonthPanels class

public class CurrentMonthPanels extends LinearLayout {

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public CurrentMonthPanels(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, 
                       R.styleable.CurrentMonthPanels);

        Drawable iconToShow = 
           a.getDrawable(R.styleable.CurrentMonthPanels_android_src);
        Drawable mainBackground = 
           a.getDrawable(R.styleable.CurrentMonthPanels_android_background);
        Drawable detailsBackground = 
           a.getDrawable(R.styleable.CurrentMonthPanels_detailsBackground);
        String textDescription = 
           a.getString(R.styleable.CurrentMonthPanels_android_text);
        a.recycle();

        inflate(context, R.layout.current_month_panel, this);

        RelativeLayout mainLayout = (RelativeLayout) 
                              findViewById(R.id.mainBackground);
        mainLayout.setBackground(mainBackground);

        ImageView iconImage = (ImageView) findViewById(R.id.iconImage);
        iconImage.setImageDrawable(iconToShow);

        TextView textDescriptionView = (TextView) 
                              findViewById(R.id.textDescription);
        textDescriptionView.setText(textDescription);

        RelativeLayout detailsLayout = (RelativeLayout) 
                              findViewById(R.id.detailsLayout);
        detailsLayout.setBackground(detailsBackground);
    }

}

And this is the layout where I am trying to display the view with custom attributes

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:namespace="http://schemas.android.com/apk/res-auto"
    tools:context="com.company.package.CurrentMonthPanels"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <com.company.package.CurrentMonthPanels
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/full_panel_red"
        android:src="@mipmap/ic_launcher"
        android:text="Dasaa"
        namespace:detailsBackground="@drawable/full_panel_red_transparent" 
/>

</RelativeLayout>
0

There are 0 best solutions below