All the interaction of a user with the Android application is through the user interface(UI), hence it is very important to understand the basics about the User Interface of an android application. Here in this tutorial, we are going to cover about various
ViewGroups and will try to explain how they can be used to design the User Interface of an android application.
View is the basic building block of UI(User Interface) in android. View refers to the
android.view.View class, which is the super class for all the GUI components like
View class extends
Object class and implements
View can be considered as a rectangle on the screen that shows some type of content. It can be an image, a piece of text, a button or anything that an android application can display. The rectangle here is actually invisible, but every view occupies a rectangle shape.
The question that might be bothering you would be , what can be the size of this rectangle?
The answer is either we can set it manually, by specifying the exact size(with proper units) or by using some predefined values. These predefined values are
match_parent means it will occupy the complete space available on the display of the device. Whereas,
wrap_content means it will occupy only that much space as required for its content to display.
A View is also known as Widget in Android. Any visual(that we can see on screen) and interactive(with which user can interact with) is called a Widget.
Now, as we have explained earlier as well, to draw anything in your android application, you will have to sepcify it in the design XML files. And to add functionality we will create Java files.
Every view in XML has the following format:
<ViewName Attribute1=Value1 Attribute2=Value2 Attribute3=Value3 . . AttributeN=ValueN />
View subclass needs to follow this format so that it can appear on the screen of the app. And this format is nothing but default XML style. Right!
There are two attributes that are necessary for every
View. These are:
These attributes define the size of the invisible rectangle that a view makes. Using these attributes we can easily control the size for every view in our android application.
Apart from the above mentioned attributes, attributes like
margin are some other commonly used attributes.
Here we have some of the most commonly used android
To create/define a View or a ViewGroup in your android application, there are two possible ways:
Button myButton = new Button(this); myButton.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); myLayout.addView(myButton);
addView() is the function used to add any View to the UI and
setLayoutParams() function is used to set the various attributes.