0

I have limited space in one of my views and I am trying to implement a Scroll View which can help to resolve the limited space issue. However when I try to implement the scroll view it messes-up the keyboard, every time I now try to edit a EditText it causes the elements within the view to move up with the keyboard.

Is there any other solutions or work arounds which I can try to implement?

I have tried the solution at Move layouts up when soft keyboard is shown? which suggest to add the following code to the Manifest android:windowSoftInputMode="stateHidden|adjustResize which prevents the keyboard from jumping up at runtime.

I have also tried to set the gravity of the scrollview and use different layouts example: linear, constraint, relative-layouts. I have tried to make a custom keyboard class and to implement it by setting it in the relevant view activity class (Not my code but tried to implement it)

Here is the solution for this fix
- android:windowSoftInputMode="adjustResize" in Manifest File
- Make a class "CommentKeyBoardFix.Java" and copy and paste the below code.

public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();

public CommentKeyBoardFix(Activity activity)
{
    FrameLayout content = activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}

private void possiblyResizeChildOfContent()
{
    int usableHeightNow = computeUsableHeight();
    if (usableHeightNow != usableHeightPrevious)
    {
        int heightDifference = 0;
        if (heightDifference > (usableHeightNow /4))
        {
            // keyboard probably just became visible
            frameLayoutParams.height = usableHeightNow - heightDifference;
        }
        else
        {
            // keyboard probably just became hidden
            frameLayoutParams.height = usableHeightNow;
        }
        mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
        mChildOfContent.requestLayout();
        usableHeightPrevious = usableHeightNow;
    }
}

private int computeUsableHeight()
{
    mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
    return contentAreaOfWindowBounds.height();
}
}

And then call this class in your Activity or Fragment
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this) ---> For Kotlin
or
new CommentKeyBoardFix(this) ---> For Java

This gives a static member error which I don't know how to solve.

However none of these solved the problem.

My current layout file which is causing the error.

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:id="@+id/main_layout1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/freq_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"
                android:text="Freq"
                android:textSize="14sp" />

            <EditText
                android:id="@+id/freq"
                android:layout_width="80dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:digits="0123456789.,"
                android:inputType="numberDecimal"
                android:textAlignment="center"
                android:textColor="@color/black" />

            <Spinner
                android:id="@+id/spinner_r1_unit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:tooltipText="unit"

                />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="5dp">

            <TextView
                android:id="@+id/duty_cycle_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/duty_cycle"
                android:textSize="14sp"
                android:layout_marginLeft="5dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/duty_cycle_percentage"
                android:layout_marginLeft="5dp"
                android:text="50%"/>


        </LinearLayout>

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="180dp"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:max="100"
            android:progress="50" />

        <android.support.v7.widget.AppCompatButton
            android:layout_width="100dp"
            android:layout_below="@id/main_layout1"
            android:layout_height="wrap_content"
            android:text="Design"
            android:layout_marginLeft="30dp"
            android:id="@+id/button_design"
            android:layout_marginTop="10dp"
            android:background="@drawable/white_rounded_button"
            />

        <ScrollView
            android:layout_width="200dp"
            android:layout_height="100dp"
            >

        </ScrollView>

    </LinearLayout>



</merge>

If I remove the scroll view it solves the problem however, I need to add a scroll view since this .xml file is used as a snippet in a different .xml file where the space is very limited.

Is there any other possible work-around of the implementation of a scroll view? Is there any good explanation to why this scroll view is causing this bug and considering the space issue what other alternatives are there that I can take into account, which can deliver similar results to that of a scroll view, without causing the keyboard error?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Johan Fick
  • 375
  • 2
  • 14

0 Answers0