Supercharge Your Workflow: A Step-by-Step Guide to Advanced File Templates

Mastering IntelliJ File Templates for Complex Android Views: A Guide to Multiple File Creation with MVP and Layouts

Simplify Your App Architecture: Quick MVP and Layout Creation Using a Customisable File Template

Marc Picone
by MWM

--

Introduction

Creating file templates in IntelliJ is a great way to save time and effort while developing Android applications.

In the first article of this series, we learned how to customise file and folder paths to our liking.

In this article, we’ll explore how to create custom file templates for MVP (Model-View-Presenter) architecture in an Android app.

We’ll use file templates to generate the required code and layouts automatically. In this article, we’ll take it a step further and create templates with multiple files and folders.

When building MVP architecture for an Android app, there are three primary components to consider: the View, the Presenter, and the Contract. Let’s create templates for each of these components.

Link

Articles:

Gists:

Template ZIP file:

1. The View.kt

To create the View file template, we’ll use the same approach as in the previous article, using a path with variables. We’ll simplify the process by passing the variable for the View name in the include tab.

First, we’ll create an include template named “NameToCamelCase” that will extract the CamelCase method to keep flexibility. Set the extension to kt and fill the body with ${StringUtils.removeAndHump(“${NAME}”, “_”)}.

Now, we can use this include template in the path to generate the name for the View. To do this, create a new template and name it “CustomView” in the files tab. The new path will be :

${NAME}/#parse("NameToCamelCase.kt")View

Remember to set the file extension to kt.

To start, declare the package name at the top of the file:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}.${NAME}
#end

Next, add the required imports:

import android.content.Context 
import android.util.AttributeSet
import android.view.View
import android.widget.FrameLayout
import androidx.annotation.IdRes
import ${PACKAGE_NAME}.R

Notice how we could easily get the R class and keep it available for every app.

Then we could add the class declaration using our parse directive.

class #parse("NameToCamelCase.kt")View @JvmOverloads constructor( 
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0 ) : FrameLayout(context, attrs, defStyleAttr){

private val view = inflate(context, R.layout.${NAME}, this)
}

2. The Presenter.kt

To create the Presenter file template, we’ll need to create a new child template. Fill the File Name field with :

${NAME}/#parse("NameToCamelCase.kt")Presenter

And set the extension to kt.

In the body, start by declaring the package name with:

#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}.${NAME}
#end

Next, add the class declaration using our NameToCamelCase.kt template:

class #parse("NameToCamelCase.kt")Presenter(
private val screen : #parse("NameToCamelCase.kt")Contract.Screen
) : #parse("NameToCamelCase.kt")Contract.UserAction {

}

Notice how easy it is to format all our file names with just one include template. This is a powerful feature that can save a lot of time and effort!

3. The Contract.kt

For the Contract, create a new child template with the following settings:

File Name:

${NAME}/#parse("NameToCamelCase.kt")Contract

Extension: kt
Body:

Contract Snippet

Note that this template follows the same structure as the Presenter and View templates, using the NameToCamelCase include template to format the file name.

4. The Layout.XML

To create a new XML file, we’ll create a child template in Android Studio. We’ll use the RootPath template that we created earlier to generate the file name.

We fill the File name with :

#parse("RootPath.kt")/res/layout/${NAME}

set extension to xml.

And finally the body :

XML snippet

This template will generate a new XML file with a merge tag and a UI Link comment. The file will be created in the appropriate layout folder based on the RootPath template.

Now you can easily create a custom view by using this template.

Conclusion

In conclusion, we have learned how to streamline our MVP architecture in Android by creating file templates for the Presenter, View, Contract, and XML files. By using templates, we can save time and reduce errors in our code.

While this article focused on the MVP architecture, the same principles can be applied to other architectures, such as MVVM or MVI. By creating file templates that match the architecture, we can quickly create new screens or features and ensure that they adhere to the chosen architecture.

By utilising file templates for the MVP pattern, we’ve learned how to standardise our code and improve productivity. But our template approach doesn’t stop there. In the next article, we’ll take it up a notch and dive into creating templates for Room database entities and DAO repositories.

--

--

Marc Picone
by MWM
Writer for

Hello! I'm Marc, Android developer at MWM. I've been creating innovative music, drawing, and creative apps with MWM since 2021. Let's connect and exchange ideas