File Templates.
Stop to write same code again and again.
For mobile application developer it is so common to have application feature page which is displaying some sort of list item and then it’s detail. No worry, Developement SDK provide us awesom in build view widgets, for an example List View, Table View. Just write a supported adapter and you will be almost done.
Well, now you have new applicaiton to earn some bucks & what a miracle here, this also has very similar feature to list down item. So what you can do. One approach would be write a complete ListAdapter again, well I’m skilled enough to write a bug free code. That’s awesome, but what if you are in your green day and you can earn more by doing some other work. Hmm..well Ctrl+C and Ctrl+V and that’s it, most of work done. Cool, google bless you !!
Well, you have something different which can help to build almost same same component in less than a minute. Really !! Yes, in general this feature known as File Template.
Name itself explain, A file template is a source file that already contains some code to get you started.
You build a template with require variable, say class name & it will merge with your predefined code and will generate code file for you.
Heyy, have you used something similar already !! 🤔 please tell me YES YES. Activity, Fragment, ViewController are very regular File Templates…we already have. So this time to build your own now.
Let’s checkout steps to create own File Template for Android developer using Android Studio.
Here we will build a custom view group by passing class name and ViewGroup name i.e. LinearLayout, FrameLayout.
- Move to Android Studio -> preference -> File and Code Template. OR select File -> New -> Edit File Templates -> Files.
- Select + to create new template. Give it a suitable name, file of extension i.g. java, kt.
- Add your template code. You can use variable to make it more generalise. Couple of we used here, PACKAGE_NAME, NAME, DATE. We can also create own custom variable like VIEWGROUP_NAME we create here.
- Just apply and OK and you done. Cool !!
- Time to use it. Select your package and select File -> New and there your custom view display and and you done. Wolla.
But wait, what is that #parse(“File Header.java”) ??
Well, this is just another template to defined author information.
File templates are powerful, for an example Adapter classes, Singleton (though Kotlin has made it far simple) or any other object pattern.
Try your own. #HappyCoding #HappyLearning 🙌