Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - EditText height resize based on keyboard

I am having an activity in android where there is an editbox and 3 buttons below the editbox. Please see attachment. When this activity is launched, the default state is STATE1.(Please see image). The keyboard is visible by default. Now when I press the back button or dispose the keyboard, I wish to have the edittext resized and occupy the whole screen as shown in STATE2.

I am not sure how to accomplish this. I have the height of the edittext hardcoded to some dp based on the target device. I believe this has to be changed. Can anyone help me in how to accomplish this.

The XML of the layout file is below as well as the 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

like image 281
Timothy Rajan Avatar asked Dec 05 '13 10:12

Timothy Rajan


2 Answers

Change your 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 upon you to decide. The behaviour will be different and you can try it out. Here is what the documentation says about adjustPan:

The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

Your buttons at the bottom may get hidden if using this. Instead use adjustResize: Put this line in the activity inside AndroidManifest

android:windowSoftInputMode="stateVisible|adjustResize"
like image 118
Amulya Khare Avatar answered Sep 22 '22 04:09

Amulya Khare


Try this.. in your manifest.

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

and your edittext use <requestFocus /> for from that start itself it'll 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>
like image 27
Hariharan Avatar answered Sep 24 '22 04:09

Hariharan