Scroll Layout 0
A Layout is a ViewGroup that defines the structure for a user interface in your application, such as in an activity .
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:
You can declare a layout in two ways:
- (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.
- Instantiate layout elements at runtime. Your app can create View and ViewGroup objects (and manipulate their properties) programmatically.