Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio code formatting issue

I am using Android Studio and having an issue when formatting the code. When there is a statement that breaks into multiple lines and I need to format that to a single line statement, I can't do it using (Ctrl+Alt+L). The code didn't format to a single line using that shortcut.

I tried changing the value of Right margin(Settings -> Editor -> Code Style) to a higher number, but that also didn't work.

How to format the code to a single line?

Thanks.

like image 519
chathura Avatar asked Nov 23 '25 11:11

chathura


1 Answers

First go to Preferences > Editor > Code Style and set Right margin (columns) to 140 (or higher)

Now go to Preferences > Editor > Code Style > Java > Wrapping and Braces > Keep when reformatting and uncheck the checkbox Line Breaks, so the formatter will remove unnecessary line breaks and make the statement fit as much as possible in one single line

Also recommended, in Preferences > Editor > Code Style > Java > Wrapping and Braces make sure to check the checkbox Ensure right margin is not exceeded so that the formatter will force a line break when the statement exceeds the maximum right margin.

like image 77
Carlos B. Flores Avatar answered Nov 25 '25 04:11

Carlos B. Flores