Search code examples
javaandroidiosdateandroid-tools-namespace

Namespace declaration is never used: checks for unused namespace declarations and location hints in XML


  xmlns:tools="http://schemas.android.com/tools" 

this one is my problem it is said that "Namespace declaration is never used"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="josh appear here"
    android:id="@+id/josh_text_view"
    />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="open Second Activity"
    android:layout_below="@+id/josh_text_view"
    android:onClick="showJosh"
    android:id="@+id/button" />

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="open Third Activity"
    android:onClick="showJosh"
    android:id="@+id/button2"

and in every buttons there is this warning that always popping up

[I18N] Hardcoded string "open Second Activity", should use @string resource less... (Ctrl+F1) Hardcoding text attributes directly in layout files is bad for several reasons:

  • When creating configuration variations (for example for landscape or portrait)you have to repeat the actual text (and keep it up to date when making changes)

  • The application cannot be translated to other languages by just adding new translations for existing string resources. In Android Studio and Eclipse there are quickfixes to automatically extract this hardcoded string into a resource lookup.

            android:layout_below="@+id/button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="10dp" />
    
            <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="open Fourth Activity"
            android:onClick="showJosh"
            android:id="@+id/button3"
    
            android:layout_below="@+id/button"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="75dp" />
    
    </RelativeLayout>
    

Solution

  • Namespace declaration is not used is not an error. It's just a warning. You can delete that line if you want to but you can just leave it alone. Regarding the use of @string resource , it is considered best practice to define your string values inside the strings.xml file and reference them in your project. The strings.xml file is inside your res>values folder. For example: Inside your strings.xml file, there should be already some string resources present. Add the following line to the file

    <string name="openThird">open Third Activity</string>
    

    Now, inside your layout file, do this

    <Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/openThird"
    android:onClick="showJosh"
    android:id="@+id/button2"
    

    Do the same with your other buttons. Define the string values inside your strings.xml and then use them in your layout.