Skip to content

Creating custom file templates in Android Studio πŸ“‚

Notifications You must be signed in to change notification settings

nimisaya/android-file-templates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

4 Commits
Β 
Β 
Β 
Β 

Repository files navigation

Android File Templates

Android Studio provides you with file templates to help speed up your development. You can use the pre-defined templates or create your own.

Getting Started

Use a file template

  1. Right click on the package where you want to create your file
  2. Select New from the dropdown
  3. Select the file template for the type of file you want to create

Create a file template

  1. Open File and Code Template menu

    This can be accessed through either:
    a. Preferences > File and Code Templates, or
    b. Right click on a package in Project tab > New > Edit file templates

    select default of project schema
  2. Click on + under the Files tab

    image
  3. Select Scheme
    Default: File template available for entire application (all projects). Use for personal templates.
    Project: File template for the current project. Use if you want it to be available to everyone working on project. Stored in .idea/fileTemplates.

    image
  4. Add a name, file extension and filename.
    If you want the filename to contain the name you can use the ${NAME} variable in the filename field.

  5. Add the code snippet you want included in the file template. Below is a sample code snippet for a Compose Screen that includes a preview

    #if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")
    package ${PACKAGE_NAME}
    #end
    
    import androidx.compose.material.Text
    import androidx.compose.runtime.Composable
    import androidx.compose.ui.tooling.preview.Preview
    
    @Composable
    fun ${NAME}() {
        Text("${NAME} Composable")
    }
    
    @Preview
    @Composable
    private fun ${NAME}Preview() {
        ${NAME}()
    }
    image
  6. If you want to use live templates or a set it so code generated based on the template conforms to a style in the file template then you can toggle those options to on

  7. Click OK

  8. Start using your new file template

Template Variables

To help with your file template creation you can use template variables. There are pre-defined ones or you can create your own. In the Compose template NAME is an example of a pre-defined variable.

Custom template variable

  1. Use the custom variable in your file template by surrounding it with ${} e.g. ${customParameter}
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")
package ${PACKAGE_NAME}
#end

import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun ${NAME}(${customParameter}: String) {
    Text("${customParameter}")
}

@Preview
@Composable
private fun ${NAME}Preview() {
    ${NAME}(${customParameter} = "${customParameter}")
}
  1. Create a file using the file template
  2. Enter the name of the variable when prompted by Android Studio

image

Generated code when customParameter = banana:

package com.nimisaya.filetemplates.ui.theme

import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview

@Composable
fun UseCustomVariable(banana: String) {
    Text(banana)
}

@Preview
@Composable
private fun UseCustomVariablePreview() {
    UseCustomVariable(banana = "Banana")
}

Directives

File templates use the Velocity Template Langauge (VTL). As well as using variables and plain text, you can also use directives such as #if, #set and #foreach.

println("Are you a Banana?")

#if ($NAME == "Banana")
println("Yes, hungry?") 
#else 
println("No, please don't eat me!")
#end

#set( $items = ["One", "Two", "Three"] )
#foreach( $item in $items)
println("$item")
#end

In this example if the file name is Banana the generated code will be:

println("Are you a Banana?")

println("Yes, hungry?") 

println("One")
println("Two")
println("Three")

Next steps

Explore child templates and live templates.

Resources

  1. Jetbrains File and Code Templates
  2. Templates with multiple files
  3. File template variables
  4. Velocity - VTL Guide (Template Directives)