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.
Related
For example, I have a GUI like this:
So, I have a lot of panels. My GUI is rather functional, so it is very big. I don't want to create all GUI in one class. So, I can separate left panel, center panel and bottom panel in individaul classes. But they need to have access to some shared data. So, I could create class MainView which will store shared data and draw panels where they should be. After that, I could create classes LeftPanelView, CenterPanelView, BottomPanelView, that should extend MainView.
For all these classes I should have a ViewModel. For each View I should have its own ViewModel. So, I should have MainViewModel, LeftViewModel, CenterViewModel, BottomViewModel. And, as well as hierarchy of view view models will be connected in hierarchy. LeftViewModel, CenterViewModel and BottomViewModel will extend MainViewModel.
I have no doubt that View models have to be connected in hierarchy. But I do not know whether this is good to connect view classes in hierarchy. Prompt me please. Maybe, it is an awful design and it will be better to develope all gui in one class?
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. :)
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.
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.
I installed the ADT plugin for Android for Eclipse, and I don't like it.
I would like use my own Grid Layout the way I want and design windows by code (the same way as JFrame in Java, because I already know how to do this) not by click and drag.
How to do this in Android?
While I don't think it's a great idea to write the UI code in java, here's how you would do it.
First, you'll want to look at the android.view.* and the android.widget.* packages in the Android Reference docs.
Everything the the Xml corresponds to a View (or Subclass). So, if you see LinearLayout in the Xml, then there will be a LinearLayout java component as well.
So, in your Activity's onCreate() method, where normally you'd just set the content resource xml view, you can instantiate a Widget and set it directly.
public void onCreate(Bundle state) {
TextView tv = new TextView();
tv.setText("Hellow World);
setContentView(tv);
}
You might also need to the set the LayoutPararms as well, since most widgets need to know if they should wrap content or fill the parent.
Many of the Layout widgets are analogous to Swing Containers, ie, they accept children, so you can call addChild() with another view to create a widget hierarchy.
This should be enough to get you started.
As a final note, I'd like to reiterate that while you can build the complete UI in Java code, the Xml layout does offer some other benefits. For example the Xml layouts allow you to support multiple screen sizes, or different layouts (landscape vs portrait, etc), which is much harder to do (but not impossible) in Java. Also, you might be tempted to think that because the Views are in Xml, then they will be "slow" to be created on the device. But, Android optimizes the Xml resources, and in fact, I don't think they are even Xml at the time they are compiled into your App. So the resource Xml files are very efficient, and writing direct Java code for the Views probably won't get your much extra in terms of performance.