How to move (up/down) code lines in Android Studio?

Intellij IdeaIdeAndroid Studio

Intellij Idea Problem Overview


In eclipse we use Alt + / to move a line up or down.

Is there any shortcut in Android Studio to do the same? Or any quick way to avoid copy & paste?

Intellij Idea Solutions


Solution 1 - Intellij Idea

To move a line:

Place the caret at the line to be moved.

Do one of the following:

On the main menu, choose Code | Move Line Up or Code | Move Line Down.

Press Shift+Alt+Up or Shift+Alt+Down.

Solution 2 - Intellij Idea

If you want the exact behavior of eclipse you can do this:

File --> Settings --> Keymap --> Code--> Folding--> assign Alt+/ to "Move Line Up/down" instead of "Move Statement up/down"

Solution 3 - Intellij Idea

There are (at least) two kinds of Moving line up/down in Android Studio: The "intelligent" one and the "dumb" one. Like IngoAlbers said, the dumb one (Shift+Alt+<Arrow>) just moves the line.

Using Ctrl+Shift+<Arrow> instead, makes the functionality more intelligent:

  • It doesn't leave the current "context":

      public static void test() {
          int i = 5;
          Runnable theodor = new Runnable() {
              public void run() {
                  System.out.println("Hi!");
              }
          };
      }
    

    Moving the int i = 5;line down one step, brings you this:

      public static void test() {
          Runnable theodor = new Runnable() {
              public void run() {
                  System.out.println("Hi!");
              }
          };
          int i = 5;
      }
    
  • It keeps methods together:

      public static void hello() {
          System.out.println("Hello!");
      }
    
      public static void dinner() {
          System.out.println("Dinner's ready!");
      }
    
      public static void sleep() {
          System.out.println("Good night.");
      }
    

    Moving the line public static void sleep() { up one step moves the complete sleep() method above dinner():

      public static void hello() {
          System.out.println("Hello!");
      }
    
      public static void sleep() {
          System.out.println("Good night.");
      }
    
      public static void dinner() {
          System.out.println("Dinner's ready!");
      }
    
  • In most cases, it is just annoying. ;-)

Solution 4 - Intellij Idea

Android Studio move code lines

MacOS

Code -> Move Line Up/Down

Option + Shift + Up/Down

Solution 5 - Intellij Idea

In windows, you can go to 'Keymap' settings(File --> Settings --> Keymap --> Editor Actions) to customize this action and more actions.

I think it's a better way.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3473590View Question on Stackoverflow
Solution 1 - Intellij IdeaIngoAlbersView Answer on Stackoverflow
Solution 2 - Intellij IdeaMilad FaridniaView Answer on Stackoverflow
Solution 3 - Intellij IdeaBowiView Answer on Stackoverflow
Solution 4 - Intellij IdeayoAlex5View Answer on Stackoverflow
Solution 5 - Intellij IdeaITAB_It'sMeView Answer on Stackoverflow