25

Before Android Studio, I was using android-formatting.xml as Code Style in Eclipse.

How can I setup Android Studio / Intellij Idea to format code as in Eclipse?

UPDATE

I tried Eclipse Code Formatter plugin. It seems to work, but you cannot edit any setting at all. My team prefers 120 columns instead of 100.

I tried @CrossleSong answer too. It's a bit tricky, but more powerful. You can save this

https://android.googlesource.com/platform/development/+/android-4.2.2_r1.2/ide/intellij/codestyles/AndroidStyle.xml

as Android.xml and copy it to AndroidStudio/Intellij codestyles folder: for me is

~/Library/Preferences/AndroidStudioPreview/codestyles

Now you can select Android as Code Style in Android Studio Preferences.

Janusz
  • 187,060
  • 113
  • 301
  • 369
Ivan Morgillo
  • 2,837
  • 4
  • 30
  • 46
  • 1
    Turns out, if you restart Android Studio, then you don't have to rename the AndroidStyle.xml file, which might be a better approach just in case a future update tweaks the original Android.xml file. Just choose the right scheme under preferences > code style > java – gMale Oct 04 '13 at 17:44
  • 1
    With the new Android Studio Beta release you can add the xml file here: ~/Library/Preferences/AndroidStudioBeta/codestyles – Ray Hunter Jun 27 '14 at 16:55

4 Answers4

12

You can download the development repo from Android source. The development/ide/intellij folder includes code style, templates...

Daniel Serodio
  • 4,229
  • 5
  • 37
  • 33
Crossle Song
  • 10,104
  • 2
  • 29
  • 38
6

Instructions to use Eclipse XML formatter in Android Studio:

  1. File -> Settings -> Plugins
  2. click 'Browse repositories' button, enter 'Eclipse Code Formatter' in the search field and install the plugin.
  3. Restart Android Studio.
  4. File -> Settings -> Other Settings -> Eclipse Code Formatter (if you cannot find it, then use search field for that)
  5. select 'Use the Eclipse code formatter', browse your xml style file and press OK button to close the Settings.

I hope I wrote instructions clear. Let me know in case you didn't understand.

Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
  • I cant find 'File -> Settings -> Other Settings' in android studio – Vihar Nov 26 '15 at 12:33
  • @Vihar, maybe you have another Android Studio version, this is how it looks like in mine http://prntscr.com/97586q. Anyway if you cannot find it you can use a search field, just type there 'Eclipse Code Formatter'. – Ayaz Alifov Nov 26 '15 at 12:47
4

You can use the third-party Eclipse Code Formatter plug-in for IntelliJ IDEA / Android Studio.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
2

For Android Studio 1.2.2 and above:

What didn't work for me:

Thanks for the question. There are good answers and hints here but for my case where I updated to Android Studio 1.2.2, it didn't work right out of the box. Some answers and blogs suggested that after downloading the xml file,

  1. Save the xml file to ~/Library/Preferences/AndroidStudio/codestyles.

  2. Then restart AndroidStudio

  3. Go to Android Studio -> Preferences -> Code Style -> Java and from Scheme dropdown select the scheme name and save.

    However when I put the xml file inside ~/Library/Preferences/AndroidStudio/codestyles/ folder it didn't show my scheme name in the Scheme dropdown to select from.

What worked for me:

After I updated Android Studio to latest version, under ~/Library/Preferences/ in mac, I had two folders: AndroidStudio and AndroidStudio1.2. For importing the AndroidStyle.xml code style file in the updated Android studio, I had to create a folder named codestyles inside ~/Library/Preferences/AndroidStudio1.2/ folder, since none existed. Then I saved the xml file in there and followed the above procedure. Just saving the file solely in .../AndroidStudio/codestyles folder didn't work for me.

Hope it helps someone some day.

Community
  • 1
  • 1
Shobhit Puri
  • 25,769
  • 11
  • 95
  • 124
  • After I tried this, Android Studio 1.5 was always crashing on startup. The only solution that I found was by removing the whole codestyles dir like here: http://stackoverflow.com/a/34140623/1390874 – goRGon Dec 07 '15 at 19:13