Display one view in portrait while another is in landscape? - android

I have an activity that per our requirements has to be locked into landscape orientation. However when the user holds the device in portrait we display a view on top of it. Up until now it has just been an image so I have rotated the image and I display it. Really the image is also being displayed in landscape but since I have rotated it, it appears like it is in portrait.
Now I need to complicate this by having a more complex view with layouts being displayed (layouts, textviews, buttons, etc) instead of just an image. The same "rotation" doesn't seem to work because stuff is displayed off of the screen. What is the recommended solution to show one view in portrait while the view behind it is locked in landscape?

edit:
if the idea is to literally have the view underneath to show rotated 90% while the top view is in "normal" view, you could possibly call setRotation method, available since API 11
view.setRotation(90);
edit: below is my old answer, please see above my updated question
Do not lock the view in landscape. Instead use the layout/ and layout-land/ folders to place appropriate views. For example:
layout/main.xml
<FrameLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<include layout="#layout/main_content"/>
</FrameLayout>
layout-land/main.xml
<FrameLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”>
<include layout="#layout/main_content"/>
<FrameLayout
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:padding="144dp"
background="#4000">
<include layout="#layout/top_content"/>
</FrameLayout>
</FrameLayout>

Related

Android layout orientation change in landscape mode

I have a FrameLayout inside my fragment, and three other layouts inside my FrameLayout. I want one of the three layouts to change its view positions on a landscape, so I created two XML files for this layout and put them inside layout-port and layout-land accordingly, but the layout is still using the portrait layout in landscape mode. So what else do I need to do?
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
.
.>
<include
layout="#layout/xxx"
... />
<include
layout="#layout/xxx2"
... />
...
</FrameLayout>
I want to change the layout of xxx on a landscape, and I created 2 XML for xxx.
The default folder for layout is layout no need to add port suffix to it, when you create a landscape version you should make sure that its in the folder layout-land and the device will automatically select this layout version at runtime. for more information on supporting different screen sizes look at this documentation

IllegalArgumentException on orientation change using different layouts for landscape & portrait

I have a Fragment which uses different layouts in landscape and portrait orientation. In landscape mode, the root element is a LinearLayout, while in portrait mode it is a ScrollLayout:
res/layout/fragment.xml
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".NewBathingSiteFragment"
tools:showIn="#layout/activity_new_bathing_site"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/nbs_scrollView">
....
res/layout-land/fragment.xml
<LinearLayout
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".NewBathingSiteFragment"
tools:showIn="#layout/activity_new_bathing_site"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/nbs_linearLayout">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/nbs_scrollView">
...
I'm getting this error on orientation change:
java.lang.IllegalArgumentException: Wrong state class, expecting View State but
received class android.widget.ScrollView$SavedState instead. This usually
happens when two views of different type have the same id in the same hierarchy.
This view's id is id/newBathingSiteFragment. Make sure other views do not use
the same id.
No two views have the same ID in the same layout (they do have the same ID in the two different layouts, because I need to find them using findViewById()). The ID newBathingSiteFragment is the ID for the Fragment, and is set in XML in the activity layout.
I was able to make the problem go away by adding a LinearLayout as the root element for portrait mode as well, making the structure identical to landscape mode (except that in landscape mode there is one more view in the LinearLayout displaying next to the ScrollView).
Is this the preferred solution to this? Or is there a better way, without adding an "unnecessary" LinearLayout to portrait mode?

Rotated ListFragment disappears when empty view displayed after Filter

Just to be clear, we're talking about an XML rotated view here, not the effects of rotating the device. I have a SlidingDrawer that contains a ListFragment. The ListFragment implements the Filterable interface so that users can search its contents by providing a string input through an EditText.
The relevant layout is included below. Because the SlidingDrawer class was deprecated in API 17, the source code was copied over an accessed via a local class. That's why the name of that view looks like a custom class when really it's not.
<com.example.echo.views.SlidingDrawer
android:id="#+id/left_sliding_drawer"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:handle="#+id/left_handle"
android:orientation="horizontal"
android:content="#+id/people_fragment_container"
android:rotation="180">
<LinearLayout
android:id="#id/left_handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/people_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/people_map_tab_grey"
android:rotation="180"
android:contentDescription="#string/people_tab"/>
</LinearLayout>
<FrameLayout
android:id="#+id/people_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.example.echo.views.SlidingDrawer>
What happens is, when the users provides input and filters the list such that there are no matching results, i.e., the ListView is empty and size is 0, the entire SlidingDrawer disappears.
Some things I've noticed in trying to fix this:
I am pretty sure this is related to displaying the empty view and/or whatever layout change occurs when it is displayed. If I simply do not set an empty view for the ListFragment the issue does not occur.
I am also pretty sure the effects are related to the fact that the SlidingDrawer is being rotated by 180 degrees since, if I remove the rotation attribute, the issue also does not occur. However, because SlidingDrawer in its default state only opened right to left, this drawer applies the XML attribute android:rotation="180" to flip the view so that it can be opened left to right. This must remain since there is other stuff on the right side of the screen that cannot be moved.
I'm not sure what is making the view disappear or where to start fixing it. I've trying fixing the child views' sizes by overriding onMeasure, onSizeChanged, and onLayout but cannot find anything that solves the issue.
Any ideas are appreciated.

Layout Orientation conflicting with manifest

In the manifest file of this application, I have specified the screen orientation to be landscape for each activity. However, in each layout file it lists android:orientation="vertical" as default. The application sometimes crashes, sometimes it doesn't, could these values be conflicting?
No way, it is different things at all.
Landscape for activity mean how to show activity for user
android:orientation="vertical" for LinearLayout mean how LinearLayout will draw its children views
android:orientation in layout defines how the child view will be stacked on the screen (for example see LinearLayout). The orientation in the manifest is all about the screen orientation (or the orientation of specific Activity).

android best layout to display image button that will fills the screen

what's the best layout to use to display a series of image button, side by side (as displayed in attached image), and that will fills the screen horizontally when the orientation changes to horizontal (as attached)?
horizontal orientation http://img254.imageshack.us/img254/657/layoutxs.png
vertical orientation http://img208.imageshack.us/img208/2428/layouthy.png
<GridView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit" >
</GridView>
It looks like you want to use something like FlowLayout, which doesn't exist in Android. You can make your own, however. More information
How can I do something like a FlowLayout in Android?
http://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/
I have an activity with a layout similar to yours, but I use a different method. What I do is define a different xml layout for landscape and portrait. So I have layout-port/mylayout.xml and layout-land/mylayout.xml and the system determines which one the activity should use.
just create two separate layout files and call setContentView() in your onOrientationChanged().

Categories

Resources