XML layout Files in Android Studio

XML layout in android studio

Through the previous article we learned the activities and fragments in android studio. In both activities and fragment we used two types of files. The first is for designing the UI which is XML layout file, and the other is for writing the excitable java code. So, in this article it is important to know what is XML files and the language it works with.

What is XML?

XML stands for Extensible Markup Language. The World Wide Web organization created it to construct a syntax in the way that the machine and human can read. Moreover, XML dose this by using tags that define the structure of the layout files. Further more, It is extensible meaning that the user can create their own markup symbols to design their layout.

How to design an XML layout?

With in a series of nested elements, you can use android’s vocabulary to design UI layout and screen elements. Moreover, Each layout file must contain at least one root element. Also, this root element must be View or ViewGroup. After that you can add additional views or layout object as child elements. The next example, shows an xml file with parent relative layout with a text view (widget) as child view.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" 
        android:layout_centerInParent="true"/>

</RelativeLayout>

How to load XML resource

The android app works by compiling the xml file into a view resource. In order that we should load the layout resource in the java code in oncreate() method implementation. We can do that by calling the method setContentView() and passing the layout resource file in it.

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        }

Layout Attributes

Any view or view group has their own xml attributes. Some of these attributes are specific to the view object like text size of text view. Moreover, some of them are related to all views because they are inherited by the root view, for example, layout_id. Other attributes are considered as layout parameters that describe layout orientations of the view object.

Layout ID

Layout Id specifies the unique instance of each view in the same XML file. That means we can not repeat the id in the same file. In order to specify the id of the view we use (android:id=”@+id/name”). Furthermore, the plus (+) sign here is to add the name id and it differs from the (android:id=”@id/name”) which is used to refer a specific id already created in the XML file.


Text View in Android Studio

Learn more about Layouts in android studio

Related posts

Leave a Comment