Adding Views to an Android Viewgroup Programmatically - android

This seems to be a common question yet documentation is very hard to find. I'm looking for examples that show me how to create my own view group (preferably by extending an already existing one) and then add views programmaticly.
Thanks.

ViewGroup
ViewGroup is abstract, and its onLayout is abstract too. So you need to provide an implementation for onLayout where you do assign a position at every child (View) of the viewgroup.

Related

Is it possible to enable touch events for a View inside a ViewGroup but outside its bounds?

I'm currently trying to design a ViewGroup (specifically a subclass of FrameLayout) that can layout any number of subviews and enable them for drag/drop outside the layout group. It's almost identical to a LinearLayout:
Curently I am able to drag the views outside the ViewGroup and draw them, however after letting go of the view it can't be re-selected and further dragged around.
I'm wondering if there is a way to do this in a way that isolates the logic to the Layout subclass and doesn't involve needing to do much/any extra stuff in consuming fragments/view groups.
I've tried overriding getHitRect(Rect) in my FrameLayout subclass but it never seems to be called. dispatchTouchEvent(MotionEvent) is of course not called either, presumably because the parent ViewGroup has decided not to deliver the touch to it because it was outside the bounds. I've tried implementing a TouchDelegate as well, but I think that needs to be set on the parent view, which means needing to know about this and doing this additional step when using this Layout.
Any ideas on if/how this is possible? On iOS it can be implemented fairly easily by overriding hitTest: to take into account the frames of the child views. Is there a similar method like this on Android?
Your help is greatly appreciated!
Without any code it is very hard to see what you are doing, but best guess would be that each view should a child view of the relative layout.

Which is better either Viewgroup or Layout

Am creating a grid in android Now am struck with either to use ViewGroup or Layouts (Frame layout /Linear layout) for creating a container in which the cells will be loaded and it would be helpful if provided with links for detailed learning process.
Thanks in advance.
Use GridView or GridLayout for your requirement.
Viewgroup is derived from views and layouts are derived from view group so better to use layouts.
check gridlayout also

Difference between View and Subview - Android

What is the difference between a view and a subview in Android?
there is no such thing called a 'subview', its just used to refer to a view inside another view.
View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). and if we insert a view inside the another the its become Subview like a linear Layout containing a button view, here button is a subview
Like Vinay said, there is no such a thing. But you have ViewGroup that contains other Views. For example, LinearLayout, RelativeLayout etc are derived from that class.
If you wish, you can read more about it here:
http://developer.android.com/reference/android/view/ViewGroup.html

Android View and ViewGroup

In Android ViewGroup inherits from View. A ViewGroup is a container which holds Views.
ViewGroup (LinearLayout)
View (TextView)
Why did folks at Android defined this relationship as Inheritance instead of composition. As the ViewGroup contains Views shouldn't it be composition ?
I think you're getting too hung up on the wording.
A "ViewGroup" has every bit as much reason to inherit from a "View" as a "TextView", and "ImageView" or ... more to the point ... a "ScrollView" or a "SurfaceView" (the latter two both "contain things").
Perhaps "View" wasn't necessarily the best choice of terms ... but the class heirarchy makes complete sense. Regardless of what it's subclasses are named :)
IMHO ...
I think this is a great example of the Composite design pattern:
http://en.wikipedia.org/wiki/Composite_pattern
Even though the naming might not be the best...
Reading the official doc is the golden rule.
A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.LayoutParams class which serves as the base class for layouts parameters.
If you still do not find out what it is, search with Google image:
A ViewGroup is a (subclass of) View because it can serve as a view in important ways:
It can be an element in a layout XML file
It can be displayed on the screen (by displaying its child views, its own background color, etc.)
It gets inflated along with the rest of the view hierarchy
It can serve as an activity's content view (via setContentView())
So it really is a View.
I agree that the classname ViewGroup is a bit confusing, because it sounds like it's a group, not a view. Calling it ViewGroupView might have been more logical, if unwieldy.
Why did folks at Android define this relationship as Inheritance instead of composition? As the ViewGroup contains Views shouldn't it be composition?
In a case like this, inheritance and composition are not mutually exclusive. A ViewGroup is a View (inheritance) and a ViewGroup can contain Views (composition).
Viewgroup inherits properties of views and does more with other views and viewgroup
A ViewGroup is a special view that can contain other views.
- The view group is the base class for layouts and views containers
- For example, RelativeLayout is the ViewGroup that contains TextView(View), and other Layouts also.
refer link for info:
https://developer.android.com/reference/android/view/ViewGroup.html
A View represents the basic building block for user interface components
-It occupies rectangle on the screen and is responsible for drawing and handling events.
- Examples are EditText, Button, TextView etc
refer link for info:
https://developer.android.com/reference/android/view/View.html
All UI elements in an
Android app are built using View
and ViewGroup objects.
View :- A View is an object that draws
something on the screen that the
user can interact with.
ViewGroup :- ViewGroup is used to hold Views
and ViewGroups.

Removing a View from an Activity

You've got a View defined in a layout file. How do you remove it from you Activity code?
ViewGroup vg = (ViewGroup)(myView.getParent());
vg.removeView(myView);
should do what you want as far as correctly removing the View from the Activity. The other guys' answer will just make the View invisible, using up resources.
As Android mentioned in a comment:
view.setVisibility(View.GONE);
Quoting the Android reference:
This view is invisible, and it doesn't take any space for layout purposes.
I think it's the best solution unless you really need to delete the object.

Categories

Resources