Live Templates

Posted By : Ankita Singh | 17-May-2016

If you have written much code you have made this mistake once in your coding life

Toast.makeText(MainActivity.this, "This will not be displayed");

Yes there is a way to guarantee to avoid these mistake?

Now you were wondering what this magic is.So let me tell you the magic of Live templates of Android Studios,Unless you're getting paid by the keystorke,no one wants to write repetitive boilerplate code.

you can see a live templates are nothing but a shortcut displayed as code completion option that, when selected ,inserted insert code snippet that you can tab through to specify any required argument.

For example as above shown : typing a "Toast" and hitting a tab key insert the whole code for the "Toast" with argument and a place holder before hitting tab and moving on to the next argument.

You can also create your own Live templates and also can see all the templates use by android studios

File >Setting > Editor > Live Templates

You can create your own live templates by navig,ating to File >Setting > Editor > Live Templates.Click on the android view and click on the plus button to add your own live templetes.

You'll want to choose an abbreviation to use the template,a description to remember what it does ,and of course the code you'd like it to insert-like this example for writing a boolean compose Email method.

 

public void composeEmail(String[] $addresses$, String $subject$) {
    android.content.Intent intent = new android.content.Intent(Intent.ACTION_SENDTO);
    intent.setData(Uri.parse("mailto:")); // only email apps should handle this
    intent.putExtra(android.content.Intent.EXTRA_EMAIL, addresses);
    intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

About Author

Author Image
Ankita Singh

Ankita is an Android Application Developer. She has experience in developing android application by using Java and xml.

Request for Proposal

Name is required

Comment is required

Sending message..