882

I have started developing with Android Studio. In Eclipse I was using Ctrl + Shift + F, but in Android Studio it does not work. It will be different.

How can I jump to any method in a .java file? I was using Ctrl + O in Eclipse.

Is it available? What is the shortcut for code formatting?

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Bhavesh Hirpara
  • 22,255
  • 15
  • 63
  • 104
  • 1
    http://stackoverflow.com/questions/294167/what-are-the-most-useful-intellij-idea-keyboard-shortcuts – OschtärEi May 16 '13 at 06:12
  • 13
    Its just intelliJ with some addons. So whenever you have a "how do I" question on it, search for the answer for intellij – Gabe Sechan May 16 '13 at 06:14
  • 2
    For a complete list of shortcuts visit http://developer.android.com/sdk/installing/studio-tips.html#KeyCommands – Stelian Feb 01 '14 at 23:19
  • 2
    u could also record a macro like "CMD + SHIFT + L" and then "CMD + S" and set the CMD+S Shortcut to this macro. u can record a macro in Edit->Macros->Start Macro Recording. And you can change the shortcuts in AndroidStudio->Preferences->KeyMap, where you'll find your saved macro in "Macros" and the Save-Action (where you have to remove the CMD+S shortcut) in Main menu->File. Hope this is an additional Help! I think it's faster to format on save because you should be saving your code often anyway. – Coding John Jan 11 '18 at 08:39
  • See also "Intellij Idea Keyboard Shortcuts" http://wiki.c2.com/?IntellijIdeaKeyboardShortcuts – Shomu Oct 13 '18 at 11:23

24 Answers24

1882

Windows: Ctrl + Alt + L

Linux: Ctrl + Shift + Alt + L

macOS: Option + Command + L

Reference: Key Commands and here are all of the commands for Windows/ Linux users and for Mac users.


As Rohit faced a problem in Ubuntu with the format code shortcut, this is due to the Ctrl + Alt + L key being used to lock the screen in Ubuntu.

I found that Ubuntu handles this keyboard shortcut first. So you should bind the Ctrl + Alt + L keyboard shortcut to something else so that it doesn't conflict with Ubuntu.

Steps

  1. Go to System ToolsSystem SettingsKeyboardShortcuts tab → SystemLock Screen.

  2. Select the row New Accelerator..., then press any special key with the Alpha key (e.g. Shift + L). You should've successfully changed the keyboard shortcut.

  3. Check if the keyboard shortcut now works in Android Studio.

Alternative method

Ctrl + Alt + Shift + L (to show a dialog)


You can also use Eclipse shortcuts.

  • Windows and Linux: Go to menu FileSettingsKeymap
  • macOS: Go to menu PreferencesKeymap and choose Eclipse from the drop-down menu.

Using MenuBar

  • Select code which you want to reformat

  • Click on Code in the menu bar and select Reformat Code as shown in the image below:

    Enter image description here

In Android studio 3.5, there is problem with rearranging the xml code, it also rearrange the views as well, so need to use these settings first.

Settings -> Editor -> Code Style -> XML-> Set From -> Predefined Style > Android.

Update for Android Studio 4+

Since Android Studio 4+, you might face issue shortcuts on mac. Shortcuts not working after updating to Android studio 4, in Mac? have an answer for that.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • Thanks. can you please tell me how to jump to any method in android studio.in eclipse `ctrl+o` – Bhavesh Hirpara May 16 '13 at 06:21
  • it does not format the comments. if i have given more space before a comment line it will stay there until i manually cut all those space. please do suggest a walk around. – Sagar Nayak Jun 01 '17 at 10:53
  • @SagarNayak For that you need to set lines into editor's settings. Read more at https://stackoverflow.com/questions/11118204/how-to-remove-unnecessary-blank-line-on-code-formatting-in-intellij or https://stackoverflow.com/questions/26354301/how-do-i-auto-remove-trailing-whitespace-in-android-studio – Pankaj Kumar Jun 01 '17 at 11:01
  • 1
    Also works in PyCharm (at least on Mac), so I assume this could be a helpful answer for users of all JetBrains products (not only Android Studio) – jammartin Aug 26 '17 at 08:15
  • @jmartin Correct. Actually Android Studio is customized JetBrains, so almost all shortcuts are same. – Pankaj Kumar Aug 26 '17 at 12:11
  • Ohh. They don't understand how it is important to format the code during the writing. Why? Why on NetBeans I dont bother about what shortcut it should be? Or recall that stuff. Just rightclick and format! – CodeToLife Oct 07 '18 at 17:11
  • EDIT 3 : Using MenuBar... only helped me for rearranging XML. I tried many times alt+cmd+L in mac Android studio but no luck.. for java file it was working fine. – Himanshu Mori Feb 19 '19 at 19:59
  • 1
    Ctrl + Alt + L not working when i tried with korlin. – Arbaz.in Aug 05 '19 at 08:28
