My goal is to create an automatic form generator, so the user can add controls to the screen. And the controls must have different widths.
Example:
The user could add an EditText that will use the width of the screen and add a CheckBox after the EditText that would be placed below the EditText. And could also add a button or spinner that will use the rest of the width of the screen. The user can all all the controls desired.
How can i achieve this goal?
Instead of a gridview, you could use a TableLayout, which has multiple TableRow elements.
What's interesting is that the width of a column is defined by the width of the widest element in the column.
You can take a look at the TableLayout documentation for more info.
There are a number of ways to do it such as TableLayout and also using lots of nested layouts.
The TableLayout way is probably the best and easiest way to implement what you're looking for.
If you want to dynamically change the layout depending on the width of the screen there are two ways.
Method 1 Programmatically create the layouts instead of using xml layout files. this way you can do calculations and change things on the fly.
Method 2 This is probably the way I would do it. Include different layout files for different screen densities, orientations and sizes.
Related
I was recently tasked with creating a compound view element as an exercise. I found this to be a relatively simple task, however I am somewhat stumped by the following requirement:
- The component has to have both a horizontal and a vertical layout (selected via an attribute).
The compound view is (effectively) a single LinearLayout with one EditText and two Button elements, one before it and one after. This would make solving the task trivial (just letting the user change the android:orientation attribute it inherited from LinearLayout), if it weren't for two factors:
In the horizontal version, I want the top Button to be on the right of the EditText instead of the left (so effectively make the layout right-to-left as far as element order in the horizontal orientation is concerned).
Depending on the layout, the components should have different width and height values (for instance, Button width set to match_parent in vertical layout, but wrap_content in horizontal).
It seems like it would be most easily solved with two separate layout files (one for each orientation), but I can't seem to be able to find any information on how to achieve that - which is leading me to the conclusion that it might be impossible. If so, what would be the easiest way to achieve that goal?
Just to reiterate, I don't mean different layouts depending on screen orientation - the layouts have to be selectable while adding the component into the application.
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.
Which Android layout would be best suited for this example. Inside each box would be a picture. I am mainly wondering what would be the best parent layout for the entire screen.
I was probably going to use TableLayout for each of the rows of boxes. Then have each column in the TableLayout be a FrameLayout. Then have each FrameLayout contain the picture.
But i am not sure what parent Layout i should use to contain the entire screen.
I would like this to be able to fit in Landscape on the majority of the phones on the market. Maybe it resizes everything to fit those screens in landscape.
I would also like there to be spacing between those TableLayouts.
Any assistance is greatly appreciated.
Thanks.
If I were building that, and the number of images/boxes in each set was static, I would probably use RelativeLayout as the parent and LinearLayout for each of the "rows." Then just use ImageView directly as children of the LinearLayout. RelativeLayout allows you to position things relative to each other or the parent.
Either RelativeLayout or GridView whichever fulfills the exact requirement.
I have read many articles regarding layout, but I am still quitely confused. My questions are:
When to use relative layout? Example?
When to use table layout and why we can't use it instead of relative layout?
When to use linear layout?
I just need brief answers.
When use which layout?
I think It depends on your UI, and most important thing that how you create optimized layout.
From definition : -
LinearLayout – designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout – designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
TableLayout – designed to organize child View controls into rows and columns. Individual View controls are added within each row of the table using a TableRow layout View (which is basically a horizontally oriented LinearLayout) for each row of the table.
References :
Creating Efficient Layouts
Common Layout Objects
And most important Hierarchy Viewer
at first there is some confusion about these layouts but as you start playing with these three layouts u will get idea where to use what.. I worked on relative-layout the most.
Consider i want to use a widget always at bottom of screen then with table or linear layout this is not possible always.. without feeling screen other two can not make item at bottom but relative can do.use of any type of layout depends on your screen requirements.
I started out using relativelayout. But recently I've switched to using mostly linearlayout.
The reason is kind of hard to explain, but take this as an example: Say I want a layout that has two images centered in the middle of the screen. Both images should take up 1/4 of the screen width and 1/4 of the screen height. This is impossible to do with relativelayout assuming you want it to work exactly the same on all devices. But you can do this with Linearlayout. By creating vertical and horizontal parents, you can create "boxes". To accomplish this you must learn about weigthsum and weigth. Parent layouts should have the weigthsum attribute and children should have the weight attribute.
Anyway, my point: Relativelayout is easy to use but it's also deceptive. You may think that your layout will look exactly alike on all device, but most likely, they won't look alike. The reason for this is:
With relativelayout you must define size with either dp or px(assuming you don't fill parent or wrap content).
Different devices have different aspect ratios.
I hoped that helped in terms of understanding relative and linearlayout.
I am having some difficulty with an Android layout problem. I am trying to make a form for users to fill out. This form is defined programmatically (from a server-provided configuration over which I have no control) and thus I must implement it programmatically. The form has several different field types, but for simplicity we can assume they are all simple text fields (EditText).
I currently have the form implemented as a vertical LinearLayout. For each field I have a horizontal LinearLayout that contains a TextView for a field label and an EditText for the user to enter a value for the field. I have the EditText set to fill the width using LinearLayout.LayoutParams(FILL_PARENT, WRAP_CONTENT).
This works well when the TextView label is short, but when it is long it causes the EditText to be very small and makes it hard for the user to enter a value. Ideally I would like the EditText to be at least half the width of the screen with the TextView label wrapping if necessary. I've tried a TableLayout with TableRows but I still had difficulty. I would also rather not force a grid and thus waste the space on the lines with short labels (assuming the other requirements are met I'm flexible on this). I would have tried something like a FlowLayout to force the EditText to wrap onto the next line but it's not supported on Android.
Any suggestions for how I can make this work better? XML-based solutions will be accepted assuming I can port them to a programmatic approach. I would also like to make this as flexible with respect to screen size and orientation as possible, so this means avoiding hard-coding any widths.
Thanks in advance.
Try playing around with layout weights. You should be able to tell your two items to each take up half the screen, for example, by setting the width=0 and layout_weight=1 for both.