We are the Dev Teams of
  • brands
  • ebay_main
  • ebay
  • mobile
<
>
BLOG

File Templates on Android Studio

by Yasin Yildirim
in Tutorials

Eclipse new file wizard

It's been about a year since we switched to using Android Studio for our Android App development at eBay Kleinanzeigen. In the beginning it was a bit confusing and difficult to get used to all the new keyboard shortcuts. Many times I've accidentally hit the keyboard shortcut from Eclipse and executed some wrong command. But those days passed quickly, and after couple of weeks I started to learn all the good features of Android Studio.

There was still one big thing missing for me after using Android Studio for several months. When creating a new class, there's no option to select which superclass to extend or which interface to implement. After suffering for a while, I decided to find a solution for this, and IntelliJ documentation came to help.

 

In this article I will explain how to create a file template on Android Studio.

There are just three easy steps to create a file template.
  1. Go to Android Studio Preferences and select the File and Code Templates option under the Editor section.
  2. Select the Templates tab if not already selected. Here you will find a list of all the default pre-defined templates.
  3. Click on the small plus icon on top. This will create an empty template for you to modify for your needs. Give it a name and add any line you need for your file template.Android Studio file template editor

 

The new templates will be ready to use when you try to create a new class using the IDE's "New..." option.

New dialog

 
Here are my most used file templates for those who do not want to deal with the weird syntax:

 

Class extends SuperClass
The IDE will prompt with a dialog for the SuperClass name when you create a new file.

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

public class ${NAME} extends ${SUPERCLASS} {
}



Class implements Interface

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

public class ${NAME} implements ${SUPER_INTERFACE} {
}



Class extends SuperClass implements Interface

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

public class ${NAME} extends ${SUPERCLASS} implements ${INTERFACE} {
}



Interface extends SuperInterface

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

public interface ${NAME} extends ${SUPERINTERFACE} {
}



JUnit Test Class

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

import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ${NAME} {
}



JUnitTest extends SuperClass

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

import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class ${NAME} extends ${SUPERCLASS} {
}



Robolectric Test class

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

import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ${NAME} {
}



RobolectricTest extends SuperClass

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

import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ${NAME} extends ${SUPERCLASS} {
}

If you want to write your own templates, you can find IntelliJ's documentation about the File Templates and the syntax here.

android, android studio, intellij, file template

?>