2

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.

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Fixed the issue!
0

Is the code wrapping into the next line? If so, you can disable soft wrapping ins View -> Active Editor -> Use Soft Wraps. The code is still considered on the same line with or without soft wraps, the editor just displays it differently.

If not, try editing the reformatting settings. Under Preferences, go to Editor -> Code Style and select the language. There is a tab Wrapping and Braces that let you customize the rules Android Studio will use when formatting the code.

2 Comments

soft wraps is disabled,
An example of what you are seeing would be helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.