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)
Related
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
I have defined my Relative Layout using the drag and drop tool in Eclipse, so all of my buttons are laid out how I wish. My issue is when I set the onClick listener, that calls a method in another class. So to be able to redraw items on screen, I need to access the layout manager so I can add and remove buttons from the screen as well as update textViews. I have done all of this in a demo I made in Java, and I used a JPanel with GridBagConstraints. Now that I am moving to Android, a system I haven't done much development in, I am at the point where I have to learn some new stuff. For example in my demo I made I could do this:
grid.remove(trueButton);
grid.add(falseButton);
grid.remove(textField);
grid.add(backButton);
Essentially I want to be able to do the same sort of thing in my Android app. If you guys need more info I can provide, I wasn't really sure how much would be needed since I am looking at really just where to start. Everything has been declared in the XML since the drag and drop part of Eclipse does that all for me. It is just the Java part that is giving me some issue.
Why not just setVisibility of the buttons you wish to hide/show? Same with the TextViews.
You can set visibility to 'GONE' and it will be as if the view has been removed (taking up no space in the layout and not responding to touch events.).
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 have four images in a row at the top , on click of each icon i want to change the underlaying background (image) and the controls on the layout , this way it achieves tab like structure and behavior , i want to know whats the best way to achieve this ? I think i will have four layouts each layout having one image highlighted showing that tab selected and corresponding components on layout, and will change this layout when user clicks on image.
Is this a good idea to achieve this ? or i have different solution available ?
its nice if u give me some idea about necessary features or API or layout component related code
Suggestions are welcome thanks!
That is not a good idea. You should use android's tab architecture. Here is a example at developer.android
you can change layouts by switching views to invisible and visible. but it's not a good idea when you want to change 4 layouts.
It becomes hard to maintain the code if you have 4 layouts switching.
It's better to use Tabs which will help you in preserving the state of the each layout.
Customize the tabWidget to make it look like you have 4 buttons on the top, not the tabs.
HTH.
I am working on an Android project where a group of buttons needs to show on the bottom of every screen (activity) in the application. The group of buttons are basically a navigation bar. I want to know the best way to do this without creating new buttons for every activity. I have been around programming (C++/C#) for many years but am pretty new to Android and Java so if someone can point me in a general direction, it would be appreciated.
Thanks.
I bet you need to use "include" tag for xml layouts. It's the best when you need to reuse some UI components. See http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ for the examples and description.
To elaborate on Konstantin's answer, after you've used include, you'll need to bind actions to these buttons.
If the buttons should have the same action regardless of the activity they are in, use the include tag to create their layout and then create a parent NavigationActivity (or whatever else you want to call it) class from which all your other activites will inherits.
In the parent NavigationActivity class' onCreate method, you can set up the onClickListener (and other needed stuff) for the buttons.