Android Activity/Fragment Class and Layout File Naming

Feb 27, 2019

Naming List and Item

Usually we have a QuoteListActivity which have a RecyclerView of quotes, and a QuoteActivity which allow us to show detail/edit a quote.

QuoteActivity
QuoteListActivity

or

QuoteActivity
QuoteEditActivity
QuoteViewActivity

ViewModel

If you are using MVVM, you shall have a ViewModel for each activity/fragment as well.

QuoteActivity
QuoteListActivity
QuoteListViewModel
QuoteViewModel

NOTE: I prefer putting activity/fragment and ViewModel within the same package/directory for easy reference.

To maintain ordering, you can rename the file (not the class) to the following. (only for Kotlin, not Java)

Quote_Activity
Quote_ViewModel
QuoteList_Activity
QuoteList_ViewModel

NOTE: Refer to Android File Naming To Maintain Sort Order.

Layout

By default, Android Studio shall create the following layout files.

activity_quote.xml
activity_quote_list.xml
content_quote.xml
content_quote_list.xml
fragment_profile.xml

I personally found seperation of activity_ and content_ not necessary (most of the time), and distinguishing activity_ and fragment_ unecessary. I prefer to quotelist.xml rather than quote_list.xml (underscore _ reserved for sub layout for an activity/fragment).

profile.xml
quote.xml
quoteedit.xml
quotelist.xml

If separating activity_ and content_ is prefered, the following could be used.

profile.xml
profile_content.xml

NOTE: I should not have a activity/dialog/fragment of the same name.

For a activity with RecyclerView, usually there are one or more additional layout files for the individual item. Or an activity might have a sub fragment.

profile.xml
profile_history.xml
quote.xml
quoteedit.xml
quotelist.xml
quotelist_item.xml

NOTE: quotelist_item.xml is an RecyclerView item of quotelist.xml. profile_history.xml is a sub layout of profile.xml.

NOTE: the motivation of the naming is to group relevant files together.

Using underscore (e.g. quote_edit) or camelCase (e.g. quoteEdit) will mess up the ordering.

profile.xml
profileBasic.xml
profile_basic.xml
profile_content.xml

NOTE: Notice profileBasic.xml and profile_basic.xml (another activity) is in between profile.xml (activity) and profile_content.xml, where profile.xml (activity) and profile_content.xml (content) should be next to each other.

Using all lowercase naming (e.g. profile, profiletwo, etc) will maintain ordering.

profile.xml
profile_content.xml
profilebasic.xml

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.