How to Automatically Format Your Code in Android Studio

preview_player
Показать описание
In this Android Studio Note video you'll learn how to keep your code perfectly formatted without manual changes.

Mac: Command+Option+L
Windows: Ctrl+Alt+L

Tip: turn on subtitles to deal with my accent. A full transcript is also available below.

----------------------------------------

Checkout 320+ technical in-depth tutorials:

Subscribe for two new videos every week:

----------------------------------------

Follow us on social media to get updates on new content:

----------------------------------------

Full transcript:

Today we've the reformat code option. Ideally, in your company or in your open source project, you've agreed on code conventions. So, sometimes developers like to have spaces between parameters, sometimes they don't. There is the discussion, should curly brackets be on the same line or on the new line? It doesn't matter to me, just make it consistent.

You can configure all of these preferences in the settings. Once you've started the project, make sure everyone has the settings configured. You can even share them. Then you can use the "reformat code" option.

The reformat code shortcut is command+option+L, for windows it's ctrl+alt+L. Once you've hit that it'll automatically change all the code in your class to the defined pattern.

If your code conventions says there is no spaces and the curly bracket should be on the same line, it'll apply this logic. You don't need to go through it manually. Just one click of a button and it'll automatically do it across the entire file.

What I recommend that you enable this option in the commit dialog. If you see up here you can enable this by default and it'll reformat the code for the commit. This makes sure that the code in the central repository is always perfectly formatted. Ideally, you can convince all of your team mates to enable this option.

That was it. Thank you for watching! If you didn't know this option before, give this video a thumbs up and subscribe for more video. Make it rock!
Рекомендации по теме
Комментарии
Автор

This video would be greatly improved if you'd explained how to set the formatting settings.

fedos