0

I struggle with this every time I work on a project. I am not entirely sure what the standard conventions are for creating view ids. I usually use something like

view_more_button

but then if we have, let's say, multiple dialogs with a view more button then I'll start doing

view_more_button_dialog1

and from there it just spirals downward. Any suggestions?

1 Answer 1

0

I usually prefix resource IDs with the "component" they belong to. Note that I say resource IDs and not View IDs because this applies to other resources too (String for example).

Specifically for views, I further add the view type after the component prefix.

Here's a concrete example:

login_button_done (View ID)
login_ok (String resource)
login_forgot_password (String resource)
dialog_button_view_more (View ID)
dialog_label_warning (View ID)

One advantage of this approach is it makes it easier for IDE auto-complete. The downside is more keystrokes at the time of creating the views/String resources.

I have seen developers use a convention that puts the prefixes in the other order (i.e., viewtype_component_id). You should pick whatever suits your needs the best and just remember to stick to it throughout.

Not directly related to the question - but for some of my larger applications where the number of resources (especially strings) is very large, I also prefer to split up the strings into multiple files by component. For example, I'd have strings_login.xml, strings_settings.xml etc.

Specifically for dialogs, your view hierarchy would rarely (never ??) contain two dialogs simultaneously, so it is okay to re-use view IDs. In fact, I do sometimes create a few generic View IDs for this purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.