Android File Naming To Maintain Sort Order

Oct 28, 2018

Activity/Fragment Filename Naming

Lets assume I want to create an app about album, where I usually have an activity/fragment to list all the albums, and also an activity/fragment to show/view an album in full detail.

Show album detail.

AlbumFragment
AlbumViewModel

List all albums.

AlbumListActivity
AlbumListViewModel

These files will be sorted in the following manner in Android Studio's project view.

AlbumFragment
AlbumListActivity
AlbumListViewModel
AlbumViewModel

I would suggest renaming the file to the following to maintain proper sort order and grouping.

Album_Fragment
Album_ViewModel
AlbumList_Activity
AlbumList_ViewModel

NOTE: If you are using Kotlin, the class name doesn't have to match filename, thus you can rename the file as Album_Fragment.kt while maintaining the class name as class AlbumFragment.

Layout

By default Android Studio wizard create an activity with the following layout.

activity_hello.xml
activity_test.xml
activity_test2.xml
content_hello.xml
content_test.xml
content_test2.xml

I prefer to name the layout file as such to group related layout together/next to each other.

hello_activity.xml
hello_content.xml
test_activity.xml
test_content.xml
test2_activity.xml
test2_content.xml

The following naming mess up sort order as well

album_activity.xml
album_cat_activity.xml
album_cat_content.xml
album_content.xml

Change to the following to maintain sort oder.

album_activity.xml
album_content.xml
albumcat_activity.xml
albumcat_content.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.