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);
}
}
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ankita Singh
Ankita is an Android Application Developer. She has experience in developing android application by using Java and xml.