Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build a Simple Android App (2014) Basic Android Programming Accessing Views in Code

Walter Bueso
Walter Bueso
10,654 Points

Not fully grasping what "Views" are.

I understand that casting specifies the type of "View" that findViewById returns, but I'm not fully comprehending what a "View" is. Can someone explain it to me? Thank you.

2 Answers

Seth Kroger
Seth Kroger
56,405 Points

A "View" in the general sense is what something looks like. In Android a View describes an area of the display/screen and what should be shown there (and in some cases, how to respond to user input). There are different kinds of Views, all descended from the View class, depending on what needs to be done. It could be in image, text label, button, etc. You can have interactive views like an input field. You can also have Views inside other views, so you can specify the layout of a group of buttons, a text caption below an image, etc.

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Walter

Every element you place inside your XML file is a View. Because of a technique called inheritance every element (Button, EditText, TextView etc.etc.) extend the original code from the View class and supercede this to make a new class with unique methods while still retaining all the old methods that are common across all views.

for example you create a class called Cat which has methods and variables common to every cat like eat, sleep, drink etc etc. You could then create new classes that extend the cat class such as Lion, Tiger and HouseCat which adopt new methods such as Roar which are not applicable to cat as they are unique to the Lion but methods such as Eat, Sleep and Drink would still be available as they would be used by all cats (strange example but one I was told once upon a time which does kind of work).

So basically View holds methods that are common across all views regardless of type things such as position and size will have to be defined in all views, while button will still need a size and position it will also create new methods such as onClick which extend the View class further to make a more 'defined View' which you know as Button.

android's documentation on Views is here - http://developer.android.com/reference/android/view/View.html

Hopefully this makes sense Daniel