Android Tips: Code Generation with Live Template
For a long time I didn't know that Android Studio provides the feature to generate code.
![Generating Code](https://bloggie.io/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBakFCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--1bf271c13f0aedd0db2ec439b0194c89aef7e389/live_template.gif)
After knowing about this, I've been spamming only the logd
generation for Log.d(TAG, "message")
, but I never tried to configure my own Live Template.
Recently I've been trying to write an application from scratch, so whenever I tried to create a new Activity. I always have to create the following files:
- SomeActivity
- SomeViewModel
- SomeRepository
- SomeContract
And I realized most of their content are the same at the beginning. So as you can guess, it's a good fit for using Live Template!
Here's an example of generating the RessetPasswordViewModel
for the Password Resetting Page:
![Generating ViewModel](https://bloggie.io/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBakVCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--e5793819444a3ec2b1bfd515627a340f5eb19af7/generate_viewmodel.gif)
How to configure it
Here's a quick screenshot with 1,2,3,4 steps to do it:
![Live Template Quick Steps](https://bloggie.io/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBaklCIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--c74fe02da9f0d8d413e560cc1ccde73a20ccd331/Screen%20Shot%202018-12-06%20at%200.40.30.png)
Note that in step 3, you can key in any variable by surrounding it with dollar sign: $variable$
. Other stuff is very easy that I don't have to explain it.
Sharing Live Template
I also discover that you can actually share your Live Template, this way I can share the creation of Activity
, ViewModel
, Repository
, Contract
with my team. Anyway you can share anything with your team.
To export Live Template, simple do this:
- File
- Export Settings
- Choose Live Template
To Import, do the same, but opposite:
- File
- Import Settings
- Select the file that is being exported
That's all for the quick tip, see you next time!
![Tan Jun Rong avatar](https://res.cloudinary.com/bloggie/image/upload/c_fill,f_auto,h_200,q_auto,w_200/v1/bloggie_store/b3f8face08e331473e18366778766b1a.png)
Tan Jun Rong
Clap to support the author, help others find it, and make your opinion count.