OHM
OHM

Reputation: 47

Android Studio saving custom code templates

I couldn't find where exaclty in the android studio you can save custom codes for example if i want to save: Toast.makeText(getApplicationContext(), "Message.",Toast.LENGTH_SHORT).show(); If i wanted to save the code and make a shortcut for it for example toastMT how do i do that exacly?

Upvotes: 3

Views: 640

Answers (2)

LordWabbit
LordWabbit

Reputation: 307

Select the code you want to create a live template from.
Click Code on the menu - and then Save As Live Template.

The live template editor window will open up and you can give the template a shortcut name, and that will give you a basic live template from your selected code.

You can also put placeholders in to rename certain sections of the template (variable names etc) by replacing those values in the template with $NAME$ where name is whatever name you deem appropriate. You can also put an $END$ so when you are done editing and press enter the cursor will move to that position. The other reserved name is $SELECTION$ which is reserved for templates that surround selected code.
Here is the jetbrains entry although to be honest I only use 1% of the functionality available in live templates.

Upvotes: 0

Christopher
Christopher

Reputation: 10259

  1. Goto Preferences
  2. Select Editor on the left pane
  3. Select Live Templates on the left pane

Here you can find a list of predefined templates and you can add your custom ones. If you want to use your template, simply start typing, e.g. toastMT and Android Studio will provide a suggestion to use your template.

Upvotes: 4

Related Questions