Home » Android » Android studio format code won't auto break line

Android studio format code won't auto break line

Posted by: admin April 23, 2020 Leave a comment

Questions:
tempCanvas.drawArc(cx - mInnerCircleRadius, cy + mInnerCircleRadius,cy + mInnerCircleRadius,cy + mInnerCircleRadius);

For example, the above code is too long, so I want to break the line at every ,. Eclipse will do it but Android Studio won’t break the line when I hit format code. Is there any method to do it?

How to&Answers:

In Android Studio (and probably any other IDE) you don’t break (wrap) lines by , or any other character, you define a maximum line width and set the formatter to ensure the maximum line width is not exceeded.

You do that on File -> Settings on Windows, or in Preferences on Mac OS.

To do this in Android Studio go to

File > Settings > Editor > Code Style

and set Right margin (columns) to 120 (or the line width you want)

Now go to

File > Settings > Editor > Code Style > Java > Wrapping and Braces (tab)

and check the checkbox Ensure right margin in not exceeded

Apply the changes and press OK

Now in the editor, to format your code to that line width, press:

Code > Reformat Code... 

Done

In Mac OS instead of File > Settings go to Preferences, follow the same steps.

Answer:

Configure > Settings > Editor > 'Use soft wraps in editor'

Or right click on the divider where you can choose to show line numbers, and click ‘Use soft wraps’

This won’t break your code at your commas, but it will wrap your code so it’s not too long to read.

Answer:

File -> Settings > Editor > Code Style.

Then select Wrap on typing CHECKBOX.

Answer:

On the latest Android Studio:

  1. Goto File > Settings > Editor > Code style > Java > Wrapping and Braces
  2. Change do not wrap to wrap if long for all the items
  3. Now Select the portion of the text you want to format and press ctrl + Alt + L

enter image description here

Answer:

File> Settings > Editor > Code Style > Kotlin > 'Set from...' - 'Predefined Style' - 'Kotlin style guide' 

when you want to use predefined code style for Kotlin in Android Studio/Idea (general rules from Coding Conventions)

Answer:

As a reminder to others visiting this question, you can also manually add line breaks by hitting enter after the commas or other appropriate places in the code. Android Studio will automatically indent it. This makes long lines more readable.

tempCanvas.drawArc(
        cx - mInnerCircleRadius, // You can also add comments 
        cy + mInnerCircleRadius, // to different parts of a line
        cy + mInnerCircleRadius, // this way.
        cy + mInnerCircleRadius);

Answer:

If you don’t wanna wrap a line:

File -> Settings -> Editor -> Soft Wraps -> Uncheck Use soft wraps in editor