..


Sponsored Links

Widgets and View: Some observations

As for the sample project, we analyzed the contents of all files except for the manifest.xml file, this is because the explanation of the code requires the knowledge that, at this point of the guide, the reader has not yet, for this reason I chose to postpone the discussion of the issue. For the moment, therefore, concentrate on android.widget package.

The package android.widget

This package contains all the graphics that you can insert into a View and thus contain elements such as: TextView, EditText, Button, and much more.
To create a custom widget, just create a class that extends the View class or a subclass.

Every element belonging to the package as the superclass has android.widget the View class that is nothing but an abstraction of what the user sees on the screen.

Within a window you can insert more elements descended from the View class and these elements will be incorporated into a sort of hierarchical tree that identifies the final structure of the window.

What can I do about these items?

The operation is performed more frequently is that of the set of properties: for each graphic that you can customize the size, the text content, the position within the window and much more.

Another operation commonly is done is to attach a listener to a particular element. A listener is an object that is able to "listen" waiting for an event generated by the user (a very common practice, for example, is to associate a listener to a button to capture the pressure of it and the user can perform a particular action).

The source code to associate a listener with a graphic will be shown several times throughout this guide.

Hide items from view of the user

In a graphical user interface can often be useful to hide certain elements to the user's view and appear only when certain conditions are met. For example, in an application where the user is prompted to fill out a form, you may hide the button "Send Form" and make it appear only when all fields are completed.
To hide a graphic element is to invoke the method suffiente setVisibility (int visibility) from the object that you want to hide.

This practice of hiding elements is widely used, but we recommend the reader not to abuse this technique when the elements, even if not visible, however, are instantiated and thus reside in memory. This implies a waste of memory, in some cases, is not negligible. Therefore it is recommended to instantiate objects only when really necessary and use the practice previously presented sparingly.

Making an object "editable"

Let us now view some of our reflections on the project starting, as you recall, printed on the screen simply a string of greeting. The view, and in particular the TextView object which contained the greeting string, turns out to be a "static object." This title is an object that can not be changed during application execution and therefore will always contain the same value which was initialized.

Obviously, an application, we will need to change the value of a given object, for example, due to some event generated by the user, in order to achieve this behavior, you must:

  • main.xml in the source file must be added as properties of a unique id (which will be assigned automatically upon filling) that identifies the object;
  • in the source file. java is necessary to instantiate an object of the same type that we declared in the file with the id main.xml and make some sort of connection between the two by calling the method findViewById (R.id.nomeOggetto) (as objectName is the name given object in the file main.xml).
In the next chapter we will show a practical example of the above.

Help develop applications for Android
E-Learning
Front Page (Ebook) Front Page (Ebook)
Create Web pages without knowing HTML. Just 25 €.
Java (Course) Java (Course)
OOP Programming in Java SUN. From 49 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Sponsored Links