Change ScrollView into HorizontalScrollView - android

I have two layouts defined mainactivity.xml which is found in the layout folder, and another mainactivity.xml which is found in the layout-port folder. As you can probably tell one layout is for Landscape orientation and the other is for Portrait.
The issue I'm having is that in the Landscape layout I have a ScrollView, and in the Portrait layout it is a HorizontalScrollView with the same id. How do I change (in code) the ScrollView into a HorizontalScrollView when the orientation of the device changes?

You could create a bools.xml file in res/values-land (and also one in res/values-port in which is_landscape should be set to false)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">true</bool>
</resources>
Then in your code you can check
if (getResources().getBool(R.bool.is_landscape)) {
//setup horizontal scrollview
} else {
//setup vertical scrollview
}
Although perhaps a better way is to use this library (so you don't have to code for two different ui components).
https://github.com/lucasr/twoway-view
I'm using this in a current project and it works beautifully.

In your scrollview define this property
android:orientation="horizontal" for landscape
android:orientation="vertical" for portrait

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?

Landscape ViewPager

When I change my orientationon landscape my photo will be only half ... I want when I change my orientation to be like on facebook , to show full picture .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
You have to create another layout for horizontal layout. if you want your portrait view to be different from your landscape view. you must create another XML file for landscape.
The two XML file will be handled by the same JAVA code. although you can programmatically listen to orientation change but would prefer the different layout for Different Orientation.
create two types of layout directories to handle orientation . layout-land layout-port put the xml with the same name in both the directory. example if you have main.xml file then add have to put it in both directory.
Documentation on supporting multiple screen

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

Landscape resource not picked up via LayoutInflater

I am trying to dynamically add buttons to table rows, but need to alter the style of the buttons based on screen orientation. I have the following "game_answer_button.xml" layout file in both "layout" and "layout-land" folders.
res/layout:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
res/layout-land:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="#android:style/Widget.Button.Small"
/>
The button layout is being inflated in my activity as follows:
LayoutInflater inflater = getLayoutInflater();
Button button = (Button)inflater.inflate(R.layout.game_answer_button, null);
button.setText(image.getDescription());
However, it seems the default (portrait) layout is selected every time. If the contents of the default layout are replaced with the landscape layout, then the landscape layout is finally rendered. It doesn't seem to be correctly selecting the landscape layout resource via LayoutInflater.
Any ideas on this?
did you check if the configuration of the activity, where the button is used, in the manifest is set to portrait only?
I found the problem. I was loading the dynamic buttons to an ArrayList and merely adding the buttons to my parent view on each configuration change--meaning the button style would have already been inflated when pulling the button objects from "cache". Just another Dev "doh!" moment.

Categories

Resources