Search code examples
androidandroid-studionaming-conventionsstandards

Best practices for ID naming conventions in Android?


I saw this link first but it didn't seem to have much activity: Naming convention for IDs in Android

I am curious what the best practice is for naming IDs for the various items in Design View on Android Studio.

Right now I'm doing stuff like this: If it's an TextView with the text "Welcome To My Program", I'll name it welcomeTextViewID. If it's a Button that starts some subroutine called doStuff, I might call it doStuffButtonID.

In other words I use a sort of descriptor + datatype + ID convention.

Is this considered bad practice? I always heard mixed things on using descriptors inside the name. For example in a language like C++ naming a string variable nameString (since if you change the data type later you have to also update the name).


Solution

  • Checkout --> https://github.com/umesh0492/android-guidelines

    Further ID naming

    IDs should be prefixed with the name of the element in lowercase underscore. For example:

    +---------------------+
    | Element   | Prefix  |
    |-----------+---------+
    | TextView  | text_   |
    | ImageView | image_  |
    | Button    | button_ |
    | Menu      | menu_   |
    +-----------+---------+
    

    view example:

    <ImageView
        android:id="@+id/image_profile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    Menu example:

    <menu>
        <item
            android:id="@+id/menu_done"
            android:title="Done" />
    

    The best guidelines I have ever seen and I do follow them.