Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rearrange code with overridden methods at the top in Android Studio?

In the Preferences > Code Style > Arrangement- tab, I have to manually enter matching rules for specific method names if I want to arrange them to be at the top.

For example, I have an activity where I want the lifecycle- and other overridden methods to be at the top:

public final class MyActivity extends Activity {
    // Fields

    @Override
    public final void onCreate(...) { ... }
    ...

    // Other methods
}

Is there a better way?

like image 595
Gerstmann Avatar asked Apr 25 '14 07:04

Gerstmann


People also ask

How do I rearrange the code in Android Studio?

After opening the file we have to simply press the shortcut key as Ctrl+Alt+L to reformat the code in your file. With this key, the code in your file will be rearranged according to the code standards.

What does @override do in Android?

@Override is an java annotation . Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

How can I see all methods in Android Studio?

Open your file in editor and click on structure tab, it will show all the methods in the class selected. Click on any method and you will be redirected to that method. Show activity on this post. You can use CTRL + F12 shortcut in windows for that purpose.


1 Answers

In Android Studio 3.0 under Preferences > Editor > Code Style > Java > Arrangement Tab you can add a rule to match overridden and place it after the rules for fields and constructors but before the rules for methods.

You can then perform Code > Rearrange Code and it should arrange your code appropriately

Arrangement Screenshot

like image 144
shyamal Avatar answered Oct 03 '22 11:10

shyamal