0

When I try to format code in Eclipse Helios, by pressing ctrl+shift+f, the code is getting formatted, but the results are very ugly. Two or three statements in one line, for example. Indentation is also very poor.

For ex: After formatting the code it looks like:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent" android:background="@drawable/scannerbg">

 <include android:id="@+id/headerLayout"
  android:layout_alignParentTop="true" layout="@layout/headerlayout" />

 <ListView android:layout_below="@id/headerLayout"
  android:layout_height="fill_parent" android:layout_marginTop="5dp"
  android:listSelector="@android:color/transparent" android:id="@+id/listView"
  android:layout_width="fill_parent">
 </ListView>

</RelativeLayout>

As you can see in ListView, two to three statements are there in single line.

Can anyone offer a solution to this?

alex
  • 10,900
  • 15
  • 70
  • 100
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61

2 Answers2

3

In Eclipse, from the Window menu select Preferences. Expand the Android node and then select the Editors subnode. On the right pane ensure the "Format XML files using the standard Android XML style.." (the first option) is checked, and maybe other option(s).

Ctrl+Shift+f and/or Ctrl+i should work as expected and give these result:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/scannerbg"
    android:orientation="vertical" >

    <include
        android:id="@+id/headerLayout"
        android:layout_alignParentTop="true"
        layout="@layout/headerlayout" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/headerLayout"
        android:layout_marginTop="5dp"
        android:listSelector="@android:color/transparent" >
    </ListView>

</RelativeLayout> 
Chilledrat
  • 2,593
  • 3
  • 28
  • 38
Aladin Q
  • 550
  • 5
  • 12
  • I could not found "Allow single attributes to appear on the same line as their elements" under Editors tab. – Shrikant Ballal Aug 29 '12 at 10:57
  • I quickly edited my answer when I realized the big typo while at same time you were reading it ;-) – Aladin Q Aug 29 '12 at 10:59
  • Oh sorry, but I could not found this "Format XML files using the standard Android XML style" also. There is nothing like this in Editors tab – Shrikant Ballal Aug 29 '12 at 11:01
  • Just below the "General" node in the Preferences, you should have an "Android" node for Android SDK settings. Is the Android plugin for eclipse properly installed? – Aladin Q Aug 29 '12 at 11:04
  • There is only single checkBox asking "Autmatically format the xml edited by the visual layout editor". – Shrikant Ballal Aug 29 '12 at 11:13
  • Hope you will find a solution. FYI, I'm using Android Development Toolkit Version: 20.0.3.v201208082019-427395 – Aladin Q Aug 29 '12 at 11:19
1

Use ADT version 20 for perfect formatting.

Shrenik
  • 399
  • 2
  • 5
  • 22