Different Ways to Format Code in Android Studio
Code formatting is very important when you are building an Android application. This will help you to organize your code properly and to maintain your code to make it easily readable. Below is a list of purposes of having a standard coding format.
Purposes of Having Coding Standards
- A coding standard gives a uniform appearance to the codes written by different engineers.
- It improves the readability, and maintainability of the code, and it reduces complexity also.
- It helps in code reuse and helps to detect errors easily.
- It promotes sound programming practices and increases the efficiency of the programmers.
In this article, we will take a look at different ways to format the code in Android Studio. We are going to discuss three different methods with the help of which we can format the code in our Android Studio.
- Method 1: By using a shortcut key for formatting a code in any file in Android Studio
- Method 2: Formatting a code from MenuBar
- Method 3: By using different shortcut keys for formatting our code
Method 1: By using a shortcut key for formatting a code in any file in Android Studio
In this method, we simply have to navigate to the file in which we have to format our code. 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. You can get to see below image 1 in which our code is not formatted properly and after pressing this shortcut key our code will be formatted as shown in the second image.
Shortcut keys for different Operating Systems
Operating System |
Shortcut key |
---|---|
Windows | Ctrl+Alt+L |
Mac OS | Option+Cmd+L |
Linux | Ctrl+Shift+Alt+L |
Method 2: Formatting a code from MenuBar
In this method, we will rearrange our code from the Menu Bar. To format our code navigate to the file in which we have to format our code and inside that file. Navigate to the top bar and click on the Code option and inside that option click on Reformat code option which is shown in the below screenshot.
Note: This method will be similar for all the operating systems
Method 3: By using different shortcut keys for formatting our code
Navigate to the file in which we have to format our code and then click on the shortcut key as Ctrl+Shift+Alt+L after clicking this key you will get to see the below dialog on your screen. Select the formatting options and your code will be formatted and rearranged according to selected options. The options present inside this dialog box is explained below.
Shortcut keys for different Operating System
Operating System | Shortcut key |
---|---|
Windows | Ctrl+Shift+Alt+L |
Mac OS | Cmd+Alt+L |
Linux | Ctrl+Super+Alt+L |
Contact Us