Android Studio provides you with file templates to help speed up your development. You can use the pre-defined templates or create your own.
- Right click on the package where you want to create your file
- Select
New
from the dropdown - Select the file template for the type of file you want to create
-
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
-
Click on
+
under theFiles
tab -
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
. -
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. -
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}() }
-
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
-
Click
OK
-
Start using your new file template
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.
- 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}")
}
- Create a file using the file template
- Enter the name of the variable when prompted by Android Studio
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")
}
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")
Explore child templates and live templates.