How to set max line length in Android Studio code editor?
Asked Answered
S

15

104

How can I set a maximum length for Android Studio's code editor, or may be set a word wrap?

In my Android Studio, no matter how long a statement I type on a single line, it never automatically goes to the next line, but rather a horizontal scroll bar appears.

When I autoformat using the Ctrl+Alt+L (reference), nothing happens.

My SO or Google search found a lot of material on this for Eclipse, but not for Android Studio.

Suntan answered 15/6, 2015 at 17:43 Comment(0)
T
77

If you need not a soft wrap,
but actually, brake one long line onto several lines you need:

Go to Preferences(mac) or Settings(windows) -> Editor -> Code style -> Java/Kotlin -> Wrapping and braces (tab) -> Check "Ensure that right margin is not exceeded"

Now try to reformat your code:
press OPTION (ALT) + CMD + L on your Mac (or CTRL + ALT + L on PC)

Edit 13.02.19

As noted in comments this option seems not available in settings for Kotlin. However, there is a workaround to manually add it.
Here is what it takes to make it work:

  1. Export the settings scheme. enter image description here

  2. Open saved xml file.

  3. Find <codeStyleSettings language="kotlin"> tag (or create)

  4. Add the WRAP_LONG_LINES setting manually:

    <codeStyleSettings language="kotlin">
       ...
       <option name="WRAP_LONG_LINES" value="true" />
    </codeStyleSettings>
    
  5. Save edits and and import back the scheme.

  6. Here you go!

Tizes answered 10/3, 2016 at 16:2 Comment(5)
I think it is "Settings", not "Preferences". Sits in the "File" menu.Barnette
It depends on whether you are on Mac or PCTizes
this option is not available for kotlin in andriod studio 3.2Crowther
Created an issue about this problem in Kotlin for JetBrains youtrack.jetbrains.com/issue/IDEA-229595Apomixis
@KonstantinZyubin is your feature request the same as supporting WRAP_LONG_LINESGerome
W
77

MacOS:

  1. Click on Android Studio ( left top corner )
  2. Click Preferences
  3. Type Code Style in search
  4. Click on Code Style
  5. There is a filed right to the 'Hard wrap at' - you can enter needed about of symbols you need

    enter image description here

Welty answered 11/12, 2019 at 10:14 Comment(2)
for dart you have to set the dart file line-length limit, Preferences->Editor->Code Style->Dart->Line LengthPokeberry
any reason why at Dart it's only 80?Napalm
B
37

In File | Settings, select "Editor", then "Code Style". There is an option "Wrap when typing reaches right margin".

Edit: Just tried it, and it doesn't seem to work. Anyway, maybe the option "Use soft wrap" in the "General" group is more what you want.

Blasien answered 15/6, 2015 at 17:55 Comment(0)
A
27

With version 4.1.2 and with Dart language you need to go to "File/Settings/Editor/Code Style/Dart" and modify the "Line length" to the preferred number. enter image description here

Aubade answered 18/2, 2021 at 12:56 Comment(0)
W
19

@Solace, actually @Fahim Faysal's answer is quite close to what you need, but since you want to solve the problem no matter how long a statement I type on a single line, it never automatically goes to the next line, 2 more steps you need to follow:
[Step 9] type the max length you prefer at `"Hard wrap at" ()
[Step 10] change "Wrap on typing" from Default: No or No to Yes

enter image description here

Walliw answered 21/10, 2019 at 14:52 Comment(0)
C
14

In Android Studio: File->Setting-> Editor->code Style->java->Wrapping and braces(tab:right side)->keep when reformatting->Line break(make unchecked)

enter image description here

Counterirritant answered 13/6, 2016 at 7:25 Comment(0)
O
11

For dart settings => code style => Dart => Line length

enter image description here

Orpha answered 30/11, 2022 at 11:11 Comment(0)
H
8

You can change the max column width from: File > Settings > Editor > Code Style > Right Margin (columns)

Hassle answered 26/12, 2016 at 12:58 Comment(2)
When I change the right margin, after I restart Android Studio the margin value is reseted to 100. To avoid it, I have to go to java / wrapping and braces and then change right margin to my value (for example 150) AND change Wrap on typing to NO, then APPLY...Snow
THANK YOU. That's exactly what I was looking for.Ambiguous
I
5

Android Studio 3.5:

Mac - Android Studio > Preferences > Editor > CodeStyle > HardWrap at: ____

enter image description here

Inhabitant answered 26/8, 2019 at 17:16 Comment(0)
L
5

The existing answers already answer the question in straight forward way. As a different approach you could set the style guide to kotlin style guide code style. Then if you do alt + ctrl + l as you did, you'll see auto wrap as you expected.

  1. Just open the settings, search for kotlin and look for kotlin under code style.

  2. On the top right hand side of the settings window you'll see Set from..., click on it.

  3. Then on predifined style > kotlin style guide

Android studio settings

If you'd like to follow the kotlin formatting style this could reduce more effort. For more information: https://developer.android.com/kotlin/style-guide

Logistic answered 1/5, 2020 at 6:39 Comment(0)
P
4

My Environment:

  • Ubuntu 18.04
  • AndroidStudio 3.3.2

My operate:

  • File -> Settings -> Editor -> Code Style
    1. Scheme -> select Project
    2. Hard wrap at '120'
    3. Apply
Placentation answered 20/3, 2019 at 11:14 Comment(0)
G
2

To move the grey vertical line, that tells Android Studio where to wrap your code, go to

Settings- Editor- Code Style- Java or Kotlin- Wrapping and Braces- Hard wrap at

and enter the number of characters, where the line should be.

enter image description here

Gounod answered 14/2, 2019 at 7:37 Comment(0)
G
1

In settings/code style/kotlin/wrapping and braces replace the "do not wrap" values with "wrap if long"

Gratulate answered 25/2, 2019 at 9:30 Comment(0)
A
1

You can change the Hard wrap at param

Hard wrap at

Andino answered 5/8, 2019 at 14:48 Comment(0)
V
0

if you want to increase the font size of Ediotr go to -> settings-> scroll down and click on Editor and then select the font increase its size according to you need.

settings->Editor->font

Vashtee answered 20/4 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.