Creating Template Code in Android Studio

If you have been coding from long time, and don’t used this cool feature of Live code template then you wasted your time in writing some repeated code. Don’t worry this article is all about Live code template. So lets learn together .

Why Live Template ?

Many time a small mistakes happen when we do coding for example

//snippet of code

Toast.makeToast(YourActivity.this,getString(R.string.mystring),Toast.LengthLong);

So the above Toast will not be shown, Hence to avoid this kind of stuff which we generally use in our programming, We can utilize this concept.

So To avoid this kind of mistakes we can use Live Code Template.

What this Live Template all about ?

If you are users of IntelliJ you will be aware of this concept, Anyway this concept is similar to the code completion, or shortcut of the code like auto code completion do.

So we can also create our own Template instead of writing repetitive boilerplate.

Android Studio Live Template: Handy Reference

IntelliJ includes by default some of the Live Templates for us and Even we can make a large code for the Live template, Even we can make a class to be template.

In this way it will remove repetitive writing of codes .

What we are waiting for ? Lets Start

The steps of Creating Live Template are quite simple and easy to implement.

So initially it may take some time for you to create the template code but I bet you it will save your Tons of time in coming days.

So before starting the steps there is common thing which needs to be discussed is the naming and description of the code which we wants to make live template .

It is very important strategy because creating tons of template and searching it across autocomplete suggestions will puzzle you in future so I have some strategy for this it is again a HOT topic so not going in deep just follow some basic rule (its not universal you may implement your own because end of the day you have to implement).

Naming Strategies

Just think of WHY this template is necessary so that you can make decision to include it or not.

Ask yourself some few things

What this all about.

Where it is helpful.

Why and what is important Example

Toast.makeText(instance, R.string.app_name, Toast.LENGTH_LONG).show();

Whyà{PROMPT}

Whatà{Toast}

Whereà{screen}

Hence the preferred abbreviation will be PTS and name would be likepromp_toast_activity or anything you remember its just an organizing thing technique.

So now below are some steps which will help you to create a live template I am stating this with an example.

Steps

1. Write the code to the Android Studio editor .

2. Select and copy the as shown below .

3. Go to Menu bar and click on Insert live Template.

4. Go to the edit box and paste the code

5. Write description and abbreviation which you wants to give.

6. Now apply this code click ok.

7. That’s it you have done this.

Edit Steps.

You may follow the above steps for edit as well but there is short cut to go there command +J and then go to your abbreviated template name and just select it you will see a yellow marker at the right click it and a menu Edit live template setting

Change your setting accordingly.

Happy coding 😄

Don’t forget to subscribe us ,add your comments/suggestions anything is appreciated