view and viewgroup android - java

I know java basics and I am learning Android development now. I have learnt that there are two central android classes for the ui, View and ViewGroup. I want you to correct me wherever I am wrong.
ViewGroup is a subclass of View and holds View objects together. TextView, EditText, etc are subclasses of View. LinearLayout, Gridview, etc are subclasses of ViewGroup.
I want to relate this to the OOP concept of java but I don't seem to get all this. Where are the objects? How come a subclass is a container?

I'll give you a basic overview here. I'm also pretty new to Android, so the correct-me-if-I'm-wrong thing goes for me too. :)
A View is basically a unit of UI; like, say, a box of 24x24 pixels (yes, THAT basic). Now this box can be used for anything, because it is the top-most entity in the hierarchy. We can define it more precisely by specifying what we want it to hold, this is where TextView, ImageView, WebView, etc. come in. So this box 24x24 may hold text, an image or a web-page content, respectively.
A ViewGroup can belong in the 'etc.' above. Just as for showing text, the 24x24 box can be used to display 4 boxes 12x12. If so, the View can be classified as a ViewGroup.
Further, when we know what type of arrangement we require the box to hold, in this case, we can further classify it as any of LinearLayout, RelativeLayout, GridLayout, FrameLayout, etc.
In such a hierarchy, an entity can have a child object of any kind, even an instance of its own class.
You may even say that View is the 'Object' of UI.
I hope I have helped you.
Comments/edits welcome. :)

Related

What does findViewById(android.R.id.content) do in Snackbar?

I have seen in Snackbars being passed this findViewById(android.R.id.content) argument. We can't pass getContext() method as it demands a View parameter. I have seen on internet that programmers pass this argument inside Snackbar, what does it really mean?
Also, since it asks a View argument can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work? If yes, isn't it a little unexplanatory in code about what's really going on?
what does it really mean?
It asks the hosting activity to find a widget whose ID is android.R.id.content. All activities have one of these, set up by the framework Activity implementation and its associated Window. It represents the main content area of the activity.
isn't it a little unexplanatory in code about what's really going on?
You are certainly welcome to add comments to your code to explain your choice.
The Snackbar documentation explains the role of the View:
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view. Snackbar will walk up the view tree trying to find a suitable parent, which is defined as a CoordinatorLayout or the window decor's content view, whichever comes first.
Having a CoordinatorLayout in your view hierarchy allows Snackbar to enable certain features, such as swipe-to-dismiss and automatically moving of widgets.
With that in mind...
can i pass like any view that i have in my xml file, for example, any imageview or any videoView. If i pass these as an arguments, would my code still work?
Perhaps. It depends a bit on the UI of your app. If there is a particular CoordinatorLayout that you want to use with the Snackbar, pass it (or a child) to make(). Otherwise, any widget should work.

Meaning of Import android.view.View

I have been following tutorials on how to make apps and I want to make a GUI purely in java in AndroidStudio. The am confused as to what packages are to be imported. Specifically, what does the following code mean?
import android.view.View
View is a simple rectangle, the building block of applications, as the documentation refers to it. Every Widget (TextViews, Buttons, and every other UI component) extends this class.
Show us your code and we can tell you why you need this class, but seriously, for this kind of question, just use the documentation.
Ask for help if you don't know how to use the docs.
Views (and ViewGroups) are the basic building blocks of the Android UI. I believe you should go through this introductory training article first to get familiar with the concept and its use.
The graphical user interface for an Android app is built using a
hierarchy of View and ViewGroup objects. View objects are usually UI
widgets such as buttons or text fields. ViewGroup objects are
invisible view containers that define how the child views are laid
out, such as in a grid or a vertical list.

Is there a way to remove a view from a ViewGroup

