How to add code snippets in Android Studio IDE?

AndroidAndroid Studio

Android Problem Overview


does anyone know how to add code snippets in Android Studio IDE? I didn't found a menu entry or something like this.

I also want to know is it possible to change the existing snippets?

Android Solutions


Solution 1 - Android

To add/edit templates:

On the menu choose File -> Settings, then under IDE Settings (or Editor) go to Live Templates.

To insert a template:

On the menu choose Code -> Insert Live Template (or use it's assigned shortcut key).

Screenshot Android Studio 3.4 Add Live Templete Android Studio 3.4

Solution 2 - Android

Code snippets are known as Live Templates in Android Studio

#Browse the current Live Templates

Go to File > Settings > Editor > Live Templates. Then click on the different options to see what they do.

enter image description here

Here are some interesting ones:

  • foreach

      for ($i$ : $data$) {
          $cursor$
      }
    
  • Toast

      android.widget.Toast.makeText($className$.this, "$text$", Toast.LENGTH_SHORT).show();
    
  • todo

      // TODO: $date$ $todo$ 
    
  • logi

      android.util.Log.i(TAG, "$METHOD_NAME$: $content$");
    

The words surrounded by $ signs are places where things will be filled in automatically from the context or where the user can tab through to fill them in.

#Use a Live Template

Just type in the abbreviation and tab through anything that you need to fill in.

Example:

logi

and I'm given this

Log.i(TAG, "myMethodName: |");

with the cursor located at the position I marked with the vertical line |.

#Make you own Live Template

Just write the code, highlight it, and go to Tools > Save as Live Template.

#Further Study

Solution 3 - Android

Go to Settings (File > Settings, or CTRLALT + S) then search for "Live Templates".

A template screen will be opened. You can see here all live templates and you can also create your own custom live templates.

It is important that you choose the correct template group to define your new template (e.g. "Android").

As an example, I will define a Toast method as a live template.

You can also click "use static import as possible" not to be handle importing stuff.

enter image description here enter image description here

Solution 4 - Android

Cntr + j : to insert an existant live template. File -> Settings -> Editor -> Code style -> live Templates >> add a template with button + edit your template and OK :)

Solution 5 - Android

go to Settings (Files > Settings (ctrl + alt + s) > then click Editor > Live Templates )

like this:

enter image description here

then type your abbreviation name and use your template any where

enter image description here

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStephanView Question on Stackoverflow
Solution 1 - Androidfree3domView Answer on Stackoverflow
Solution 2 - AndroidSuragchView Answer on Stackoverflow
Solution 3 - AndroidhuseyinView Answer on Stackoverflow
Solution 4 - AndroidSamir ThebtiView Answer on Stackoverflow
Solution 5 - Androidamir khanView Answer on Stackoverflow