What is a View on Android?

Asked

Viewed 2,941 times

11

The visual components of Android as EditText, Button and others, possess the Listeners to handle events triggered by actions performed by users.

Therefore, in the method corresponding to the event, it is always necessary to pass a View as parameter. See 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 method onClick() the variable view and in the method findViewById() is looking for a EditText that might be a View.

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

Doubts

  1. I have always seen a View as the representation of the entire graphical interface of an application, however in this case the View does not seem to assume this role, so I would like to know what is a View on Android?
  2. What is the purpose of the class View?
  3. What is the importance of this class in relation to visual components android?
  • 2

    From what I see, it seems to be just a screen control. In the GWT has the Widget, in Totalcross has the Control and the AWT has the Component. A generic deal that can be plotted on the screen

  • 2
  • 1

    The parameter view in this case is the Button. You could cast him for the type Button and work with it (for example, changing the color of the button when it was clicked).

3 answers

11


What is a View on Android ?

The concrete definition is the same class, as can be seen 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 the graphical interface that the user sees. Occupies a rectangular area and is responsible for drawing the component on the screen as well as responding to your events.

To View is only 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 who indicated.

Looking at a diagram of these classes becomes even clearer:

inserir a descrição da imagem aqui

The purpose of the hierarchy is to be able to give behavior and visual appearance more specific to sub-classes of View.

What is the purpose of the View class ?

In general terms I have already answered in the point above, and in relation to the code you submitted:

@Override
public void onClick(View view) {

The view refers to the element that was clicked, which in this case corresponds to the button where the OnClickListener, this:

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

This allows you to do more specific things like record the same listener on various buttons and inside the listener find out which one was clicked by referring to id through the view, with view.getId().

In most cases this parameter is not used.

What is the importance of this class in relation to visual components android?

This class represents the visual components!

Documentation for the Android View class

  • Da para ver que a classe View é bastante importante para os componentes visuais do Android, e a mesma tem many lines of code Xp

  • @cat 26485 lines of code(including comments) to be precise!

4

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

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

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

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

Simply and representatively, we have:

Some links I found interesting reading:

4

What is a View?

Nothing more and nothing less than a simple rectangular box that responds to the user’s 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 the widgets, which in turn are used to create the UI with interactive components, such as buttons, text fields and others.

What is the importance of this class?

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.