Introduction to Android Views and ViewGroups.

Hello Android lovers, this article is all about android views and view groups. If you want to create your own application then it is very important to learn because it is very basic about UI(User interface).

so let’s start……

What is View?

What is View in android

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.

<ViewName
    Attribute1=Value1
    Attribute2=Value2
    Attribute3=Value3
    .
    .
    AttributeN=ValueN
/>

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.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    ... />

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.

Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.MATCH_PARENT,
                                        LinearLayout.LayoutParams.MATCH_PARENT));

myLayout.addView(myButton);

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.

How to install Android Studio on Windows.

Also Read :-

If you like this article then share with your friends and keep visiting Smgplaza for the latest android related articles. Thank you for your love and support.