Android Views
Let's know about Android views
Ashwini Kumar
Nov. 7, 2022 Views: 113
Android Views
The main building block for the user interface (UI) of an app like the video player you are preparing to develop is creating a View.
What are Views?
The View object is created from the View class and occupies a rectangular screen area. It is responsible for the processing of drawings and events.
The View is the base class for widgets that are used to create interactive components of the user UI such as buttons or text fields.
In summary, a View can be considered a rectangle on the screen that displays a certain type of content, like an image, a piece of text or even a button!
In Android, Views are one of the most important building block of the UI(User Interface) as it contains all the components related to the User Interface. Which contain how the app will look.
Most used Android Views
Some of the most used Android Views include:
- TextView
- EditText
- Button
- ImageView
- ImageButton
- CheckBox
- RadioButton
- RecyclerView
- DatePicker and
- Spinner
View sizes
A View occupies a rectangle shape, although the rectangle is invisible.
The size of this rectangle can be manually set by specifying the exact size (with appropriate units) or using some predetermined values. These predefined values are match_parent and wrap_content.
match_parent means it will occupy all of the available space available within its parent ViewGroup, whereas wrap_content means it will occupy only as much space as required for its content to display.
XML syntax for creating a View
Now, as you learned previously, to draw anything in your Android application you need to specify it in the design XML files. You will create Kotlin files to add functionality.
Every View in XML has the following format:
<ViewName
Attribute1=Value1
Attribute2=Value2
Attribute3=Value3
AttributeN=ValueN
/>
It always starts with an angle bracket, followed by the View name.
- You then write attributes that will determine what this view will look like on the app screen along with a value for the attribute. Each view has its own unique attributes.
- In the end, it is closed by a forward slash and an angle bracket.
So, every View subclass needs to follow this standard so that it can display on the screen of the app. This format is the default standard of XML.
Take note that there are two attributes that are required for every View. These are android:layout_height and android:layout_width.
These attributes determine the size of the invisible rectangle created by the view. Using these attributes, you control the size of every view in your Android app.