113

You can also use Eclipse's keyboard shortcuts: just go to menu Preferenceskeymap and choose Eclipse from the dropdown menu.


The actual path is: menu FileSettingsKeymap (under IDE settings)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2340612
  • 10,053
  • 4
  • 41
  • 66
74

Check Keyboard Commands given in the Android Studio Tips & Trick documentation:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
33

Just select the code and

  • on Windows do Ctrl + Alt + L

  • on Linux do Ctrl + Super + Alt + L

  • on Mac do CMD + Alt + L

This solution from junaidp

Sid110307
  • 497
  • 2
  • 8
  • 22
Michael Horojanski
  • 4,543
  • 4
  • 33
  • 34
  • 1
    P.S. It's probably a good idea to state what the "Windows key" is especially on a Mac running Ubuntu :) – Edric Nov 12 '18 at 09:49
16

For formatting code in Android Studio on Linux you could instead use Ctrl + Alt + Super + L. You could use this and avoid having to change the system shortcut. (Super key is the Windows icon key besides the Alt key).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Capt
  • 261
  • 4
  • 11
  • 1
    This solved my problem. Not sure why this wasn't mentioned in android's Keyboard Commands document. – mnaa Oct 03 '14 at 10:28
12

For code formatting in Android Studio:

Ctrl + Alt + L (Windows/Linux)

Option + Cmd + L (Mac)

The user can also use Eclipse's keyboard shortcuts: just go on menu SettingPreferencesKeymap and choose Eclipse (or any one you like) from the dropdown menu.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
11

You will have to apply all Eclipse shortcuts with Android Studio before using all those shortcuts.

Procedure:

Steps:

Setting -> KeyMap -> Select Eclipse -> Apply -> OK

Now you can use all Eclipse shortcuts in Android Studio...

Have some snapshots here.

Enter image description here

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manoj Behera
  • 2,736
  • 22
  • 13
11

For auto formatting your Java/XML files in Android Studio, use following commands:

As you are looking for a single point to go with your work as Eclipse did, there is no such built-in functionality in Android Studio, but it gives you the ability to do it through a macro. Follow these steps:

In Android Studio, open any of your source code files.

Press Ctrl + Alt + O This is used to find/optimize unused import.

If a dialog open select "Don't show it again" and hit Run.

Go to Edit > Macros > Start Macro Recording.

Press Ctrl + Alt + O.

Press Ctrl + Alt + L. This formats your code. (Shift+Ctrl+Alt+L for Ubuntu).

Press Ctrl + S. This saves your file;)

Go to Edit > Macros > Stop Macro Recording.

Save this created macro with a name, for instance: “Auto Formatting".

Boom, you have now successfully created your macro. Now just add/assign a shortcut to it:

Open Preferences.

Search in the left bar for Keymap.

In the right hand pane, click in the search bar and type the name of your saved macro ("Auto Formatting".)

Double click on your item. There might be two, it doesn’t matter which one you click on.

Click Add Keyboard Shortcut.

Set your keyboard shortcut to Ctrl + S.

Finally, confirm overriding Ctrl + S to be your new macro.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bethan
  • 971
  • 8
  • 23
9

Check this. Also you can change it as per your preference.

enter image description here

enter image description here

Deniz
  • 12,332
  • 10
  • 44
  • 62
8

Just to add to @user2340612 answer to switch keymaps to Eclipse, the path for Android Studio 1.0.1 is:

Menu FileSettingsKeymap (under the Editor option) → Keymaps = Eclipse

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74
7

In addition to auto-formatting that the other answers describe, another trick is to highlight the text that you want to indent and then press

  • Tab to increase the indent or
  • Shift+Tab to decrease the indent.

enter image description here

Check out the Keyboard Shortcut docs for more.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
6

Ctrl+Alt+L might conflict with Lock Screen shortcut in Ubuntu. In such case you can change the keyboard shortcut for Reformatting Code as follows:

File-> Settings-> IDE Settings-> Keymap

Search for Reformat Code and change the keyboard shortcut.

Philipp M
  • 1,877
  • 7
  • 27
  • 38
Prashant_M
  • 2,868
  • 1
  • 31
  • 24
6

