0

I have the folowing layout

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/DarkGray"
    android:orientation="vertical">


        <EditText
            android:id="@+id/etCaption"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="1"           
            android:gravity="start|top" />

        <TextView
            android:id="@+id/tvCounter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_gravity="right" />


</LinearLayout>

Which essentially has Edit text occupying the screen and textview at the bottom.

When I click on edit text then the keyboard shows up and it covers the text view at the bottom.

How can I ensure the textview is pushed up so it is visible to the user while he is typing in the edit text.

I had it orginally as constraint layout and I changed it to linrear layout and I still can't get it to work

Thanks

Snake
  • 14,228
  • 27
  • 117
  • 250

3 Answers3

0

a node with layout_height="match_parent" won't be pushed into any direction, because the layout has no space to move; this should be layout_height="wrap_content" ( "show layout boundaries"). adding android:windowSoftInputMode="adjustResize" or "adjustPan|adjustResize" to the activity in Manifest.xml should resize the layout, when the keyboard appears & disappears.

The activity's main window is always resized to make room for the soft keyboard on screen.

and that (or quite similar) effect is provided by material TextInputLayout:

anatomy of a TextInputLayout

api "com.google.android.material:material:1.0.0"

for example (notice the app:counterEnabled and app:counterMaxLength attributes):

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/caption"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="start"
        android:hint="@string/hint_caption"
        android:inputType="textNoSuggestions"
        android:text="@={item.caption}"

        app:counterEnabled="true"
        app:counterMaxLength="50"/>

</com.google.android.material.textfield.TextInputLayout>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • Thank you Martin. But if I use wrap_parent at the root then the Edit text will shrink to one line! I want the edit text to take as much as space as available. What do you suggest so the text view below appears? I tried the adjustsize and it didn't work. I can't use material component as I am using appcompat and plus the above code is for edit text. I want the textview to appear – Snake Jan 27 '19 at 08:21
0

You need to this statement to manifest.xml.

android:windowSoftInputMode="stateVisible|adjustResize"

Make sure its under the activity the textview is declared in.

Gayan Mettananda
  • 1,498
  • 14
  • 21
0

To adjust layout if keyboard is open

Try this code: manifest

<activity android:name=".MainActivity"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

In which activity you want to adjust keyboard add this line

android:windowSoftInputMode="adjustResize"

see screen Shots:

enter image description here

i hope its work for you

white hills
  • 237
  • 1
  • 8