Layout in XML


Layout: 

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. 

Declare UI elements in XML:  Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

Instantiate layout elements at runtime: Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

Android Activity doesn’t contain anything of its own. It loads a layout i.e declared in  /res/layout  folder in the project directory.

Layouts are important to your Android applications, which is why Layouts have their very own
layout folder in the standard project resource folder architecture for Android.
Initially android project directory has a activity_main.xml file which defines the elements of the activity. Every class when act as an activity must extends Activity class and override onCreate() method.

This onCreate() method to push it onto your screen on the startup of your application activity.

Lets take a look to the activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <TextView
        android:id="@+id/smalltextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/mediumtextView"
        android:layout_below="@+id/mediumtextView"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

Lets have a look to the code (How to Load the xml layout):

public class MainActivity extends Activity {

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

The advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it without having to modify your source code and recompile.

There are various types of Layouts few of them are:
·         Linear Layout
·         Relative Layout
·         Table Layout
·         Grid View
·         Tab Layout
·         List View


 Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - Smruti@apextgi.com and pratap@apextgi.com

No comments:

Post a Comment