My Hexaville
Spead the Word

Android Smarter Getter/Setter Generators

Android Smarter Code Template

Designed by Freepik

How often do you create POJO’s? You start with some fields: private String mFirstName, mAdress, mLastName… All starting with m, because you follow Android code conventions, right? You created all those fields and as a pro Intellij Idea platform user you press Alt+Insert -> Getters And Setters, selects all fields and BOOM! What a mess! Half of methods is a mess, you need to change all those names. In this post we’ll fix it. Let’s get started!

Code Template

So what happens is for getters you get this stupid name like getmName. For setters you get setmName with paremeter of mName. That’s awful. What I found myself is manually fixing all this mess so it looks like this in the end

It’s not that time consuming to manually change, but rather annoying, especially for big number of fields. Makes you feel like a robot. Can we make it generate this way? Yep.


When you press Generate Getters/Setters there’s a customization button

Android Smarter Code Template

Over there you can see source code for this template. By the way there’s a default builder template for setters. It looks like Freemarker, but with different syntax, there’s no angle brackets. And it seems to not support else if statements and functions.


For getters we just need to change the method name by removing m letter.  Create a new template with this code

What starts with # is an expression, set method creates new variable. Each variable you need to start with $ to get value, horrible syntax. I just added three lines 15-17. If variable name starts with m, then we remove first letter.

Public Variable

We’re done, right? But what is we have a public variable without m prefix and starts with m? Like menu. This code will generate getenu method. But the thing is you won’t need a getter/setter for public fields, so we’re good with this code. Anyway, you can just change template to default.


Just create one more setter template with this code

What I added here is lines 2-4 and 9-11. And use a variable in line 12.

Easier Way

Actually there’s even easier way to do that. Go to File -> Settings -> Code Style -> Java. In Code Generation tab add m as name prefix for field.

Android Better Getter/Setter


That’s a short post, but I feel like it’s super useful for Android Studio guys. If you want to automate the rest of Android Studio stuff and feel like doing same boiler plate code all the time, then check this blog post.


Ok, thanks for reading! You can get gist here. Don’t forget to subscribe, follow me on G+, Twitter, Facebook and share with friends if you think they will benefit from it!

About the Author Ihor Klimov

Formerly an Android developer, lately picked up some Flutter. This blog is everything that I find exciting about Android and Flutter development. Stay tuned and hope to see you again!