I'm working on a Tetris android game. I need to remove some ImageViews from a layout manager. But I can't find any methods in the ViewGroup class to remove views. I know that you can use
view.setVisibility (View.INVISIBLE);
to make the view invisible, but the view is still here, in the memory, right?
So that will affect the performance, right?
Yes:
((ViewGroup)view.getParent()).removeView(view);
What IDE are you using? simply putting ((ViewGroup) view.getParent()). in the ide should give you the list of all methods.
And ofcourse you have to remove it from the parent viewgroup. that is the need of the method getParent() to get the ViewGroup the view is in, first.
And there is the method removeView(View view) right there in the list. Self Explanatory.
Just one thing to remember, you have to call this method from the UIThread. You cannot call this from any other Thread running in the background.
as you focused to INVISIBLE of VIEW in your Question,instead of using this you can use Gone of View.so further you can use gone in your XML or Java both to a view.
like in XML
android:visibility="gone"
And In Java
view.setVisibility(View.GONE);
enjoy your code :)-

Implementing a custom view in android

I have been working with Android for a while now , but recently something new came up .
I am supposed to make a Calendar. There is a default CalendarView View however it is drastically different from the Calendar that my client wants .
Now I embarked upon the "Create your custom view " tutorials on the internet.
I found out either I could extend something that existed , or extend View .
I extended View . My motive was to start with the basics that is draw the grid of buttons that would represent days of the month, upon clicking which the user would be navigated to a different page .
Here is where I reached a deadend. I know that onDraw is used to create the visual of a view. However we have to use the Canvas class. How can I include a android.widget.Button, as a part of layout in the onDraw method ?
Or have I completely gone rouge and am following an incorrect way ?
Please aid me. Thanks
You should create custom ViewGroup, in case of the Calendar, I would advice you to subclass GridView.
It might be easier to just draw your gird of days to the canvas manually.
GridView will be easier to start with, but canvas will offer you much richer possibilities.
If you choose GridView, you should create an adapter where will be one a day.
If you choose Canvas, you should basically draw your items in two nested for cycles, the outer for rows the inner for columns. You can draw text, shapes, basically just everything directly to the canvas.
On the other hand going with standard android views will allow you to integrate touch events or for example animations in much easier way. Doing such things only with canvas is much more complicated.

How to compose the Android Equivalent of a custom JPanel (AKA View)

I am trying to create a really involved UI for integrating with a proprietary product (it's currently web based and a total hack). Each screen/view has the same look-and-feel, the same 2-3 buttons in the same locations on every page. It's just 1 portion of that display changes.
If this were swing I would define a JPanel, compose everything but this center component, and then create instances of said panel Supplying the differing fields per instance.
I want to represent the 90% common portions of this UI flow as a single view and just fill in some blanks.
Can I do that? How do I do that? No haters please. Total Android rookie (seasoned Java vet though)
Can I do that?
Sure! However, I wouldn't describe it as a "total Android rookie" sort of problem, just as it wouldn't be a "total Swing rookie" sort of problem in that environment.
How do I do that?
There are a few possible approaches. The simplest solution is to define a layout resource file that defines the entire UI, with a FrameLayout as the placeholder where "some blanks" will eventually go. Then, at runtime, when you use that layout, you would "fill in the blanks", by putting something into that FrameLayout:
If you want each "screen/view" to be an Activity, you would use the aforementioned layout file in setContentView(), then manually inflate (or instantiate directly in Java) whatever "some blanks" are. You would call addView() on the FrameLayout to "fill in the blanks" with whatever you inflated. If you wanted, most of the logic could be bundled up in a base class, with subclasses overriding some gimmeTheBlanksPlease() method that supplies what is to be poured into the FrameLayout.
If you want each "screen/view" to be a Fragment, you would use the aforementioned layout file in onCreateView(), then manually inflate (or instantiate directly in Java) whatever "some blanks" are. Again, you would pour that stuff into the FrameLayout via addView(), and once again most of this codde could be implemented in an abstract base class.
There are more complex solutions (e.g., custom ViewGroup) as well.
The only simpler solution is if the "90% common portions of this UI flow" can be defined in ~1 layout file, you can use the <include> tag for composition of layouts. You'd have one common layout with the common elements, which would be included into the per "screen/view" layout and used at runtime. Again, you'd probably have an abstract base Activity or Fragment that knew about the common stuff. However, this gets messy if the "90%" would wind up being split among a whole bunch of layout files, just because of how the XML and positioning worked.

Categories

Resources