What are android Views?

A View occupies a rectangular area on the screen and is responsible for drawing and event handling. The View class is a superclass for all GUI components in Android.

<View
android:id="@+id/sample_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

View Group

A ViewGroup is a special view that can contain other views (called children). View Group is a nesting of views inside a view that is called view grouping.

<View
android:id="@+id/sample_view_group"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<View android:id="@+id/sample_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

<TextView
android:text="Text view "
android:layout_height="100dp"
android:layout_width="56dp" />
</View>

Commonly used Views are:

  • EditText
  • ImageView
  • TextView
  • Button
  • ImageButton
  • CheckBox etc

Common attributes that are applied in all views

id: These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree. View IDs need not to be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching for.

Syntax:

android:id="@+id/my_id"

height & width: As the name itself suggests, both of these attributes describe the height and width of the given view.l These are necessary attributes for every view in an XML file.

Syntax :

android:layout_width="match_parent"
android:layout_height="wrap_content"

    match_parent means that the view wants to be as big as its parent (minus padding)

    wrap_content means that the view wants to be just big enough to enclose its content (plus padding).

Ideally, values should be in "dp" i.e density-independent pixels

Padding & Margin

Padding is the space inside the border, between the border and the actual view's content. Note that padding goes completely around the content.

Margins are the spaces outside the border, between the border and the other elements next to this view.

<TextView
....
android:layout_margin="10dp"
android:padding="20dp"
/>

 

 

.



×