Adv

Android Layouts

A Layout is a ViewGroup that defines the structure for a user interface in your application, such as in an activity .

Layouts are normally used as a template or structure to hold other elements. These elements are commonly wigets like button, textview, listview etc.

These elements will make up a hierarchy of views and ViewGroups objects.

The difference between a view and viewgroup is that a view will draw an element the user can interact with while a viewgroup will is just an invisible container for holding other views. Note that ViewGroups can be nested.

So in short a view is a widget while a viewgroup is layout.

Here are examples of Layouts/ViewGroups:

  1. RelativeLayout.
  2. LinearLayout.
  3. ConstraintLayout.
  4. FramewLayout.
  5. CordinatorLayout.

Layout Declaration

You can declare a layout in two ways:

  1. (a) 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.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
              android_layout_width="match_parent"
              android_layout_height="match_parent"
              android_orientation="vertical" >
    <TextView android_id="@+id/myTextView"
              android_layout_width="wrap_content"
              android_layout_height="wrap_content"
              android_text="This is a TextView" />
    <Button android_id="@+id/myButton"
            android_layout_width="wrap_content"
            android_layout_height="wrap_content"
            android_text="Hello, I am a Button" />
</LinearLayout>

After you’ve declared your layout in XML, save the file with the .xml extension, in your Android project’s res/layout/ directory, so it will properly compile.

(b). You can also use Android Studio’s Layout Editor to build your XML layout using a drag-and-drop interface.

  1. Instantiate layout elements at runtime. Your app can create View and ViewGroup objects (and manipulate their properties) programmatically.
    <!–
    [/section]
{{ shortcode.section.layout_declaration }}
Share
Adv



Share an Example

Share an Example

Browse
What is the capital of Egypt? ( Cairo )