Another problem that I encountered today...
I am looking for something similiar to flowLayout from swing, I was looking and found nothing...
I need some layout inside which I am gonna place 3 buttons, when there is not enough space ( In potrait mode for example) I want them to be shown like one button above taking 100% space of layout, the other two just under the first, when there is enough space (landscape mode) I want them to be placed with the same weights in one line.I thought about relative layout but it misses weight, linear layouts on the other hand doesnt fit my requirements from what I have read.
What you could try is overriding the onConfigurationChange method of the activity and try implementing some logic there to change the position of the views depending on the orientation of the device. Or in your oncreate you could instantiate all your views and hide a complete set of controls for one type of orientation and then on another type do the opposite. Ive done both of these and hiding and unhiding the views seemed to be the least performance taxing because everything was instantiated once in oncreate. You could also try using seperate layouts all together for each orientation which might decrease performance due to view inflation but if your views are not to complex you might not notice anything except on lesser devices.
Related
I am new to android development and currently struggling with fragments and layouts (Xamarin.Android for what it's worth).
I am adding fragments to my View programatically in the OnCreate method of my main view. One of the fragments holds the controls for navigation and implements a fly-over Idiom. Now my problem is, that I haven't found a way to make the width of the navigation fragment, say, half of the width of the parent control. In every override I have been able to access the layout has not been performed and hence the width of the fragments and the main view are both 0. Setting the size of the fragments decleratively in the layout files did not work either (neither for the translate properties which I'll nees to access in the same way to hide the fragment initially). Xamarin/VS complains that setting the size or translate properties to a string (i.e. parent_width or the like) was not valid.
Every tutorial on fragments I've read omits this point.
If I am understanding your problem correctly you are having an issue with layout sizing of fragments added dynamically.
It may be helpful if you were able to paste your code outlining how you are adding your fragments and what override methods you attempted to use. However, here is a best attempt to assist without seeing any code.
There are two approaches to sizing dynamically added elements that I have used in the past consistently. One is to use a base container for your layout of LinearLayout and specify weights for your fragments dynamically. This would allow you to specify that your fragments should consume portions of the layout easily. For instance if you have fragment A and B and you wish for fragment A to consume 25% of your layout space - you can set its weight to 1 and Fragment B's weight to 4. You can find support for dynamically setting weight of an element (such as your fragment) here: How to set layout_weight attribute dynamically from code?
Another option would be to use the an OnGlobalLayoutListener that will "fire" when the control is laid out. This will ensure when you try to measure the layout to determine its width/height that it has already laid out and consumed the proper space for measurement and you will not get back 0. There are a number of articles outlining how to write up the code for this listener scattered throughout the web. I It may also be useful to subclass out some of the listener functionality for easy reuse if you find you have a need often to know when the control has laid out. Here is a link to the Android documentation on the subject http://developer.android.com/reference/android/view/ViewTreeObserver.OnGlobalLayoutListener.html.
Hope it helps.
i have some issues to get by.
In portrait orientation i need simple listView with individual button for each row.
In landscape orientation i need to divide view for two areas. First (let's say at left) is ListView mentioned before. Second area contains few TextViews filled after clicking on ListView.
Questions:
To differ between land and portrait orientation i'm going to use layout-land and layout-port folders. But in lanscape orientation there will be few additional controls. Checking orientation in onCreate method and not initializing additional controls will be enough?
How to get described above landscape orientation view?
Thakns in advance.
You can do it the way you mentioned. Another possibility is to include all of the elements in both layouts, but in the portrait orientation you can hide some of them (by setting width/height to 0, or by setting android:visibility to 2 (gone)).
You can use a horizontal LinearLayout to get two side-by-side regions. The first child of the LinearLayout will be your ListView. The second child of the LinearLayout can contain the extra TextViews you want.
you need not check orientation in onCreate method as because android operating system will load entire activity when the orientation changes.
you need to do only one thing. for the UI give same id's in both landscape and portrait xml files.that's enough
I think that the use of Fragments is fit for the layout you want to achieve.
Here is a link to a tutorial that you may find useful.
http://mobile.tutsplus.com/tutorials/android/android-sdk_fragments/
Hope that helps. :)
Is there any way position views relative to each other like you can with Android layouts?
Example: You have two UILabels that are dynamically set to strings of variable length, one above the other, and you want the bottom label to appear directly below the last line of the top label, regardless of how many lines the top label ends up having.
Another example: Same situation as above, but one of the labels is sometimes hidden. You want the resulting label(s) to be centered vertically in the parent, regardless of whether it's one label or two labels.
Android's Linear Layout and Relative Layout make this very easy to do, but I can't figure out how to do this is iOS. Can it be done?
You will be able to do this using AutoLayout in iOS6. For an application that should run in iOS before 6.0, you have to do it by yourself, but this is not very complicated.
Actually I have implemented a class to do this (that's a long time ago, I hope it still works, but there is no reason not to). This OHStackView class is a subclass of UIView that automatically layout its subviews horizontally or vertically to stack or align them.
You can ask OHStackView to stack its subviews horizontally or vertically (one above the other, etc), or align their top/bottom/left/right borders or their centers, and even specify a padding between each subviews. Each time one of the subview changes its frame or size, OHStackView will automatically relayout all depending views to realign everything.
(E.g with your two UILabels, a simple call to sizeToFit on your labels to make them adjust their size to their content will relayout everything around automagically)
There is an example project provided so feel free to test it.
Note: IIRC, my subclass does not take the "hidden" property of the subviews into account. But you can easily add support to this behavior by adding a condition like if (v.hidden) continue; in the for loop of its layoutSubview implementation to only take non-hidden views into account in the layout algorithm.
HTH
I've been searching if it's possible for a long time. As far as i can tell, it's not possible for now. I don't remember where i read this,but it will be possible with ios6. Instead of using Android's Linear Layout,you can use sizeWithFont method to detect size of your UILabel,then you can set their frame to position them.First,you need to set their text of course to find their size according to their font family. Another thing you can use is sizeToFit method. Unfortunately,i don't know how to use it. You can give a shot,though. At that time,i found this.Maybe ,you can use it https://github.com/scalessec/CSLinearLayoutView
Hello! I have just started playing with android layouts and i wonder if there is a general way of applying basic layout so that it will adjust itself to multiple screens and automatically to landscape view. For example:
In the picture above, I have added some buttons. Now what i want to learn is which layout or options(like weight,gravity,alignment) to b used so that they remain the same in Every view & on every screen. Some says to use linear layout within linear and then add weight and alignment. They said that by doing this, you have flexibility to remove any button and yet no other button looses its place(unlike in relative layout). Can there be better way that will have same layout on all screens and yet flexible??
You can use multiple linear layouts if you want to create a FORM.
otherwise Absolute layout is also good but not much preferable.
Relative layout needs practice, as you have to set widgets with respect to other.
multiple linear layouts may be useful.
RelativeLayout is very easy to use and if you learn to align the widgets in it, the layout will look the same on every screen BUT it's good for a layout that is very simple (few widgets on layout) or a layout that you know that will never change because changing on RelativeLayout is so hard and the best way is editing the XML not working on DesignView.
LinearLayout is not flexible like RelativeLayout but making change in it is so simple and other widgets will not lose their positions.
After all if you want to design layout for multiple screen size I recommend to use Fragments.
Lets say I have a list on screen, which I always want to be usable. I also want a small image or textview to slightly overlap the listview. Is anything this possible without using absolute layout parameters?
I've never seen it in any android app or tutorial, but there are things like this in many iPhone apps. It adds a nice touch.
(Also, I don't have any code to show because I'm not sure where to start)
Relative Layouts also allow things to overlap. Views declared later in the xml will be on top. I believe that aligning view edges and use of margins should allow you to achieve this affect without great difficulty.
You could use RelativeLayout and set for example android:layout_marginTop="-50dip" android:layout_below="#id/my_list".
As well as RelativeLayouts, you can also use FrameLayouts to stack objects. Other than the z-order (last object declared = highest z-order), the child objects don't depend on the positioning of other objects in the group, so you can just set margins or gravity to position them.
So in your instance, just declare a TextView after your ListView, and position it wherever you want. It won't interfere with the ListView's positioning, and it will sit on top.