android-best-practices icon indicating copy to clipboard operation
android-best-practices copied to clipboard

ID naming conventions

Open steelrooter opened this issue 10 years ago • 2 comments

Please include some best practices about naming conventions for android:id of components in XML layouts.

steelrooter avatar Aug 13 '15 07:08 steelrooter

I would suggest the following:

  • id should contain the prefix of the layout in which it exists. For example, inside the layout activity_auth.xml all ids of controls will start with activity_auth;
  • id should contain clear description of the control and what it does. Like connect_options, user_name etc.;
  • each id should have a suffix which defines the type of the control. For example, btn for the Button (like activity_auth_connect_btn).

Like in any framework some controls become deprecated, another controls are introduced. That's why there is a sense to define the following basic suffixes for the controls:

  • btn - button control (Button, RadioButton, ImageButton, CheckBox, custom button)
  • txt - TextView control
  • input - EditText or any input related control
  • list - for list control (ListView, GridView, RecyclerView)
  • selector / spinner - for Spinner control

I use such naming rules for a while and think they are quite convenient. They give an opportunity not to intersect ids between different layout. Also it's much easier to find related control by id inside the layout.

@peter-tackage What do you think?

dkhmelenko avatar Feb 28 '16 20:02 dkhmelenko

I'm only slightly late on this one. I've made another ticket to revise this #154 once I've gather more feedback internally.

peter-tackage avatar Jul 16 '17 15:07 peter-tackage