Question

When overriding methods, android studio automatically inserts a line break after the @Override annotation.

How can I have the IDE insert the @Override without the line break and have it immediately followed by the method declaration (ie on the same line ?):

@Override public Fragment getItem(int position) { ... }

instead of:

@Override
public Fragment getItem(int position) { ... }
Was it helpful?

Solution

File -> Settings -> Code Style -> Wrapping and Braces -> Method annotations and set to Do not wrap.

Note: Changing the setting will not actually reformat all existing ones. It just won't be wrapped when inserted automatically by the IDE, as you want in your question. This applies to all annotations for methods.

enter image description here

OTHER TIPS

It is possible to have it on the same line, but I would advise against it. It is a common convention to always have the annotation on a separate line.

  • not following conventions without any reason to makes it inconvenient for developers working on several projects or changing project
  • when you use many annotations the lines will very easily break ~120 chars, making the code less readable

if you still want them on the same line, this is a solution: Intellij code formatting, Java annotations on new lines

It's perfectly valid from the viewpoint of Java syntax to have @Override annotation on the same line as method signature. One thing to do is to tune your IDE autoformatiing settings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top