Android uses XML to define the layout of its user interface elements. The structure and naming of XML elements are important in ensuring that the user interface is well-defined, easy to understand, and maintainable.
Here are some general naming conventions for Android XML files:
<WHAT>-<WHERE>-<DESCRIPTION>-<SIZE>
Indicate what the resource actually represents, often a standard Android view class. Limited options per resource type.
ex) activity, fragment, view, item, layout...
Describe where it logically belongs in the app. Resources used in multiple screens use all, all others use the custom part of the Android view subclass they are in.
ex) all, main, home, login, user...
Differentiate multiple elements in one screen.
ex) title, content, info, profile...
Either a precise size or size bucket. Optionally used for drawables and dimensions.
ex) 24dp, small...
<WHAT>_<WHERE>_<DESCRIPTION>.xml
ex) activity_main.xml, fragment_onboarding, item_product, fragment_product_search
<WHERE>_<DESCRIPTION> or all_<DESCRIPTION>
ex) main_title, user_namehint, login_titlehint, all_done, all_fail
<WHAT>_<WHERE>_<DESCRIPTION>_<SUFFIX>
<SUFFIX> : <DIRECTION>_<SHAPE>_<OUTLINE>_<STATE>_<COLOR>_<SIZE>
WHAT
: ic, bg, img, logo...
WHERE
: navi, tab, home, login...
DESCRIPTION
: check, like, btn, close...
SUFFIX
:
DIRECTION : left, right, up, down...
SHAPE : circle, square, roundsquare...
OUTLINE : outline, border...
STATE : on, off, activated, disabled, normal...
COLOR : white, black...
SIZE : 24dp, small...
ex) ic_login_btn_disabled.png, ic_heart_on.png, ic_check_white_small.png
<KIND>_<WHERE>
KIND
:
(most commonly used android view classes)
View Class | Abbrevation |
---|---|
CoordinatorLayout | cdl |
ConstraintLayout | csl |
LinearLayout | ll |
RelativeLayout | rl |
AppBarLayout | abl |
Button | btn |
EditText | et |
TextView | tv |
ProgressBar | pb |
Checkbox | chk |
RadioButton | rb |
ToggleButton | tb |
Spinner | spn |
Menu | mnu |
ListView | lv |
RecyclerView | rv |
GalleryView | gv |
ex) iv_profile, et_login_username, btn_submit, rv_product
<WHAT>_<WHERE>_<DESCRIPTION>_<SIZE>
WHAT
:
Prefix | Usage |
---|---|
width | width in dp |
height | height in dp |
size | if width == height |
margin | margin in dp |
padding | padding in dp |
elevation | elevation in dp |
keyline | absolute keyline measured from view edge in dp |
textsize | size of text in sp |
ex) height_toolbar, textsize_nomal