Code formatting shortcuts in Android Studio for Operation Systems
Code formatting shortcuts in Android Studio vary across operating systems. Use them to quickly format code and improve readability.
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 user 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.
1. Go to System Tools → System Settings → Keyboard → Shortcuts tab → System → Lock 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.
Ctrl + Alt + Shift + L
(to show a dialog)
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.
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.
Using code formatting shortcuts in Android Studio is an essential practice for developers.
These shortcuts automatically adjust the indentation, alignment, and spacing of your code, making it more readable and maintainable. The shortcuts vary depending on the operating system: on Windows and Linux, you can use Ctrl + Alt + L
, while on macOS, it's Cmd + Option + L
.
Regularly using these shortcuts will ensure your code adheres to standard formatting conventions, leading to fewer errors and easier collaboration. Additionally, these tools save valuable time during development, allowing you to focus on writing functionality rather than worrying about formatting issues.