Reputation: 669
What is the difference between a View
and widget in Android?
Upvotes: 39
Views: 17683
Reputation: 12219
As is stated in the View
class docs:
This class represents the basic building block for user interface components. A
View
occupies a rectangular area on the screen and is responsible for drawing and event handling.View
is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).The
ViewGroup
subclass is the base class for layouts, which are invisible containers that hold otherView
s (or otherViewGroup
s) and define their layout properties.
Therefore a View
is a base class for UI elements and a Widget
is loosely defined as any ready to use View
.
Let's emphasize these concepts a little more.
View
A View
is a base class for all UI elements. It, therefore, covers many different classes and concepts, including widgets, ViewGroup
s and layouts. There is a root View
attached to a Window instance which forms the basis of the View
hierarchy. In general, the word View
is usually used to describe UI elements in general, or to refer to abstract or base UI classes such as ViewGroup
s.
There are various definitions for this term, but most refer to a "ready to use" UI element, be it a Button
, ImageView
, EditText
, etc. Note that some people consider widgets to be UI elements that are complete (not abstract) and are not containers (such as ViewGroup
s (layouts/ListViews
)). It's also worth noting that "widget" is a package name (android.widget
) where the docs mention the following:
The widget package contains (mostly visual) UI elements to use on your Application screen.
Therefore, it is reasonable to consider non-visual UI elements to also be widgets, as well as any class defined under the widget package. See here for a full list of classes in the widget package: http://developer.android.com/reference/android/widget/package-summary.html
Not to be confused with a UI element widget, an App Widget is a remote View
hierarchy which is most commonly displayed on the user's home screen. As defined by the docs:
App Widgets are miniature application views that can be embedded in other applications (such as the Home screen) and receive periodic updates. These views are referred to as Widgets in the user interface, and you can publish one with an App Widget provider. An application component that is able to hold other App Widgets is called an App Widget host.
ViewGroup
A ViewGroup
is a subclass of View
and provides the ability to parent and position child View
s, such as in the case of Layouts.
Much as with Widgets, there is no Layout base class, therefore it could be loosely defined as any class which extends ViewGroup
and provides the ability to define the positioning of child View
s within it. Usually, only ViewGroup
subclasses which are appended with the word "Layout" (as in LinearLayout
, RelativeLayout
) are referred to as "layouts", other classes extending ViewGroup
are usually just referred to as "view containers".
Finally, I'd like to suggest that whenever you mention Views, widgets or any other important term, to make it clear your intended definition so that people can better understand what you're referring to.
Upvotes: 30
Reputation: 89
Lets come to the clear differences, without gigantic description..
Now you can think like , view is a container or place holder and widget is independent controls that you can place in any view.
But keep in mind these are more like design concepts, not hardcore definitions.
Finally, it can be concluded like,
For more explanation, have a look on basic code below:
<!doctype html>
<head>
<title>New jQuery UI Widget by souro</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$(function() {
$.widget("iP.myButton", {
_create: function() {
this._button = $("<button>");
this._button.text("My first Widget Button");
this._button.width(this.options.width)
this._button.css("background-color", this.options.color);
this._button.css("position", "absolute");
this._button.css("left", "100px");
$(this.element).append(this._button);
},
});
$("#button1").myButton();
});
</script>
</head>
<body>
<div id="button1"></div>
</body>
</html>
Now you can see myButton is a small scale independent view i.e., widget and it is getting placed in a container button1 i.e, view, and yes you can put this widget in any other view i.e., purpose of widget. Make sense i guess.
Upvotes: 4
Reputation: 2719
Widget is package in Android which contain all user interfaces such button, textView and layout,etc. but view is an abstract class which includes properties, focus and event handling method ,rendering ,etc
But all widgets extends the view for getting UI with it behavior such as properties, focus, etc.Thus all widgets are examples of view, but view is not the same as a widget. A View-group acts as container which contains different views examples are the frame-layout, relative-layout. They extend the view group and have as purpose as acting like a container which adds specific behavior to all the views it contains.
Upvotes: 8
Reputation: 3319
A Widget is a View. A Layout is a ViewGroup. To create a widget, you extend a View.
Upvotes: 15
Reputation: 89
view is super class of widget so that a widget is a kind of view. In "pro android 4",the author take them as the same thing.
"View,widget,control Each of these represents a UI element.Examples include a button,a grid,a list,a window,a dialog box,and so on.The terms view,widget,and control are used interchangeably in this chapter."
Upvotes: 0
Reputation: 1265
I don't believe any of the earlier answers are really correct. Different UI toolkits use different terminology for what is often essentially the same thing, and it is true that an Android View
is pretty similar to a .NET Control
or a Qt widget.
But "widget" also means something very specific in Android. If you long press on the home screen, you get an "Add to home screen" dialog that offers to add Widgets (among other things). If you touch the Widgets line, you get a menu of widgets like Analog Clock 1 through 4.
According to developer.android.com/guide/topics/appwidgets, these are special entities that lets your process display content in another process's View. Like the home screen. There is a special broadcast protocol that you inherit for free, but you also interact with the Views in your developer.android.com/reference/android/widget/RemoteViews.html host via a special protocol that appears to involve marshaling. I have just started to look into writing one (which is how I found this question) but the docs do say that you can only use FrameLayout
, LinearLayout
, and RelativeLayout
layouts, and that they can only contain AnalogClock
, Button
, Chronometer
, ImageButton
, ImageView
, ProgressBar',
TextView, and
ViewFlipper` "widget classes" - "Descendants of these classes are not supported."
This is just a bit restrictive!
Upvotes: 5
Reputation: 64261
Just some quotation from "The Busy Coder's Guide to Android Development", Ch 6:
All widgets, including the ones shown above, extend View, and as such give all widgets an array of useful properties and methods beyond those already described.
Upvotes: 1
Reputation: 15267
I had your same confusion about it, above all for the two packages android.view
and android.widget
. I ended up thinking of widgets as "ready to use" views, and views as just elements to build widgets. If I make a custom view that will be directly used in a layout, I would consider that a widget.
Upvotes: 1
Reputation: 1216
Views are concerned with layouts, placeholders.
Widgets are concerned with data, UI behaviors (ex Sliders). Views contain widgets.
Upvotes: 1