android-layoutandroid-studioandroid-custom-attributes

Unexpected namespace prefix “custom” found for tag fragment while trying to use custom attributes


I am following this android developer page for the creation of custom attributes for an app. Everything is going fine and I am able to compile and see the results also. But the part which is giving issue is IDE.Android Studio is complaining for unexpected namespace custom. Is there any way to disable/hide this error message? This is very annoying.

Even with this error, however, I am able to compile and run the app.

enter image description here

I followed follwing steps.

1) Created res/values/attrs.xml file and added

<?xml version="1.0" encoding="utf-8"?> <resources>
<declare-styleable name="FragArguments">
    <attr name="max_allowed" format="integer"/>
    <attr name="header" format="string"/>
</declare-styleable>

2) Trying to use in my mainlayout file's fragment tag

    <LinearLayout xmlns:custom="http://schemas.android.com/apk/res"
                          android:layout_width="fill_parent"
                          android:layout_height="0dip"
                          android:layout_weight="1">
                <fragment android:id="@+id/msg"
                          android:name="org.android.fragment.MessageFragment"
                          custom:max_allowed="85"
                          custom:header="@string/header"
                          android:layout_width="wrap_content"
                          android:layout_height="match_parent"/>
            </LinearLayout>

3. Applying the custom attributes through code overriding the onInflate() method of fragment

   @Override
    public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState) {
        super.onInflate(activity, attrs, savedInstanceState);

        TypedArray typedArray = activity.obtainStyledAttributes(attrs, R.styleable.FragArguments);
        max_allowed = typedArray.getInt(R.styleable.FragArguments_max_allowed, -1);
        header = typedArray.getString(R.styleable.FragArguments_header);
        typedArray.recycle();
    }

Solution

  • I just figured out the solution.

    The issue ID MissingPrefix tells lint to only scan for XML attributes that are missing the Android namespace prefix.So we can do this two ways:--

    1. Canissue the following command to scan the files under the main project directory and its subdirectories.

    lint --check MissingPrefix myproject

    1. In android studio create a lint.xml file with following content and add in app directory.
    <?xml version="1.0" encoding="utf-8"?>
    <lint>
        <issue id="MissingPrefix" severity="ignore"/>
    </lint>