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. :)
Related
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?
Well, i am developing new features inside a product which means lots of limitations i just can't break.
There is one activity which monitor the rotation event but use one unified layout for both portrait and landscape. Now i need to add some animations for several widgets inside the view.
For example, animation A to expand a toolbar, while B to collapse it.
But when i do A in landscape mode, and then rotate to portrait mode, the widgets still keep the position after animation A instead of the original layout configured in the xml file.
So what i want is to reset the layout partially. How can i achieve it???
Thanks!
Finally, i found a solution. Well may be not the best, it still solve my problem.
Insert a parent layout inside the activity view, let's say and put nothing inside it.
During the run time, add the child view according to the screen orientation dynamically as following:
Put the codes inside the onConfigurationChanged().
LinearLayout ll = (LinearLayout)findViewById(R.id.linearlayoutId);
View viewPor/viewLand = getLayoutInfalter().Infalte(R.layout.Por/Land, null);
ll.addView(viewPor/viewLand);
You may need a object associated with the specific view for manipulating the widgets inside the view for the animations.
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. )
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.
How can I get one column in portrait mode and two columns in landscape mode with a ListView or TableLayout.
For example, in Portrait:
Element 1
Element 2
Element 3
Element 4
And in Landscape :
Element1 Element2
Element3 Element4
I thought to use ListView with one column in portrait mode and TableLayout with two columns in landscape mode, but before that, I want to know your suggestions.
It'd be way easy for you to use a GridView. You can set/choose number of columns on runtime and hence set them differently in portait vs landscape mode. That way, you'd just be manipulating a single view.
I recommend using a ListView rather than a TableLayout, since ListViews only create enough Views to fill the screen while a TableLayout will create every resource whether they are visible or not. In this way a TableLayout is slower and might even crash your app.
You can check the device's orientation with one of the answers from: Check orientation on Android phone. Then if the orientation is:
landscape, load your double View layout.
portrait, load your basic, single View layout.
Loading different layouts in a ListView requires a simple custom Adapter. If you haven't written one before, here is a tutorial to help you.