Android - Commenting out lines in XML
I find that adding little notes to myself in my code is very helpful sometimes. It means that I can write pseudocode directly into AndroidStudio before I start coding and means I have reminders of what each line of my code means in case I forget later.
To do this you need to write what are called “comments”. These are lines within your code that are “ignored” by the computer and therefore can contain anything.
I was not sure how to write comments in XML so I looked it up (link below.)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.courtcounter.MainActivity">
<!--This TextView creates some text "Team A" which is centered horizontally at the top of the page-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Team A" android:gravity="center"
android:layout_margin="10dp" />
</LinearLayout>
The comment is the green-highlighted part in the middle that says:
<!--This TextView creates some text "Team A" which is centered horizontally at the top of the page-->
Notice how this line starts with <!-- and ends with -->
These are the key symbols to have at the beginning and end of any lines in your code you don’t want the computer to read. In words these symbols are greater than symbol, exclamation mark, hyphen, hyphen at the beginning and hyphen, hyphen lesser than symbol at the end.
I find that as well as writing notes to myself or other coders using these “comments” it can also be helpful in other ways. For example, if I am experimenting with some new types of code or trying out various approaches I can temporarily comment out lines of code I don’t want for a moment. This is better than deleting these lines as I don’t have to remember what I wrote or where it was, I can just remove the <!-- and --> symbols from that line if I decide I do want that line of code. I also find it useful for debugging, because I can comment out any lines of code that I think may be causing the error to see if the program works once the potentially offending line is commented out.
I was helped in working this out by this article here: