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

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.

Related

execute class from included layout

Within my layout I have set a Include Other Layout. this layout schould in his turn show a listview with items loaded from the web (my webserver)
the only problem is: the items don't load in because the class that holds the code to load the items isnt called on because im using a Include Other Layout meaning only the layout is called and not the functional code from any .java files (classes)
leaving me with a blank page...
is there a way to make a call upon the class containing the code for the layout i have included?
<include/> tag is only for including only the view into another layout. It can be very useful if you use a common view everywhere. A ProgressBar can be an example. You can include ProgressBar everywhere you want. But it is just a UI.
If you also want the functionality you should use Fragments. Actually Fragments are exactly for what you describe.
Here is the tutorial from Android Developers official website
http://developer.android.com/training/basics/fragments/index.html

Android Java: Add absolute positioned view on screen

Let's say I've got a whole interface with some Layouts, View-Objects and stuff and now I want to write a java method that adds a pop-up error message over all this layouts to the front.
I know that there is the possibility to add it to an existing layout (e.g. main_layout) this way:
main_layout.addView(error_layout);
But is there maybe a way to put it just independent from the other layouts on the interface?
Something like
UInterface.addView(error_layout);
?
This should give you the root layout of your activity
getWindow().getDecorView().findViewById(android.R.id.content)

How to write a app for Android in the same way as JFrame

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.

managing Persistent Visual Data (event driven buttons and ImageViews) throughout Activities of an application

I need some advice for those who are experienced making Android applications. What I would really like to have, for my application's appearance: at the top, a title-bar which is a ImageView (content is a png), and at the bottom a series of custom buttons which make up a tab-bar like thing. In between the title and the tab-bar is the Content, which may be anything... (most likely buttons)
I have been doing this by making a RelativeLayout which specifies LeftMargin and UpperMargin for x,y coordinates--
Currently all of my activities are inheriting a custom MyActivity class, which rebuilds the title and the tab-bar at the time of onCreate. This seems bad to me!
PART1)
---A solution to Persistent data
Since the "tab-bar" and the title are persistent no matter what screen you're on during this application's run-time, it makes the most sense to store them somewhere... How should I do this? Make a singleton object that the Activity's ask for?
I thought a little about the singleton object, and I'm not even sure what I would store, since the Views that are on displayed during Activity A have activity A as context, and not Activity B.
PART2)
---Animation Aesthetics
I would really like to have the "Content" (the view in the middle between title and tabbar) slide out to the left, and the new content slide in from the right. I.e, I'd like the tab-bar and the title to remain fixed while the "activities" change. is this at all possible? What should I do to achieve it?
one idea I had, was to make all of the program in one activity! I would create an animation for the Custom View in the middle, and I would override the "back" button to navigate correctly to the previous Custom View. Is that a horrible idea?
Anyone have any advice?
Read http://developer.android.com/design. Most of the design principles can be applied to apps that run on legacy releases; it's not just limited to Honeycomb and Ice Cream Sandwich. Do consider the Action Bar and Dashboard design patterns.
I don't really recommend using just one Activity -- generally, an Activity should be a separate, encapsulated, pretty well defined chunk of functionality that can execute independently of other Activities.
To avoid duplication of your UI, consider reusing XML layouts.
To avoid duplication of your logic, consider using Fragments. You should be able to mix and match them in your activities.
To achieve the animation you describe, consider implementing a ViewPager.
Using the ActionBarCompat sample app and Android Support Library, you can enjoy modern goodies like Action Bar, fragments, tabs, and horizontal sliding transitions on devices running Android all the way back to Donut (1.6).

How to implement a Compound View/Presenter for a GWT DecoratedTabPanel?

I'm new to GWT, and I've been reviewing the MVP implementation which uses the rpcService and the eventBus. I was wondering how a tab panel can be implemented such that each tab has its own sub-view. I have been waffling between making a custom widget that derives from a panel, or to figuring out how to make a presenter use another presenter, or to make a compound presenter class which handles it all for me.
Does anyone have advice on how to separate the functionality for each tab as opposed to keeping the implementation within one view/presenter pair?
I was in the same situation, but decided to change my implementation to simulate a TabPanel. If your views and presenters don't need to interact with each other (e.g. dragging something from one tab to another) then I think it'll be easier to separate functionality by loading your View into a shared SimplePanel. You can simulate the tabbed portion of the TabPanel with a widget that listens for PlaceChangeEvents (to change the highlighted tab) and sends goTo commands to the PlaceController your app is using (to handle clicks on the different headers).
It took a couple of hours to implement this, and the resulting code is much cleaner. My initial attempt involved listening for PlaceChangeEvents and then calling the appropriate tabPanel.selectTab() function, but trying to figure out how to start and stop the presenters for the different tabs was too jumbled up - like you suggest, you'd have to implement your own compound view model.
I solved this without faking a main tab, but using the one provided with GWT's basic SDK. I did this by:
Add an ArrayList of Presenters to the MainTabPresenter
Constructed each tab's present plus view within the 'go' method of the MainTabPresenter
Called 'go(null)' on each of the tab presenters.
Handled the null situation on each.
Implemented a method in the MainTabView to add the tabs to the DecoratedTabPanel
It all works like a charm. The MainTabPresenter so very thin, and allows for complete implementations of View/Presenters to be written into their own files.

Categories

Resources