Sobes.tech
Junior

What is ViewGroup and what types of ViewGroup exist in Android?

sobes.tech AI

Answer from AI

ViewGroup is a base class for containers of View, which can contain other View and ViewGroup. It is responsible for their layout and drawing.

Main types of ViewGroup:

  • Layouts: Used for organizing child View.
    • LinearLayout (horizontal or vertical orientation)
    • RelativeLayout (positioning relative to other elements or parent)
    • ConstraintLayout (flexible positioning based on constraints)
    • FrameLayout (overlay View on top of each other)
    • TableLayout (arranged as a table)
    • GridLayout (arranged as a grid)
  • Adapters: Display a set of data in View.
    • ListView (deprecated, used with Adapter)
    • GridView (deprecated, used with Adapter)
    • RecyclerView (modern, high-performance, used with Adapter)
  • Others:
    • ScrollView (scrolls content exceeding screen size)
    • ViewPager (allows swiping between pages of View)
    • CardView (provides card-style background and shadow)

Example of using LinearLayout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"> // Vertical orientation

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First element"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second element"/>

</LinearLayout>

ViewGroup is a key element of the user interface in Android, enabling the creation of complex hierarchies of elements.