Really, I went to this thread because of my Ubuntu locks screen after this shortcut Ctrl + Alt + L. So if you are have the same problem just go to the Settings - Keyboard - Shortcuts - System and change the default shortcut for the "Lock screen".

mtb
  • 1,350
  • 16
  • 32
Yev Kanivets
  • 1,780
  • 3
  • 22
  • 38
5

For those who are wondering about the alignment issue inside bracket, JetBrains has this as in their issue tracking.

Here is the answer:

How do I align/format code in Android Studio?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daksharma
  • 599
  • 1
  • 7
  • 9
4

The shortcut that worked for me is

SHIFT+ALT+CMD+L

You can optimize imports to remove the ones you don't use, and auto import the new classes.

enter image description here

enter image description here

mtb
  • 1,350
  • 16
  • 32
Kokusho
  • 1,113
  • 1
  • 9
  • 14
4

I think is clear that for code formatting in Android Studio the combination keys are:

CTRL + ALT + L (Win/ Linux)

OPTION + CMD + L (Mac)

However, we forgot to answer about the Jumping into the method. Well to go into any declaration/implementation there three ways:

  1. Goto Declaration

CTRL + B or CTRL + CLICK (Win/ Linux)

CMD + B or CMD + CLICK (Mac)

  1. Goto Implementation

These commands show a list of all the classes/interfaces that are implementing the selected class/interface. On variables, it has the same effect as Goto Declaration.

CTRL + ALT + B (Win/ Linux)

CMD + ALT + B (Mac)

  1. Goto Type Declaration

These shortcuts will go into the declaration of the “AnyClass” class.

CTRL + SHIFT + B (Win/ Linux)

CTRL + SHIFT + B (Mac)

Additionally, there is a shortcut for Goto the Super Class. This will open the parent of the current symbol. Pretty much the opposite of Goto Implementation. For overridden methods, it will open its parent implementation.

CTRL + U (Win/ Linux)

CMD + U (Mac)

Community
  • 1
  • 1
Teocci
  • 7,189
  • 1
  • 50
  • 48
4

If you are using the Dart plugin, go to Android Studio, menu File -> Settings. And search for "reformat code with", click "Reformat code with dartfmt" under the main menu:

Enter image description here

Click "Add keyboard Shortcut". Then press Ctrl + Alt + L and the shortcut should work (If Ctrl + Alt + L make the computer sleep/suspend, change the shortcut in your system settings to something else. Otherwise, both shortcuts will collide).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Guy Luz
  • 3,372
  • 20
  • 43
4

Some times even I type Ctrl+Alt+L is not working in XML, so found this way to make it work.

Go to Settings --> Editor --> Code Style --> Select Default --> Ok.

For your reference see the screenshot:

enter image description here

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
2

Try this.

  • On Windows do Ctrl + Alt + L
  • On Linux do Ctrl + Shift + Alt + L for dialog to open and then reformat.
  • On Mac do CMD + Alt + L

Note: Here many answers for Linux is just Ctrl + Alt + L which is wrong. In Linux, doing Ctrl + Alt + L locks the system.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
Saket Kumar
  • 463
  • 8
  • 22
  • The lock happens because in Linux DE like KDE the shortcut is set for locking the desktop. For KDE, Change it from `KDE Menu -> System Settings -> Shortcuts and Gesture -> Global Keyboard Shortcut`, then in KDE Component spinner select The KDE Session Manager. Clear **lock session** shortcut. – ישו אוהב אותך Feb 17 '17 at 06:03
2

It's Ctrl + Alt + L for Windows. For a complete list of keyboard shortcuts please take a look at the user manual: https://developer.android.com/studio/intro/keyboard-shortcuts.html

Mwiza
  • 7,780
  • 3
  • 46
  • 42
Vibhu Jain
  • 130
  • 10
2

There are some shortcut keys for rearrange or reformat code in Android studio.

Linux: Ctrl + Shift + Alt + L

mac OS: Option + Command + L

Windows: Ctrl + Alt + L

Sid009
  • 421
  • 5
  • 16
1

I have had an experience using Ctrl + Alt + L in Windows that this shortcut did not work at all because it was reserved by another software, running at the background; which I realized later.

I decided to mention this issue as I thought it may be helpful . . .

elyar abad
  • 771
  • 1
  • 8
  • 27
-1

The best key where you can find all commands in Eclipse is Ctrl + Shift + L.

By pressing this you can get all the commands in Eclipse.

One important is Ctrl + Shift + O to import and un-import useless imports.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
-1

You can use the following shortcut for code formatting: Ctrl+Alt+L

Zoe
  • 27,060
  • 21
  • 118
  • 148
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81