What is a View on Android?

9

Android visual components such as EditText , Button , and others have Listeners to handle events triggered by user actions.

Thus, in the method corresponding to the event, it is always necessary to pass a View as a parameter. Here's a small example:

Button btn = (Button) findViewById(R.id.botaoMsg);
btn.setOnClickListener(new OnClickListener()
{
    @Override
    public void onClick(View view)
    {
        EditText edtMsg = (EditText) findViewById(R.id.edtMsg);

        String msg = edtMsg.getText().toString();

        if (!msg.trim().isEmpty()) Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
        else Toast.makeText(getApplicationContext(), "Digite uma mensagem!", Toast.LENGTH_SHORT).show();
    }
});

Note that in the onClick() method the view variable was not used, and in the findViewById() method you are looking for a EditText that might be a View.

This is where my doubts arise regarding the View class and the View on Android.

Questions

  • I have always seen a View as the representation of the entire graphical interface of an application, however, in this case View does not seem to take on this role, so I'd like to know what a View is on Android? >
  • What is the purpose of class View ?
  • What is the importance of this class in relation to the visual components of Android?
  • asked by anonymous 16.09.2017 / 01:57

    3 answers

    10
      

    What is a View on Android?

    The concrete definition is even class, as you can see in android source code :

    public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
        ...
    

    This class actually represents an element of the screen. It is a piece of graphical interface that the user sees. It occupies a rectangular area and is responsible for drawing the component on the screen as well as responding to your events.

    The View is just the base class of a class hierarchy for the various types of visual elements we see in other applications, such as the Button or EditText you indicated.

    Looking at a diagram of these classes becomes even clearer:

    ThepurposeofthehierarchyistobeabletogivebehaviorandvisualaspectmorespecifictosubclassesofView.

      

    WhatisthepurposeoftheViewclass?

    IngeneraltermsIalreadyansweredintheabovepoint,andinrelationtothecodethatpresented:

    @OverridepublicvoidonClick(Viewview){

    Theviewreferstotheelementthatwasclicked,whichinthiscasecorrespondstothebuttonwhereOnClickListenerwasset,this:

    Buttonbtn=(Button)findViewById(R.id.botaoMsg);

    Thisallowsyoutodomorespecificthingslikeregisteringthesamelisteneronvariousbuttonsandwithinlistenerfindwhichwasclickedreferringtoidthroughview,withview.getId().

    Inmostcasesthisparameterwillnotbeused.

      

    Whatistheimportanceofthisclassinrelationtovisualcomponents  ofAndroid?

    Thisclassrepresentsthevisualcomponents!

    Documentation for the Android View class

        
    16.09.2017 / 03:05
    3

    As the documentation says, View is the most basic class for building visual components from simple to complex. In a nutshell, View is a rectangle that responds to user interactions.

    EditText , Button , ConstraintLayout , and all other visual components on Android are children of the most basic graphical class, View .

    The view term in the Android context may still refer to the graphical interface as a whole, but should not be confused with the android.view.View class.

    The View class still inherits from yet another more basic class, Object , which is the basis for all other classes. Although it does not inherit explicitly, if you create a new class, the instances of the objects of this class will have the methods ( toString , equals , etc) of Object .

    In a simple and representative way, we have:

    SomelinksIfoundinterestingtoread:

    16.09.2017 / 05:34
    3
      

    What is a View ?

    Nothing more and nothing less than a simple rectangular box that responds to user actions.

      

    What is the purpose of View Class

    It represents the blocks where the interface components will be built. For example, this is the base class for widgets , which in turn is used to create UI with interactive components, such as buttons, text fields, and more.

      

    How important is this class?

    Very important, without it we have no visual component :).

        
    28.12.2017 / 13:23