In our day to day coding, we generally introduce bugs in code due to typos or by forgetting to call some function on an object.( How many of you have forgotten to call the show() function while writing the Toast code? ).To help reduce such issues and to increase productivity, Android Studio provides a nice little feature called as  Live Templates.

Live Templates

A live template is actually a bundle of shortcuts for common coding tasks using which developers can write usual codes without quickly and bug-free...


A common task is to write the Toast with a message and show it.


See, how easy it is! Just write Toast and press TAB key and then all you have to do is to write your desired message in empty quotes. You can write code by finding a particular id by just typing fbc and press TAB.    

Isn't that straightforward! Let's make a small application to find the factorial number and use templates to help us out.

Step 1:

Design a view as follows.



Step 2:

Now enter following Java code and see comments before writing each line of code.

public class MainActivity extends AppCompatActivity {
    TextView txtResult;
    Button btnSubmit;
    EditText edNum;
    private static final int a = 414;
    String TAG ="Template";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtResult =(TextView) findViewById(R.id.txtResult);
        btnSubmit=(Button) findViewById(R.id.btnSubmit);
        edNum =(EditText)findViewById(R.id.edNumber);

        //Just type fbc and TAB to print this quickly
        Toast.makeText(MainActivity.this, "Welcome to program !", Toast.LENGTH_SHORT).show();

        //Write Toast and press tab and you will get above result
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int result =displayFactorial(Integer.parseInt(edNum.getText().toString().trim()));
                txtResult.setText("SUM = "+result);
            }
        });
    }
    public int displayFactorial(int num){
        int sum =0;
        for (int i = 1; i <= num; i++) {
            sum*=i;
            Log.d(TAG, "displayFactorial: "+sum);
            // To write above code, write logd
        }
        // To write above code, write fori and press TAB and you will get readymade skeleton of for loop
        Log.d(TAG, "displayFacorial() returned: " + sum);
        // To write above code, write logr
        return sum;
    }
}

You can clearly see the difference in speed of coding when using shortcuts of comments.

logd TAB produces: 
Log.d(TAG, "onCreate:");
logr TAB produces:
Log.d(TAG, "onCreate() returned: " + );
fori TAB produces:
for (int i = 0; i < ; i++) {
   
}

Step 3:

To see a list of all available templates :

For Windows go to File -> Settings ->Editor -> Live templates

For OSX go to Android Studio -> Preferences -> Editor -> Live Templates


Try out Live Templates and it will help you in write code quickly while reducing bugs that can be introduced while not handling common programming constructs correctly.