How to format Android xml in Eclipse? - android

The GUI designer for Android in Eclipse spits out this type of XML (into a layout):
<Button android:layout_height="wrap_content" android:id="#+id/btnSaveMessage" android:layout_width="wrap_content" android:layout_gravity="right" android:text="#string/text_save" android:width="125dp"></Button>
Is there a refactoring in Eclipse to turn this goo into pretty formatted XML like this:
<Button android:layout_height="wrap_content"
android:id="#+id/btnSaveMessage"
android:layout_width="wrap_content"
android:layout_gravity="right"
android:text="#string/text_save"
android:width="125dp"/>

Press ctrl + shift + F
or
Press ctrl + I

Yes you can do this.
For doing so follow the steps
Select the source code in xml file.
Right click on selected source code.
Now go the Source->cleanup document.
Now a cleanup window is popup, mark check for appropriate options and click ok.
you can also choose source->format to do some formatting in xml. cleanup provide some other fuctionalites besides format.

Related

How to apply TAB on several lines in Android Studio

I am wondering if there is a shortcode (MAC) to tab several lines (see example below).
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:id="#+id/textView"
android:layout_column="0"
android:layout_weight="1"/>
->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:id="#+id/textView"
android:layout_column="0"
android:layout_weight="1"/>
I looked through the command list, but couldn`t find something. Maybe there is another way to do so.
Sure. Press (and keep pressed) the alt-option command on your Mac, then click with the mouse/touchpad and move upwards/downwards. The effect is the following:
(source)
Select several lines, then press tab to indent them or press Shift + tab to unindent them. This works at least on OS X V10.11.6 with Android Studio V2.1.2. Hope this helps you or people who read this question. Thanks.
To tab shift multiple lines in android studio editor:
Select lines ( SHIFT-DOWN/UP ) and press TAB key
Shift TAB does the same backwards.
You can try with CMD+Shift+L (auto indent)

Eclipse/ADT xml string to strings.xml automatically?

If I have the following in a layout XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="string Test" />
it flags the 4th line "Hardcoded string should use #string resource"
Fine, I know that, and would like to. Is there an option to select this line and have the IDE automatically add a <string></string> to the strings.xml and replace the android:text= to refer to that resource?
Yes, there is an option to do this by selecting the text and have the IDE automatically do the job, just thank to JAL :
How to add string resources in Eclipse?
By the way, in JAL's answer, when he says GO TO REFACTOR, he means REFACTOR in the menu bar, not right click's context menu.

Incorrect line ending: found carriage return (\r) without corresponding newline (\n)

I received error in my xml file. "Incorrect line ending: found carriage return (\r) without corresponding newline (\n).
<Button
android:id="#+id/btn_login"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/et_un"
android:layout_marginBottom="15dp"
android:layout_marginRight="22dp"
android:text="Login"
android:textColor="#AA0000"
android:textSize="10pt" />
How to solve it. can anyone help
Using Eclipse on Windows?
Open layout file
Ctrl+Shift+F (Command+Shift+F in MacOSX)
Save the layout file and close it.
Delete any .out files the builder might have created.
Clean project.
It looks like stupid thing, But It worked for me
select all (ctrl+a)
cut (ctrl+x )
Paste it again (ctrl+v)
Save it (ctrl+s)
It really worked for me.
It's easier than other answers:
Ctrl + 1 -> Fix lines
;)
Yesterday, I got this very same error message.
My fix is by simply removing every newline after
/>
So, if you have
<Button
/>
<Button
/>
change it into
<Button
/>
<Button
/>
Might not be the best solution.
If you happen to get this annoying error from time to time when you open an android activity xml file just press ctrl+shift+f to reformat your text and you’re good to go!
To fix this in one of my layout XML files, I did the following from within ADT:
right-click on the file in the Package Explorer
Pick Open With -> Text Editor
Now the offending line gets highlighted with a big red X.
I then deleted the line endings (combining the two lines) and then hit "return" to add it back in. In some cases I found that didn't quite work so I copied a similar line that didn't have the issue, and pasted it over top of the offending line (highlight the bad line, then paste). Sometimes it seemed that the real issue was with the line before or after it.
Then after saving it would show up the next bad line, so lather-rinse-repeat until it's all done.
When all done, use right-click, Open With -> Android Common XML Editor to get back to the visual design view.
(ADT v21.0.1, on Mac OSX Lion)
Close the xml file in the Eclipse
open it in text editor (notepad or notepad++ and so forth..)
save it.
open it back in the eclipse
In menu->Project->clean project.
Just clean the project. In Eclipse go in project menu:
Project-> Clean
It will resolve the problem.
Cut your whole code, save the xml, paste the code again in the file and save. It also solves at times. Though not a standard way, but works.
clean the project Project -> clean
worked for me
First refresh the project then clean (Ctrl+shift+F).
This solves the problem for me.
open layout file
if any layout is freezed that freezed layout delete
3.ctrl + A
4.ctrl + shift + f
save
it will work

Code changes in Android's layout XML

I have a problem where my code automatically changes while I'm clicking or moving the cursor somewhere. This happens in a layout XML.
I use Eclipse Juno and I inserted an ExpandableList. My min sdk is 14 and max is 16.
It seems though that only the .xml part changes and not the layout. The .xml part, if opened again, is ok, but it does the same thing.
Helpz?
EDIT
I also noticed that the expandable list does not show up when I run the code(it just calls the layout)
The XML file:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ExpandableListView
android:id="#+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1" >
</ExpandableListView>
Do you mean the code visually changes to other lines of code already in your XML file? Eclipse does this to me every now and then. Not sure why (I'm guessing it might be a bug with Lint) but usually closing and re-opening Eclipse fixes it for me.
From what I remember the code actually isn't changing, just what's being displayed.
try to align the code using ctrl + shift + f, and if the problem stod, reopen the xml file, and at last try to close eclipse and open it again.

format main.xml file

Hello,
i would like to know shortcut key to format my xml tag
for ex: (main.xml) when i drag and drop,code display in horizontal line like::
<LinearLayout android:id="#+id/chart" android:orientation="horizontal" .. . . . . .... />
but i would like to know shoutcut , so i press it and display
like ::
<LinearLayout android:id="#+id/chart"
android:orientation="horizontal"
.. . . . . .... />
Thanks
nik
select the XML code that you want to format and press CTRL+I
select the XML code that you want to format and press CTRL+SHIFT+F.
Eclipse:
If you want to set up formatting options - use menu Window->Preferences->XML->XML Files->Editor. But before make sure that "Eclipse XML Editors and Tools" are installed (as plugin which is a part of WST (subproject of WTP))
I don't think so to select xml code or java code to format it properly. By simply pressing CTRL+SHIFT+F or CTRL+I you can properly reformat your code.
Press CTRL+SHIFT+L to shows all the command supported by Eclipse.

Categories

Resources