0

When I hit enter key many times my top layout is moving up, how to solve this?

The only way I know is adding attribute in the manifest.xml, but doing this will make I can't see the cursor when at the bottom screen.

like below:

<activity
    ...
    android:windowSoftInputMode="adjustNothing" />

my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:paddingBottom="4dp"
        >
        <RelativeLayout
            android:id="@+id/toolsLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
            ...
        </RelativeLayout>
    </com.google.android.material.appbar.AppBarLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginHorizontal="8dp"
        android:layout_marginVertical="8dp"
        >
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/inputText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingHorizontal="12dp"
            android:paddingVertical="8dp"
            android:gravity="top"
            android:lineSpacingExtra="4dp"
            android:textSize="16sp"
            android:textColor="#6F5CB3"
            />
            </RelativeLayout>
</LinearLayout>
noe256471
  • 75
  • 7

1 Answers1

1
  1. Set the max lines of your textInputEditText to 1

Simply set the max number of lines to 1 in your textInputEditText through the following attribute:

android:maxLines="1"

As a result, you will not be able to press the enter key(to go to a new line) in your textInputEditText, and this unwanted behavior will not occur.

  1. Use the adjustResize attribute

You can also try using this attribute:

android:windowSoftInputMode="adjustResize"

...to allow the system to resize and make sure all the content in your app is accessible.

CodingChap
  • 1,088
  • 2
  • 10
  • 23
  • Do you know the difference between this flags?`android:configChanges="keyboard|keyboardHidden|orientation|screenSize"` – noe256471 Jun 02 '21 at 16:02
  • Hi, `orientation` prevents restarts when the orientation changes, `screen size` does the same **but** for android 3.2+, and `keyboardHidden` prevents restarts when the keyboard availibility changes. I'm not sure about `keyboard`. Also, be sure to check out these links: https://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation, https://developer.android.com/guide/topics/resources/runtime-changes – CodingChap Jun 02 '21 at 16:19