kalehv
7/9/2017 - 7:35 PM

Naming conventions for Android

Naming conventions for Android

Java / Kotlin code naming conventions

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.

Logging tags should be named TAG and prefer static. No need to prefix TAG with Class.TAG.
private static final String TAG = <CurrentClass>.class.getSimpleName();

XML naming conventions

Strictly follow snake_case for XML names.

Avoid adding view in the name, unless a few exceptions discussed below.

Layout files

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.

XML id naming conventions

This table will be expanded as needed.

WidgetXML id prefixXML examleJava / Kotlin variable prefixJava / Kotlin example
TextView *text_text_empty_viewtexttextEmptyView
TextViewtext_text_firstnametexttextFirstname
EditTextedit_edit_firstnameediteditFirstname
Buttonbutton_button_okbuttonbuttonOk
FloatingActionButtonfab_fab_add_newfabfabAddNew
ImageButtonimgbutton_imgbutton_showimgbuttonimgButtonShow
ImageViewimage_image_user_avatarimageimageUserAvatar
RecyclerViewrecycler_<item>srecycler_todosrecyclerrecyclerTodos
CardViewcard_<item>card_todocardcardTodo
Layouts **layout_<name>layout_detaillayoutlayoutDetail
BottomNavigationViewbottom_nav_<name>bottom_nav_mainbottomNavbottomNavMain
ListViewlist_<item>slist_todoslistlistTodos
Menumenu_menu_mainmenumenuMain
MenuItemmenu_item_menu_item_settingsmenuItemmenuItemSettings
CheckBox ***check_check_donecheckcheckDone
RadioButton ***radio_radio_homeradioradioHome
Switch ***switch_switch_vibrateswitchswitchVibrate
Sliderslide_slide_color_toneslideslideColorTone
SnackBarsnack_snack_undosnacksnackUndo
Toasttoast_toast_messagetoasttoastMessage
Toolbartoolbar_toolbar_maintoolbartoolbarMain
ActionBaractionbar_actionbar_mainactionbaractionbarMain
NavigationViewnav_nav_mainnavnavMain

* 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.