How to handle android screen orientation change - android

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. )

Related

Android Layouts and Auto Scaling Fragments

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?

Why is my Android phone layout different then my layout on Android Studio?

The layout of my MainActivity on my phone appears very differently than the layout I see on the code editor. Images are included. I am using a TabbedView. However, the problem still exists on any layout I choose (empty/tabbed).
What can I do to solve this problem?
I am concerned about the text "Hello world" in the middle, not in the top left corner. I follow this tutorial and in the tutorial it is correctly positioned.
It works as expected, because section_label TextView width is wrap_content it changes, depending on what text it displays. And the 2nd TextViews position depends on it, so it changes too. You should be able to verify this by setting various length texts in your layout editor.
For what you want to achieve, RelativeLayout is not the best choice and you may want to use i.e. LinearLayout instead, with its orientation set to vertical and layout_gravity of second TextView set to right
This is happen because you are using the RelativeLayout in the relativelayout we can arrage our views in custom position wise by the help of give the android:id=#+id/some_id like this if you want your second view is shown just below the first view then give them the property android:layout_below="#id/view1" in your second view
Please read all the documentation of RelativeLayout form this link
https://developer.android.com/guide/topics/ui/layout/relative.html

ListView inside ScrollView

So I am aware that I can't use ListView inside ScrollView, because ScrollView gets the focus in that case and ListView becomes non-scrollable. But I have a program that goes out of screen when in landscape mode and I need to be able to scroll down to see the rest of the program so I use LinearLayout(vertical) with ScrollView that contains the most of the program and under that ScrollView I got my ListView. Now I want to be able to scroll down to my ListView when I am in landscape mode but it won't let me do that, it stops me where ListView begins. Is there a some kind of solution for this, or is it better for me to make my program stay in portrait mode?
You are still trying to nest a ListView inside a ScrollView in landscape mode...
I suggest creating a new landscape specific layout with two columns: the left hand side contains UI elements inside the ScrollView and the right hand side contains the ListView. This way both Views will still support scrolling.
Simply save this new layout in a new folder res/layout-land with the same file name as the portrait layout in res/layout. The OS will automatically switch layouts when the orientation changes.
You can read more about this: Supporting Different Screens
This end result will be similar to the old Google Market image:
The left, green side can scroll if necessary and the right, white side will scroll as well.
Do away with the VerticalLayout and insert the rest of the content as header and footer views of the ListView

What is the best Layout to be used While designing for multiple screens and for both landscape and portrait

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.

Linear Layout Issue at Runtime

I am trying to build a layout dynamically which display some text and image for the most part, but has a series of buttons placed next to each other in the bottom.
I have a linear layout that carries the text, another linear layout that carries the image. And yet another linear layout that carries the buttons that get created in a for loop. I have a main layout aligned vertical that adds the text, image and buttons layout, in that order. To finally generate something like this:
Text ....
Image ...
Button1 Button2 Button3....
The problem is the number of buttons get decided at runtime, so if there are more than 4 buttons, the 5th button gets displayed really tiny. Also, when I tilt the phone, I get only the text and image showing, but no buttons coz the image covers the entire screen.
Layoutting seems to be pretty complicated to me, any help is appreciated!
Thanks
George
You do not need to wrap single views in a linear layout, so add the text and image directly to the root linear layout. You might consider using a relative layout instead of linear for the root.
Using FILL_PARENT and WRAP_CONTENT for the LayoutParams width or height can give some useful results. For example, using FILL_PARENT for the image height might scale it down to leave room for the buttons.
Be careful with LayoutParams because there are lots of them and only the one that matches the ViewGroup class should be used.
One option would be to implement an onLayout method of your own in a custom ViewGroup. You will be passed the dimensions you have to work with and be able to position all the views as you see fit.

Categories

Resources