format main.xml file - android

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.

Related

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.

com.facebook.widget.loginbutton error in xml

I know as soon as I post this question on stackOverflow it is going to be marked as duplicate but trust me I have tried every possible solution discussed under the same topic in StackOverflow.
I am trying to add Facebook SDK in my project . After adding it under my project->properties->Android (then add facebook adk as library) , when i try to create my xml file and use it shows me red cross next to it and no matter what i do I am not able to make this error go away. Please help me..Anything help is appreciated!!
The 'Unbound Prefix' error is due to having a custom namespace in your xml that is not accounted for.
You need to add another prefix to the top of your file. It should be similar to what I've wrote below, but with PREFIX replaced by the prefix you are using.
xmlns:*PREFIX*="http://schemas.android.com/apk/res-auto"
So, in the end, your file should look something like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.facebook.widget.LoginButton
android:id="#+id/connectWithFbButton"
style="#style/com_facebook_loginview_default_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
android:text="#string/connect_with_facebook"
fb:login_text="#string/connect_with_facebook"
fb:logout_text="Connecting with facebook" />
</LinearLayout>
In this example I've added the prefix 'fb' and I have referenced it when using the fields login_text and logout_text :)
use
FacebookSdk.sdkInitialize(getApplicationContext());
before
super.onCreate(savedInstanceState);
Keep both library and project in the same workspace.
try to deleting this two lines
fb:login_text="#string/connect_with_facebook"
fb:logout_text="Connecting with facebook"

How do I avoid typing "android" for every attribute?

I am new to Android development and am also a designer doubling as a front-end guy.
How can I avoid having to type android: every time I add an attribute?
In other text editors (TextMate), I can trigger snippets by typing a known trigger and tab to auto-complete the word. Is there something similar for Eclipse?
Just begin typing the second part of the keyword (what follows after android: and by pressing Ctrl + Space, Eclipse will insert the desired keyword for you prefixed with android:.
You can bring up the auto-complete dialog by pressing Ctrl+Space at the start of a new line.
Furthermore, I wanted to add a note about why you need to type android: at all (and how you can change it to whatever you want).
If you look at the first element in your layout file, there will be a line like this:
xmlns:android="http://schemas.android.com/apk/res/android"
If you change the word android here, then that will be the prefix you need to type - so if you instead set
xmlns:a="http://schemas.android.com/apk/res/android"
then all you attributes need only be preceeded by a:, such as in this example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:orientation="vertical" >
<TextView
a:layout_width="fill_parent"
a:layout_height="wrap_content"
a:text="#string/hello" />
</LinearLayout>
Use Ctrl+Space first. Then start typing anything. Use arrow keys to go through the list and Enter to select one.
:-)

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

How to format Android xml in Eclipse?

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.

Categories

Resources