Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortcut to generate "Created by.." template in Android Studio?

I have a project with a lot of classes in which I need to change the Created by template.

/**
 * Created by johnnyfivedev on 19.07.16.
 */

Since there are a lot of such classes, copy and paste not an option. Rather I want to remove that generated template and regenerate it. Is there any default shortcut for doing this? If not, then how do I create one?

like image 961
Johnny Five Avatar asked Dec 29 '17 08:12

Johnny Five


People also ask

How do I create a template on Android?

For creating new templates, right click on the source code folder and select “New”, then select “Edit File Templates”. Here you can see some predefined templates but we don't need this so click on “+” icon in top left corner. After this action your screen should look like this.

What is live template?

What is a Live Template? As mentioned towards the beginning of this tutorial, a live template is a tool in Android Studio and IntelliJ that allows you to insert frequently used bits of code into your file. You may have even run into some by accident before. You'll soon learn how customizable these can be!

Does Android studio have templates?

Android Studio provides code templates that follow the Android design and development best practices to get you on the right track to creating beautiful, functional apps. You can use templates to create new app modules, individual activities, or other specific Android project components.


1 Answers

Rather I want to remove that generated template and regenerate it.

You can surely edit the template but you can't regenerate it again. I suggest editing the template for your future uses and creating a live template for those made prior to this template change.

Editing the template

Go to Settings -> Editor -> File and Code Templates -> Includes -> File Header

enter image description here

and override the ${USER} function like so:

#set( $USER = "Your name")

enter image description here

If you want, you can add a lot more variables to it.

NOTE: These changes will only take effect on new files. The ones made prior to this template change will have to be manually changed.

Press Ctrl+Alt+S to go directly to the File and Code Templates tab.

Creating the live template

Go to Settings -> Editor -> Live Templates

Press the green 'plus' sign to add a new template, and select Live Template.

enter image description here

In the abbreviation field, type what you want to, I typed annot, add a suitable description (optional) and insert the following code in the box:

/**
* Created by $USER$ on $DATE$.
*/

$END$

Select the Edit variables box and write the following expressions in the corresponding fields,

  • user() for USER
  • date("dd-MM-yyyy") for DATE

After that type define applicable contexts as you see fit. In your code, now type annot and press Enter to insert the template.

In the code, your cursor will end up at the place of $END$ upon pressing Enter after template insertion.

like image 97
Nissim R Avatar answered Nov 03 '22 00:11

Nissim R