Android Views and ViewGroups

 What is View?

View is the basic building block of UI(User Interface) in android. View refers to the android.view.View class, which is the superclass for all the GUI components like TextViewImageViewButton etc.

Basically view has used to shows some type of content. It can be an image, text, button, or anything that an android application can display. the best thing is every view occupies a rectangle shape.

Now, question is “what can be size of this rectangle” right !

There are two predefined values are match_parent and wrap_content.

1 . match_parent :-

match_parent means it will occupy the complete space available on the display of the device. For example, if you want to set the width of any button match parent then your output will be like below image

2. wrap_content :-

wrap_content means it will occupy only that much space as required for its content. For example, if you use wrap_content on any button then the output will be like the above image.

Basic XML syntax for creating a View:-

Now let’s see the basic XML syntax for creating a view but before that, you need to know every view has the same XML format.

So, every View subclass needs to follow this format so that it can appear on the screen of the app. There are two attributes that are necessary for every View. These are: android:layout_height and android:layout_width.

There are two approaches to create and define a View and ViewGroup in your application.

Programmatic and Declarative Approach:-

1. Declarative Approach:- In this, we define the View and ViewGroups directly in the design XML files, as we will doing in the next article where we will study about various Views classes.

2. Programmatic Approach:- In this, we create our View in the java file. We will also see this approach in detail later. I give you the sample code using that you can understand the basics of this approach.

There are many View classes are available like TextView, EditText,Button,ImageView,ImageButton,CheckBox,RadioButton,ListView,GridVieew, DatePicker,HorizontalScrollView, etc. We can use this all classed by using both approaches. we can see all these classes in our next article so keep visiting our website and press the red bell icon at the right corner for the latest post notification.

Now, if you don’t know how to install Android Studio and you want to learn how to install an android studio on windows then click on the below link.