3

I know java basics and I am learning Android development now. I have learnt that there are two central android classes for the ui, View and ViewGroup. I want you to correct me wherever I am wrong.

ViewGroup is a subclass of View and holds View objects together. TextView, EditText, etc are subclasses of View. LinearLayout, Gridview, etc are subclasses of ViewGroup.

I want to relate this to the OOP concept of java but I don't seem to get all this. Where are the objects? How come a subclass is a container?

Atif AbbAsi
  • 5,633
  • 7
  • 26
  • 47
garfield
  • 89
  • 7
  • 1
    it's a [composite patter](http://en.wikipedia.org/wiki/Composite_pattern) – Blackbelt Apr 01 '15 at 14:50
  • look at how you define view http://stackoverflow.com/questions/27352476/difference-between-view-and-viewgroup-in-android – barlop Apr 01 '15 at 14:55

1 Answers1

0

I'll give you a basic overview here. I'm also pretty new to Android, so the correct-me-if-I'm-wrong thing goes for me too. :)

A View is basically a unit of UI; like, say, a box of 24x24 pixels (yes, THAT basic). Now this box can be used for anything, because it is the top-most entity in the hierarchy. We can define it more precisely by specifying what we want it to hold, this is where TextView, ImageView, WebView, etc. come in. So this box 24x24 may hold text, an image or a web-page content, respectively.

A ViewGroup can belong in the 'etc.' above. Just as for showing text, the 24x24 box can be used to display 4 boxes 12x12. If so, the View can be classified as a ViewGroup.

Further, when we know what type of arrangement we require the box to hold, in this case, we can further classify it as any of LinearLayout, RelativeLayout, GridLayout, FrameLayout, etc.

In such a hierarchy, an entity can have a child object of any kind, even an instance of its own class.

You may even say that View is the 'Object' of UI.

I hope I have helped you.

Comments/edits welcome. :)

SlashG
  • 671
  • 4
  • 17
  • In general, the XML vocabulary for declaring UI elements closely follows the structure and naming of the classes and methods, where element names correspond to class names and attribute names correspond to methods. In fact, the correspondence is often so direct that you can guess what XML attribute corresponds to a class method, or guess what class corresponds to a given XML element. However, note that not all vocabulary is identical. In some cases, there are slight naming differences. For example, the EditText element has a text attribute that corresponds to EditText.setText(). – garfield Apr 02 '15 at 05:51
  • Fantastic! Congratulations! – SlashG Apr 02 '15 at 11:23