Naming conventions for Android
Strictly follow camelCase
for Java/Kotlin names.
I prefer simple readability and fluidity of code. Prefixing variables with m
or s
breaks that fluidity.
I prefer not prefix my variables with m
or s
. If you really want to differentiate between parameters and local variables, prefer prefixing them with this.
instead of m
and Class.
instead of s
.
TAG
and prefer static
. No need to prefix TAG
with Class.TAG
.private static final String TAG = <CurrentClass>.class.getSimpleName();
Strictly follow snake_case
for XML names.
Avoid adding view
in the name, unless a few exceptions discussed below.
Activity layout files start with activity_
e.g. activity_main.xml
Fragment layout files start with fragment_
e.g. fragment_main.xml
Content placeholder files start with content_
e.g. content_main.xml
Item layout files start with item_
e.g. item_todo.xml
. This item could be used in ListView
or RecyclerView
.
id
naming conventionsThis table will be expanded as needed.
Widget | XML id prefix | XML examle | Java / Kotlin variable prefix | Java / Kotlin example |
---|---|---|---|---|
TextView * | text_ | text_empty_view | text | textEmptyView |
TextView | text_ | text_firstname | text | textFirstname |
EditText | edit_ | edit_firstname | edit | editFirstname |
Button | button_ | button_ok | button | buttonOk |
FloatingActionButton | fab_ | fab_add_new | fab | fabAddNew |
ImageButton | imgbutton_ | imgbutton_show | imgbutton | imgButtonShow |
ImageView | image_ | image_user_avatar | image | imageUserAvatar |
RecyclerView | recycler_<item>s | recycler_todos | recycler | recyclerTodos |
CardView | card_<item> | card_todo | card | cardTodo |
Layouts ** | layout_<name> | layout_detail | layout | layoutDetail |
BottomNavigationView | bottom_nav_<name> | bottom_nav_main | bottomNav | bottomNavMain |
ListView | list_<item>s | list_todos | list | listTodos |
Menu | menu_ | menu_main | menu | menuMain |
MenuItem | menu_item_ | menu_item_settings | menuItem | menuItemSettings |
CheckBox *** | check_ | check_done | check | checkDone |
RadioButton *** | radio_ | radio_home | radio | radioHome |
Switch *** | switch_ | switch_vibrate | switch | switchVibrate |
Slider | slide_ | slide_color_tone | slide | slideColorTone |
SnackBar | snack_ | snack_undo | snack | snackUndo |
Toast | toast_ | toast_message | toast | toastMessage |
Toolbar | toolbar_ | toolbar_main | toolbar | toolbarMain |
ActionBar | actionbar_ | actionbar_main | actionbar | actionbarMain |
NavigationView | nav_ | nav_main | nav | navMain |
* This is an exception for empty view. Usually do not include the word view
in the name of the view.
** Only have id
for layout if they are referenced in code. Never include type of layout in name e.g. do NOT use layout_linear_detail
or layout_constraint_detail
, just include the name with layout_
prefix. This keeps flexibility to change the type of layout without needing to refactor code.
*** Never include the state of widget in the name e.g. do NOT use check_power_on
or switch_vibrate_off
, just include the name with widget specific prefix.
If using Kotlin Android Extensions, the name of the widgets in code will be same as their names from XML is snake case. It is okay to keep them snake case in Kotlin code in that case.