I have an Android App that I am setting up to handle screen rotation and with the landscape position fragment, I am putting together a Linear Layout with that contains 2 fragments that I will to sit side by side and fill the screen. The only thing that escapes me is how to auto fill the screen with the two fragments.
Use layout weight attribute to do this. Check this answer for further explanation What is android:weightSum in android, and how does it work?
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.
I am just getting into Android development and have a couple of questions about layouts.
I have a RelativeLayout as my root element and inside that I have the following items in the order:
TextView, TextView, Button, Button, Button, TextView
When I view the layout/activity in portrait orientation everything is fine. However when I switch my phone to landscape orientation the design "bunches up" and the first Button overlaps the second TextView and I can only partially see the last Button's background color and the last TextView text lands over that Button.
There is no scrollbar or means to view the whole display. Is there a way I can keep my layout the same when the screen rotates? I know I will not fit those 6 elements in a landscape orientation but I thought the layout would simply be scrollable to view the rest?
One simple trick is to just wrap your whole RelativeLayout inside a ScrollView (http://developer.android.com/reference/android/widget/ScrollView.html).
Keep in mind that the Scrollview can only contain, one layout element, in thisi case since you already have a root relativelayout that's just fine!
When the screen contains content I usually pu the entire layout or a big part of it inside a ScrollView, not only it will help users to see content when they switch their devices to portrait mode, but will also help user with devices with smaller screens see all your content
You can easily check how things will look using the layout editor on Eclipse or Android Studio.
On Android Studio, just open your layout file and switch to the design tab, then on the upper side there is an icon to change orientation.
A LinearLayout will guarantee that the views are placed below each other, not overlapping. A RelativeLayout does not do that if you don't specify each Views position in Relation to each other.
Android does not make your Layout scrollable by default if they don't fit on the screen. You have to wrap your Layout in a ScrollView.
As was pointed out, you can create different layouts for different screen sizes. You can also just create one with a ScrollView, it will make no difference when everything fits on the screen anyways (portrait mode. )
Suggest me on this
I have to use some header and body part in android screen design, Can i use plain Linear layout for screen design or can i use relative layout or else both layout combined together.For the header bar im using a gradient image and application runs both in vertical and horizontal orientation.
As of now im using two main linear layouts for the first one im using a height of 40 dp and for the second i just used 0dip is this a correct way of approach or i have change anything.
Don't mix concept of RelativeLayout and LinearLayout. RelativeLayout is preferred because it reduces extra lines as compared to LinearLayout. In RelativeLayout views are placed relative to each other i.e. left, right, top and bottom unlike LinearLayout where you can't place view in respect of some other view. Both have its own advantages. As Weight concept is not supported by RelativeLayout but LinearLayout.
Depending on the complexity of layout both are chosen. One thing to avoid is un-necessary nesting of layouts which reduces performance. I would recommend read concepts of RelativeLayout, LinearLayout and weight first then you will be able to judge which layout to use on your own. Till then use RelativeLayout as it requires minimum number of lines.
You can use Linear-Linear, Linear-Relative or Relative-Relative. Anything you want.
Your question is hard to understand. From what i get, i think your approach is fine. You should let the Screen design (second layout) use "match_parent". It will take up remaining part. For your header layout using "40dp" is fine. I made app with Header, and i used this approach.
If in Header, you are adding images as well as TextView, it is advisable to use RelativeLayout. In the rest part, use however you need it.
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.
Seeking help to design a layout as shown here:
http://docs.google.com/Doc?docid=0AQhgDtGvE2HgZGZ6cmtua185MTd0eGdyZmc&hl=en
The major challenge I face is aligning the components at desired positions. Please refer the three buttons(icons) and the way they are positioned.
Literally, going nuts, thinking how to position those exactly at the desired places.
Any help is much appreciated.
Regards,
Rony
Since you used the Android category, I'm assuming that you're trying to recreate this iPhone layout in Android.
The three buttons would probably be best laid as follows.
Your main layout container would probably be a RelativeLayout, so you can dock things to the top and bottom and lay everything else out in relation to one of its sibling elements. The three button icons (and I'm assuming you're referring to the circular buttons and not the tab bar buttons at the very bottom) would be in a LinearLayout centered within its parent (probably want to use gravity=center_horizontal on the main outer layout) and the individual items would have an equal left and right margin parameters to get the desired spacing (layout_marginLeft, layout_marginRight). You could also make the LinearLayout container of the buttons flush (layout_width=fill_parent) and using android:weight attribute on the outer buttons laying them out towards the center and using a lower weight on the center item. I'd favor the first option, personally.
If you're trying to create relatively complex layouts and any of the above doesn't make sense, go back and read the docs. Layout in Android is very powerful, but you really have to understand the available tools to take advantage of it.