1

I have a simple ScrollView set up in a landscape layout. When I rotate the device from portrait to landscape mode, the UI screen scrolls down a bit and the topmost portion of the view is no longer visilble. How do I force the view to start at the top of the view? I tried setting "mScrollView.scrollTo(0,0);" programmatically but with no luck. Please advise.

partial land\layout.xml file:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:focusableInTouchMode="true"
tools:context=".CardViewActivity">

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" >
</include>

<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  >

<LinearLayout
    android:id="@+id/LinearLayout1"
    style="@style/scrollbar_shape_style"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/rounded_corner"
    android:layout_marginLeft="6dp"
    android:layout_marginStart="6dp"
    android:layout_marginRight="6dp"
    android:layout_marginEnd="6dp"
    android:layout_marginTop="6dp"
    android:layout_marginBottom="6dp"
    android:useDefaultMargins="false"
    android:orientation="vertical"
    android:paddingRight="2dp"
    android:paddingEnd="2dp"  >

partial Activity file:

...
private ScrollView mScrollView;

@Override
protected void onResume() {
    super.onResume();
mScrollView = (ScrollView) findViewById(R.id.ScrollView1);
    if (mScrollView!=null) {
        mScrollView.setSmoothScrollingEnabled(false);
        mScrollView.fullScroll(ScrollView.FOCUS_UP);
        mScrollView.setSmoothScrollingEnabled(true);
    }
}
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

0

After understanding the background, I would advice to use the same layout, with scrollview, for portrait as well. Scrollview would do the job automatically.

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
  • I am only using the Scrollview in a land\layout file. The portrait layout file does not use a Scrollview. So when I set up mScrollView in the Activity it crashes--I think becuase it is trying to set the mScrollView on the portrait layout and their is no ScrollView to be set in that portrait layout. Would I have to set up the fullScroll code in onResume but use some type of "if" statement to only use the code only if the device is in landscape orientation? – AJW Sep 13 '15 at 03:29
  • you should simply check before you use your `mScrollView` like this `if (mScrollView!=null) { //do your stuff with mScrollView }` – Derek Fung Sep 13 '15 at 03:31
  • My apologies. I have just accepted the answer. How does the "!null" negate the mScrollView being used in portrait mode if mScrollView is set in the Activity? – AJW Sep 13 '15 at 03:38
  • `mScrollView = (ScrollView) findViewById(R.id.ScrollView1);` when you get the `mScrollView`, if it fails to find, the function will returns **null**, which is a special state for Object in Java, meaning it is nothing. `!=` means NOT equal, so the whole statement `mScrollView != null` means it is not **nothing**, in portrait mode, because you have not define the view in your layout, mScrollView will be set to `null`, but as you check everytime, you will not try to work on it which would result in error. – Derek Fung Sep 13 '15 at 03:42
  • Ok, I put the fullScroll code in the onResume method. I edited my question above to show the onResume section. I tested and it does not work. I also tried "scrollTo(0,0)" and that did not work either. Thoughts? – AJW Sep 13 '15 at 03:49
  • your scrollview should not define height as wrap content – Derek Fung Sep 13 '15 at 03:54
  • scrollview cannot depends on child for height – Derek Fung Sep 13 '15 at 03:55
  • Ok, I changed height to match_parent. I also edited my question to show the top portion of the land\layout file in case that helps. I tested with match_parent and it still does not work. Thoughts? – AJW Sep 13 '15 at 04:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89499/discussion-between-derek-fung-and-ajw). – Derek Fung Sep 13 '15 at 04:06