In addition to the already mentioned <pre>
tags, you should also use the @code
JavaDoc annotation, which will make life much easier when it comes to HTML entities issues (in particular with Generics), e.g.:
* <pre>
* {@code
* Set<String> s;
* System.out.println(s);
* }
* </pre>
Will give correct HTML output:
Set<String> s;
System.out.println(s);
While omitting the @code
block (or using a <code>
tag) will result in HTML like this:
Set s;
System.out.println(s);
For reference, a full list of tag descriptions available in Java SE 8 can be found here.
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
Go to System Tools → System Settings → Keyboard → Shortcuts tab → System → Lock Screen.
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.
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 File → Settings → Keymap
- macOS: Go to menu Preferences → Keymap and choose Eclipse from the drop-down menu.
Using MenuBar
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.
Best Answer
I can't find any shortcut to generate javadoc comments. But if you type
/**
before the method declaration and press Enter, the javadoc comment block will be generated automatically.Read this for more information.