1

I have been working on Android for several years and throughout I have been using underscore for naming ids in xml e.g. blocked_numbers_title :

<TextView
        android:id="@+id/blocked_numbers_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Blocked Numbers"
        android:gravity="center"
        android:textSize="40sp"
        android:textColor="@color/colorTextPrimary"
        android:textStyle="bold"
        tools:ignore="MissingConstraints" />

Since in kotlin we can directly access these ids like variables, they do not look good with underscore since they are not following camelCase. Will it be good practice to follow camelCase for naming ids in XML as well like blockedNumbersTitle or is there any better practice?

NeeK
  • 662
  • 1
  • 8
  • 21
  • Yes, you should use camel case while naming components always – Karan Mehta Jan 06 '20 at 06:53
  • Have you checked this one : https://stackoverflow.com/q/1832290/7666442 – AskNilesh Jan 06 '20 at 06:58
  • Don't think so, Android coding standards recommend to use snake case notation *(Underscore notations)* – Jeel Vankhede Jan 06 '20 at 06:58
  • thanks, I would appreciate if you can provide some official links. Even in all the official samples from google , they always use _underscore e.g. https://github.com/android/architecture-components-samples/blob/master/NavigationAdvancedSample/app/src/main/res/layout/fragment_user_profile.xml – NeeK Jan 06 '20 at 07:01

2 Answers2

0

You can use CamelCase or any other format for id naming convention as long as you are comfortable with it as it is a string variable.

Refer the doc for more info:

android kotlin official doc

Prajwal Waingankar
  • 2,534
  • 2
  • 13
  • 20
0

camelCase is an good choice, both for its good readability - you can differentiate the - and for its compactness. Regarding the readability, sometimes it's hard to recognize when in your variable name are more one-letter words: textToDoAWork. The AWork park looks a bit werid, but generally, I think it's the best choice

Encyklopedie
  • 111
  • 1
  • 6