Hello guys! Welcome to AndroidPlus. In our last tutorial we successfully created our first hello world App and we learned how to run our Android App in emulator as well as on our android mobile. We also learned a little bit about the XML coding. Today, we will see how to add xml file and will learn a little bit about java class. Let’s start with the xml-
- Adding XML File in Layout Folder:
1. Go to resource folder ‘res’ then in ‘layout’.
2. Right click on layout folder then New-->Android XML File.
3. Give the name of the file and select the ‘LinearLayout’ in root element then click Finish.
4. Double click the ‘sample.xml’ in layout folder to open, you can see code like this.
1. Now open the Graphical layout of the XML file and drag a ‘Textview’ into the layout.
2. Double click the Textview to open the XML code of it.
You can see this code the top of the sample.xml file-
<?xml version="1.0" encoding="utf-8"?>
This is a default code which appears each time we create an XML file. You don’t have to remember this code. This just tell us about the xml version and the encoding used in that xml file.
Next, you can see this code-
Here, ‘LinearLayout’ is defined because it holds the other items such as ‘TextView’, ’Buttons’, ‘Checkbox ’ etc.
‘xmlns:android="http://schemas.android.com/apk/res/android’
This is our reference, eclipse set this reference when a ‘LinearLayout’ is defined.
android:layout_width="fill_parent"
android:layout_height="fill_parent”
Here, layout_width and layout_height are declared as “fill_parent”. This means that the LinearLayout would take the full width and height available on the screen.
android:orientation="vertical"
Vertical orientation indicate that other items that are inside the ‘LinearLayout’ would be vertically aligned. Example- Drag another ‘TextView’ into the layout.
android:orientation="horizontal"
this tells the items would be horizontally aligned.
- Adding Java File in the Project:
1. Go to resource folder ‘src’.
2. Right click on your package name then New-->Class.
3. Give the name of the file and then click Finish.
4. Double click the ‘main.java’ in layout folder to open, you can see the main class you created.
5. Now we need to create an activity in the class. Write the code like this-
6. To create the method inside the class, right click inside the class Source-->Override/implement Methods.
7. Now select onCreate (Bundle) method and click ok.
‘setContentView(R.layout.sample)’ sets our sample layout in the screen.
8. Now simply run the code in the emulator or in your android mobile to see the output.
So guys today we understood the XML and had a little introduction with java class. In our next tutorial we will create the form screen using XML and we will learn about the ‘onCickListener’.
Hope you enjoyed this tutorial, In case you get any problem feel free to comment.