Android Studio: generate getters and setters below the field - android-studio

These are my two variables in Android Studio:
I generate the getters and setters by setting my cursor on the field called "distance" and then CMD - N:
This is the result:
I don't understand why the field is always at the bottom and the getters and setters are at the top. I always have to manually switch them around so it looks like this:
This seems to happen in IntelliJ as well.
Is there no way to make getters and setters sit below the field instead of above it? It would definitely save me some time as I don't have to manually move the field.

The getter and setter are generated in the position of the text cursor. Try to position the text cursor where you want the getter and setter to end up and then generate them.

Related

With IBM ODM does the BOM implement the getters/setters from the XOM?

If in your Java XOM you implement getters and setters, do the Navigation methods in the BOM execute the getters?
Yes, getters and setters can be used as any other methods in the rule. But, you can use the XOM even without getters and setters. And my suggestion is to go without getters and setters. Else if you need you can go..!!
Indeed, the getter and setter, if they exist, are used in preference of directly using the field, if the getter and setter are public.
Yes.
The tricky part is that Java getters and setters typically show up in the BOM as attributes, not methods. ODM automatically maps them like that by default. And then the navigation phrase will be on the attribute.
You can override that behavior overall by setting an option or one at a time by deleting the BOM attribute from the BOM class and definingthe getter (and setter) BOM method in its place (with B2X that references the Java method).

generate automatically getter and setter in Android Studio

When I programmed with Android Studio, it seems to me that is a way to generate automatically the getter and the setter of my new java class.
Can anyone tell me how to do it?
Open the class you want, right click anywhere in the code and chose the Generate option:
Then chose what you are trying to generate: setters, getters or both:
Then select the fields you want, and you're done.

Is it considered idiomatic to always prefer properties over backing fields?

I recently converted an Android project from Java to Kotlin using Android Studio's Kotlin plugin. Post-conversion, many fields in my classes were highlighted, and hovering over them displays the message
This property has a backing field
Should I infer from the existence of this message/warning that it is always considered more idiomatic (in Kotlin) to use properties with explicitly-defined getters and/or setters? Or is the IDE being a little over-zealous with the informational messages? Answers that include references to official sources preferred :)
Idiomatic is to use properties without backing properties. Backing properties are an exception case, as are custom getters and setters. You do need to be aware of the difference between a "Backing Field" and "Backing Property":
In the Kotlin docs, it talks about Backing Fields which are something you sometimes access from custom getters/setters and exist for any given property. There is an implied name for backing fields of field, so in a setter you would access this backing field by that name:
var counter = 0 // the initializer value is written directly to the backing field
set(value) {
if (value >= 0)
field = value
}
A Backing Property on the other hand is something that you define to store the value in another private property and of which you have complete control over. By convention it would be private and be prefixed with an underscore _. This looks like:
private var _table: Map<String, Int>? = null
public val table: Map<String, Int>
get() {
if (_table == null)
_table = HashMap() // Type parameters are inferred
return _table ?: throw AssertionError("Set to null by another thread")
}
Nothing to worry about, it's a known issue with the IDE. The highlighting is there simply for your information, cause backing fields require some extra care in certain cases.
If it troubles you, just turn the inspection off. Or change the highlighting to be not so bright.

How to automatically generate getters and setters in Android Studio

Is there a shortcut in Android Studio for automatically generating the getters and setters in a given class?
Using Alt+ Insert for Windows or Command+ N for Mac in the editor, you may easily generate getter and setter methods for any fields of your class. This has the same effect as using the Menu Bar -> Code -> Generate...
and then using shift or control button, select all the variables you need to add getters and setters
for macOS, ⌘+N by default.
Right-click and choose "Generate..." to see current mapping. You can select multiple fields for which to generate getters/setters with one step.
See http://www.jetbrains.com/idea/webhelp/generating-getters-and-setters.html
Android Studio & OSx :
Press cmd+n > Generate > Getter and Setter
Android Studio & Windows :
Press Alt + Insert > Generate > Getter and Setter
create the variable
right click
select "Generate" and then select "Getter and Setter" option
Android Studio & Windows :
fn + alt + insert
You can generate getter and setter by following steps:
Declare variables first.
click on ALT+Insert on keyboard placing cursor down to variable declaration part
now select constructor and press Ctrl+A on keyboard and click on Enter to create constructor.
Now again placing cursor at next line of constructor closing brace , click ALT+INSERT and select getter and setter and again press CTRL+A to select all variables and hit Enter.
That's it. Happy coding!!
Position the cursor under the variables -> right-click -> Generate -> Getter and Setter -> Choose the variables to make the get and set
or
Alt + Insert -> Getter and Setter -> Choose the variables
As noted here, you can also customise the getter/setter generation to take prefixes and suffixes (e.g. m for instance variables) into account. Go to File->Settings and expand Code Style, select Java, and add your prefixes/suffixes under the Code Generation tab.
Using Alt+ Insert or Right-click and choose "Generate..."
You may easily generate getter and setter or Override methods in Android Studio.
This has the same effect as using the Menu Bar Code -> Generate...
This answer deals with your question but is not exactly an answer to it. =) It's an interesting library I found out recently and I want to share with you.
Project Lombok can generate common methods, such as getters, setters, equals() and hashCode(), toString(), for your classes automatically. It replaces them with annotations reducing boilerplate code. To see a good example of code written using Lombok watch a video on the main page or read this article.
Android development with Lombok is easy and won't make your android application any 'heavier' because Lombok is a compile-time only library. It is important to configure your Android project properly.
Another example:
import lombok.Getter;
import lombok.Setter;
public class Profile {
#Getter #Setter
private String username;
#Getter #Setter
private String password;
}
Android development with Lombok is possible. Lombok should be a compile-time only dependency, as otherwise the entirety of Lombok will end up in your DEX files, wasting precious space. Gradle snippet:
dependencies {
compileOnly "org.projectlombok:lombok:1.16.18"
}
In addition you may want to add the Lombok IntelliJ plugin to support Lombok features in your IDE at development time. Also there is Hrisey library which is based on Lombok. Simply put, it's Lombok + Parcellable support.
You can use AndroidAccessors Plugin of Android Studio to generate getter and setter without m as prefix to methods
Ex: mId;
Will generate getId() and setId() instead of getmId() and setmId()
Use Ctrl+Enter on Mac to get list of options to generate setter, getter, constructor etc
use code=>generate=>getter() and setter() dialog ,select all the variables ,generate all the getter(),setter() methods at one time.
Another funny way
Type the parameter name anywhere in the object after definition, you will see setter and getter, Just select and click enter :)
I tried with Android Studio 2.3
Right click on Editor then Select Source -> Generate Getters and Setters or press Alt + Shift + S
Just in case someone is working with Eclipse
Windows 8.1 OS | Eclipse Idle Luna
Declare top level variable private String username Eclipse kindly generate a warning on the left of your screen click that warning and couple of suggestions show up, then select generate.

Resetting ui: how?

I have an activity with a UI with many elements (Radio, EditText, etc.). When I change the text in an EditText I want that UI back at its starting state.
I tried to kill and restart the activity but with poor results. Any ideas?
Thanks
I would try calling setContentView again with a new view or with the xml file you used for rendering your activity in the first place
Once you get the value you need from your editText, you can reset it using
editText.setText("");
You can similarly programatically control other parts of your UI too.
Why not put all of the reset stuff in one function and simply call it when you need to:
public void resetUI()
{
//reset UI programatically
}
You could add a listener to the EditText view and when the text is what you want it to be you can just programmatically empty any TextView's, EditText's, reset any radiobuttons or radio groups to their default values. No need to restart the activity. Just write a helper method that resets your view "manually". Using setContentView() could also work although I haven't tried it and you might have to setup your complete view again with listeners and such.

Resources