Android Studio Live Code Templates to Save Your Time Coding

<p>Android Studio (and any other IDE developed by JetBrains) provides multiple handy auto-complete shortcuts to fill the declarations, method calls, etc. Some of them are for the keywords in Kotlin / Android specific. Avoid repeating some actions just because you avoid using the context helper in IDE. IDE is here to help you to be more productive.</p> <h1>Built-in templates:</h1> <h2>Some pure Kotlin templates:</h2> <ul> <li><code>const</code>&nbsp;&mdash;&nbsp;<code>const val</code></li> <li><code>void, fun0, fun1, fun2</code>&mdash; created a function with the required number of parameters</li> <li><code>ifn</code>&nbsp;/&nbsp;<code>inn</code>&mdash;<code>&nbsp;if ( &hellip; == null)</code>&nbsp;/&nbsp;<code>if ( &hellip; != null)</code></li> <li><code>todo</code>&nbsp;&mdash;<code>&nbsp;// TODO:</code></li> <li><code>fixme</code>&nbsp;&mdash;&nbsp;<code>//&nbsp;<em>FIXME:</em></code></li> </ul> <h2>Some Android specific for Jetpack Compose:</h2> <ul> <li><code>comp</code>&nbsp;&mdash; creates compose function</li> <li><code>paddp</code>&nbsp;&mdash;&nbsp;<code>Modifier.padding(.dp)</code></li> <li><code>weight</code>&nbsp;&mdash;&nbsp;<code>Modifier.weight()</code></li> <li><code>prev</code>&nbsp;&mdash; preview compose function</li> <li><code>prevCol</code>&nbsp;&mdash; creates a collection of compose previews</li> </ul> <blockquote> <p>For surrounding the composable with&nbsp;<code>Container</code>,&nbsp;<code>Column</code>&nbsp;or&nbsp;<code>Row</code>, put pointer at composable, press the Alt+Enter, pick the &lsquo;Surround with Widget&rsquo;. You can set your own shortcut as you like. I set it up for Alt+i, but it is fully up to you.</p> </blockquote> <p>&nbsp;</p> <blockquote> <p>You can achieve something similar with&nbsp;<strong>ctrl + alt + t</strong>, which can even wrap your code into if-else, or try-catch.</p> </blockquote> <p>There are many more templates, but if you start writing code, they pop up automatically as they are usually obvious. If you begin writing class, or interface, for loop and many others, you will get the context helper to complete the keyword.</p> <p>However, there is much more space for improvement.</p> <h1>Custom templates</h1> <h2>How to add a new template</h2> <p>Programmers are lazy, and I am no exception. I see a lot of programmers trying to find the code snippet somewhere in their code, internet or at some hobby project. If it has a repeating pattern, spend a minute or two to create a template and never repeat writing that chunk of code from scratch every time.</p> <p>Open Android Studio and go for&nbsp;<em>File &rarr; Settings &rarr; Editor &rarr; Live Templates</em>. By clicking plus in the top right corner, you can add your group of templates or a template to an existing category.</p> <blockquote> <p>I encourage you to explore all the available templates, because you will find other useful templates other than I named above or you will get inspiration how to easen your work further.</p> </blockquote> <p>If you add a new Live Template, the context must be defined in the bottom left corner with a yellow warning sign. Feel free to create a template, not only for Kotlin. It can be Java, Dart, Gradle, or anything else.</p> <p>&nbsp;</p> <p>Example of the Live Template</p> <p><strong>The abbreviation</strong>&nbsp;is the text used in your code to invoke the template. It can be anything convenient and memorable for you. I like to keep it plain with the capital beginning letter due to Kotlin&rsquo;s most of the variables are written by camelCase.</p> <p><strong>Template text&nbsp;</strong>is your field to input the template. Custom inputs can be defined by&nbsp;<code>$&hellip;$</code>. If you name them the same, the first input will be shared across other instances. So, if you start to write first&nbsp;<code>$STATE$</code>&nbsp;as in the example above, it will be replicated across the whole template.</p> <p><a href="https://proandroiddev.com/android-studio-live-code-templates-to-save-your-time-coding-ff230495bf80">Visit Now</a></p>