Android - keyboard-based EditText text resizing

I have activity in android where there is an edit box and 3 buttons below the edit box. Please pay attention to the attached file. When this action is run, the default state is STATE1 (see Image). By default, the keyboard is visible. Now, when I press the back button or delete the keyboard, I want the edittext to resize to fit the entire screen, as shown in STATE2.

I am not sure how to do this. I have an edittext hardcoded height for some dp based on the target device. I believe that this needs to be changed. Can anyone help me on how to do this.

XML layout file below as well as screencap

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FFFFFF" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <EditText android:id="@+id/EditMessage" android:layout_width="fill_parent" android:layout_height="150dp" android:background="@drawable/newback" android:gravity="top" android:imeOptions="actionDone" android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions" android:maxLength="200" android:padding="5dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <Button android:id="@+id/PostMessage" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" android:layout_marginRight="0.2dp" android:background="@drawable/newbutton_corner" android:text="@string/SubmitMessage" android:textColor="#FFFFFF" android:textStyle="bold" /> <Button android:id="@+id/CancelMessage" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" android:layout_marginRight="0.2dp" android:background="@drawable/newbutton_corner" android:text="@string/CancelMessage" android:textColor="#FFFFFF" android:textStyle="bold" /> <Button android:id="@+id/DeleteMessage" android:layout_width="0dp" android:layout_height="45dp" android:layout_weight="1" android:background="@drawable/newbutton_corner" android:text="@string/DeleteMessage" android:textColor="#FFFFFF" android:textStyle="bold" /> </LinearLayout> </LinearLayout> </RelativeLayout> 

enter image description here

+6
source share
4 answers

Modify the EditText as follows:

 <EditText android:id="@+id/EditMessage" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:imeOptions="actionDone" android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions" android:maxLength="200" android:padding="5dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" /> 

Add layout_weight = 1

In my opinion, you do not need adjustPan . I will leave it to you to decide. The behavior will be different and you can try it. Here's what the documentation for adjustPan :

The main operation window does not change to make room for a soft keyboard. Rather, the contents of the window automatically expand so that the current focus is never hidden by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, as the user may need to close the soft keyboard to receive and interact with the shaded parts of the window.

Your buttons below may be hidden when using this. Use adjustResize : Put this line in action inside AndroidManifest

 android:windowSoftInputMode="stateVisible|adjustResize" 
+6
source

Try this .. in your manifest.

 <activity android:name="Activity" android:windowSoftInputMode="adjustPan"/> 

and your edittext uses <requestFocus /> , because from the very beginning it will focus

and android:layout_weight="1" it will automatically fill the screen.

  <EditText android:id="@+id/EditMessage" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#696969" android:gravity="top" android:imeOptions="actionDone" android:inputType="textMultiLine|textFilter|textVisiblePassword|textNoSuggestions" android:maxLength="200" android:padding="5dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" > <requestFocus /> </EditText> 
+5
source

Yes, you can do it, simply.

  • android:windowSoftInputMode="adjustResize" , add this attribute to you, appearing at the required activity.
  • Set the width and height of your text view to match_parent

Done.

0
source

add android:windowSoftInputMode="adjustPan" and change android:layout_height="150dp" to android:layout_height="fill_parent"

0
source

Source: https://habr.com/ru/post/959373/


All Articles