skip to Main Content

I am developing in Android Studio, using entirely Kotlin classes. I prefer to use tabs for indentation. If there is some grave sin I’m committing here, feel free to let me know ;). Whenever I press Enter, Android Studio adds the proper amount of tabs for indentation, then adds a single space. It’s annoying and makes my new line not line up with the others!

The space is not auto-deleted when I start to type. You’d think I could just get in the habit of pressing backspace at the beginning of every new line, but frustratingly, pressing Enter then Backspace deletes the whole line, not just the single space.

Taking inspiration from other SO posts I read, I look in Settings > Editor > Code Style > Kotlin, but there doesn’t appear to be any options to disable this? How do I get rid of it?

Before pressing Enter:

After pressing Enter:

Thank you all!

Research Update: Even after switching to using spaces for indentation instead of tabs, this still happens. And it only happens sometimes, as shown here:

My Editor settings are shown here

2

Answers


  1. It is best practice to format your code on Android Studio to make it more readable.

    Press: Ctrl + Alt + L

    It will automatically remove unwanted spaces, add indents, etc, Press it again and again for more formatting.

    If you don’t want to format your code at all,
    Preferences > Editor > Code Style and select the language you want to apply the changes to (e.g., Kotlin for Android). Then, uncheck the Auto-indent option under the General tab.

    Login or Signup to reply.
  2. TL; DR Your parent indentation is not consistent and Android Studio is trying to set indentation to 4 spaces relative to whatever the parent is at.

    In your LoginActivity onCreate function the rest of the function is indented only 3 spaces relative to the override fun onCreate. Since your indentation preference is set to 4 spaces, when you press enter to add a new line it puts it at 4 spaces of indentation relative to that (so it has an extra space relative to the line above it like you saw).

    This is because you indented override 5 spaces instead of 4, so the body of that function would be indented 9 spaces.

    The fix is simple – take a space off from in front of override so that it is indented correctly. Then new lines in there will default to 8 spaces (total) of indentation.

    The same thing is true on your second example, and is the reason you don’t get an extra space after your val test example since that is already indented to the right position relative to its parent.

    When you press enter inside a () the "parent" context there is the line above it with the (, so it matches that line instead of the overall function indentation.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search