0

I have a LinearLayout that is works great on orienation change. I then wrapped the LinearLayout in a ScrollView and now I have a problem. The vertical scrolling works fine but when there is an orientation change to landscape mode the View scrolls down a bit so now the top of the layout is cut off. The ScrollView appears to be responding to requestFocus() code on an EditText line that is just below the top of the View. But the View is plenty big enought to show the top of the View and the EditText line. How do I force the View after an orientation change to not scroll down so that it can show the top of the layout and the EditText line?

partial layout 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="0dp"
    android:layout_weight="1"  >

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

partial Activity file:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.cardviewinput);

    cListenerEditText = (ListenerEditText) findViewById(R.id.CEditText);
    cListenerEditText.requestFocus();
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

0

Put the following attribute inside the activity tag in AndroidManifest.xml android:windowSoftInputMode="adjustPan"

Aditya Naique
  • 1,120
  • 13
  • 25
  • I currently have that set to "stateVisible|adjustResize" because I use a soft keyboard for user input which shows below the main View. Thoughts? – AJW Sep 13 '15 at 19:34
  • Try changing it to "stateVisible|adjustPan" – Aditya Naique Sep 13 '15 at 19:35
  • Ok I will try that. What will "adjustPan" do rather than "adjustResize"? – AJW Sep 13 '15 at 19:39
  • Ok. And the reason that changing to adjustPan would not be "less desirable" is because the ScrollView allows the user to just scroll down to see the remaining portions of the View? – AJW Sep 13 '15 at 19:42
  • Oh yes, just realized that issue. I guess in that case the only solution is the user pressing the NEXT key on his keyboard to get to the next editText, or pressing DONE to close the keyboard. – Aditya Naique Sep 13 '15 at 19:47
  • But wait, maybe I wasn't clear. I think adjustPan with a softkeyboard may be a problem with a layout that does not have a ScrollView. Since the layout I'm using does have a ScrollView then adjustPan seems to work fine, to keep the View at the top with focus on the first EditText line. Then if user wants to scroll down to the remaining EditText lines they can do so. So are there any negatives then to using adjustPan in my case? – AJW Sep 13 '15 at 19:51
  • Yes, that answer is right - adjustPan is not very UX friendly. Just tried it in my layout which uses a scrollView and it doesn't let me scroll below the EditText in focus. adjustResize on the other hand works fine though. – Aditya Naique Sep 13 '15 at 20:00
  • Ok, I will re-set back to adjustResize. Would there be a way to gain focus at the top of the View on orienation change (use an invisible widget?) so the View draws correctly and then move the focus down to the first EditText line? – AJW Sep 13 '15 at 20:03
  • hey sorry to confuse you :) if adjustPan works fine in your case, then by all means go ahead. There are no negatives in using it. – Aditya Naique Sep 13 '15 at 20:05
  • adjustPan is not what I'm looking for because it does not allow the scrolling down to the lower portions of the View. I have to find a solution with adjustResize...any thoughts? – AJW Sep 13 '15 at 20:07
  • Check the selected answer in this thread http://stackoverflow.com/q/22141168/1944049 – Aditya Naique Sep 13 '15 at 20:13
  • I read the the thread. One difference is that I always have the keyboard open. So not sure how the thread can help...what do you think? – AJW Sep 13 '15 at 20:36