Skip to content Skip to sidebar Skip to footer

Commenting In Activity_main.xml

I am new to Android. I understand that commenting in XML works the same as it does in HTML, using the form. I would like to write some comments in my a

Solution 1:

The problem with commenting attributes on xml is that you can't have a comment break the xml tag. So if you have:

<Viewandroid:layout_width="match_parent"android:layout_height="match_parent"
/>

You can't comment layout_width, your comment must be outside of the view tag.

Solution 2:

There are two things forbidden in terms of commenting in XML:

  • double -- inside comments
  • comments inside tags

So this code would generate an error.

<EditText android:id="@+id/edit_message"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:hint="@string/edit_message" 
  <!-- That would use all empty space -->
  android:layout_weight="1" />

And this too:

<!-- This is for -- your name --><EditTextandroid:id="@+id/edit_message"android:layout_width="wrap_content"android:layout_height="wrap_content"android:hint="@string/edit_message"android:layout_weight="1" />

You could read more about comments in Android: http://android4beginners.com/2013/07/appendix-d-comments-in-xml-and-java-its-crucial-to-create-a-documentation-of-android-app/

Solution 3:

It is giving you the error because you are closing your EditText tag in the second line itself.

That's because for commenting out some code, you'd use <!-- -->, so ">" has been taken. and from next line onward, it's not having the starting "<".

For example,

<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_world" />

If you comment out the second line, i.e. layout_width, it will be considered as though you were closing the TextView tag, and next line onward it would not have any starting "<".

Post a Comment for "Commenting In Activity_main.xml"