64

When I try to write something in an EditText which is at the bottom of the screen, the soft keyboard hides the EditText. How can I resolve this issue? Below is my xml code. I'm using this in a Fragment.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >


        <LinearLayout
            android:id="@+id/linearLayoutTopDetails"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayoutTop"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="6dp"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/list_design1"
                android:orientation="vertical" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="80dp"
                    android:layout_weight="100"
                    android:orientation="horizontal" >

                    <ImageView
                        android:id="@+id/ImageViewProfImagePostDetail"
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_marginTop="2dp"
                        android:layout_weight="24.84"
                        android:paddingLeft="5dp"
                        android:paddingRight="5dp"
                        android:paddingTop="5dp" />

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="60"
                        android:orientation="vertical" >

                        <TextView
                            android:id="@+id/textViewNamePostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Robin"
                            android:textSize="17sp" />

                        <TextView
                            android:id="@+id/textViewLocationPostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="1dp"
                            android:drawableLeft="@drawable/location"
                            android:text="beijing, China" />

                        <TextView
                            android:id="@+id/textViewTimeAgoPostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text=" 18min ago" />
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="0dp"
                        android:layout_height="fill_parent"
                        android:layout_weight="15"
                        android:orientation="horizontal" >

                        <TextView
                            android:id="@+id/textViewReportAbusePostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:background="@drawable/flag"
                            android:paddingLeft="5dp" />

                        <TextView
                            android:id="@+id/textViewDeletePostDetail"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/delete"
                            android:visibility="gone" />
                    </LinearLayout>
                </LinearLayout>

                <TextView
                    android:id="@+id/textViewDescriptionPostDetail"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="2dp"
                    android:singleLine="false"
                    android:text="Description or caption of the post"
                    android:textSize="17sp" />
            </LinearLayout>

            <ImageView
                android:id="@+id/feedPostedImagePostDetail"
                android:layout_width="fill_parent"
                android:layout_height="200dp"
                android:layout_marginTop="5dp"
                android:scaleType="fitCenter"
                android:visibility="gone" />

            <LinearLayout
                android:id="@+id/linearLayoutOptions"
                android:layout_width="fill_parent"
                android:layout_height="33dp"
                android:layout_marginTop="2dp"
                android:layout_weight="100"
                android:background="@drawable/bar"
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/textViewShakePostDetail"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_marginLeft="15dp"
                    android:layout_weight="25"
                    android:drawableLeft="@drawable/like"
                    android:gravity="center"
                    android:singleLine="true"
                    android:text="Shake"
                    android:textSize="12sp" />

                <TextView
                    android:id="@+id/textViewCommentPostDetail"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="41"
                    android:drawableLeft="@drawable/comment"
                    android:gravity="center"
                    android:paddingLeft="20dp"
                    android:text="Comment" />

                <TextView
                    android:id="@+id/textViewSharePostDetail"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:layout_weight="33"
                    android:drawableLeft="@drawable/share"
                    android:gravity="center"
                    android:paddingLeft="20dp"
                    android:text="Spread" />
            </LinearLayout>
        </LinearLayout>

        <ListView
            android:id="@+id/listViewFeedsDetail1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/linearLayoutTopDetails"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="6dp" >
        </ListView>

        <LinearLayout
            android:id="@+id/linearLayoutPostcomment"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/listViewFeedsDetail1"
            android:background="#FFFFFF"
            android:orientation="horizontal"
            android:weightSum="100" >

            <EditText
                android:id="@+id/editTextComment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="80" />

            <Button
                android:id="@+id/buttonPostComment"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="20"
                android:background="#F0F8FF"
                android:text="Post" >
            </Button>
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

I have tried to resolve this issue by adding changes through code but no success.

Below is the image before and after clicking on the EditText.

screenshot without soft keyboard, EditText visible

screenshot with soft keyboard, EditText hidden

Robin Royal
  • 1,788
  • 1
  • 18
  • 29
  • 2
    scrollview is not working. – Robin Royal Jun 17 '14 at 11:28
  • 1
    Add this properties : android:windowSoftInputMode="adjustPan|adjustResize" to your Activity tag in AndroidManifest.xml – Haresh Chhelana Jun 17 '14 at 11:34
  • possible duplicate of [Android soft keyboard covers edittext field](http://stackoverflow.com/questions/3295672/android-soft-keyboard-covers-edittext-field) – Lamorak Aug 19 '15 at 17:03
  • 1
    @HareshChhelana I think either of adjustPan or adjustResize should be used, and not both at a time. They are contrary to each other – Shirish Herwade Nov 27 '15 at 11:46
  • This seems to be a bug in android when `adjustPan` mode is set Try this Custom `EditText` with workaround for this problem https://gist.github.com/Arjun-sna/768de4ca141798c59a8166a2e5d4a743 – arjun Feb 21 '17 at 12:01

11 Answers11

100

In manifest add this line on the particular activity tag:

 android:windowSoftInputMode="adjustPan" 
Aniruddh Parihar
  • 3,072
  • 3
  • 21
  • 39
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
  • 11
    I've tried this but no success. android:windowSoftInputMode="adjustPan|adjustResize" This code is still in my manifest.but is still hiding editText. – Robin Royal Jun 17 '14 at 11:23
  • 2
    @RobinRoyal JFTR, adjustPan|adjustResize is NOT the same than adjustPan alone. In fact, if what other user has said later is true, you should never ever mix two adjust* states. – Fran Marzoa Nov 13 '19 at 10:59
22

For that you have to declared in your activity manifeast

         <activity
            android:name=".activityname"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan|adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  • This is my activity manifest code. This is still hiding editText. – Robin Royal Jun 17 '14 at 11:25
  • 1
    It is fragment not an activity.Where should I do that? I've already placed this code in activity in which fragment is placed. – Robin Royal Jun 17 '14 at 11:32
  • 10
    @RobinRoyal may be I'm late, but(my be useful for others) use either adjustPan or adjustResize, not both at a time – Shirish Herwade Nov 27 '15 at 11:45
  • 2
    Don't set two "adjust..." values together as document says: The setting must be one of the values listed in the following table, or a combination of one "state..." value plus one "adjust..." value. Setting multiple values in either group — multiple "state..." values, for example — has undefined results. – EmJiHash Nov 22 '16 at 08:34
  • Here's doc for more info : https://developer.android.com/training/keyboard-input/visibility – Hanako Jan 05 '21 at 17:25
16

Here is simple solution for Android EditText issue of hiding behind SoftKeypad. Use the code in AndroidManifest.xml file of the project module.

<activity
        android:name="com.example.MainActivity"
        android:label="@string/activity_main"
        android:windowSoftInputMode="adjustResize|stateHidden" />

This code worked for me.
Inside the manifest file in activity tag add this attribute:

android:windowSoftInputMode="adjustResize|stateHidden"

There are more such values for this attribute (android:windowSoftInputMode) which will come as recommendation list. You can check with them also.

Rahul Raina
  • 3,322
  • 25
  • 30
10

Declare windowSoftInputMode="adjustResize" in manifest.xml file

<activity
        android:name=".example"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="adjustResize" />

provide scroll view to the xml layout file

<RelativeLayout
         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:paddingBottom="16dp"
         android:paddingLeft="16dp"
         android:paddingRight="16dp"
         android:paddingTop="16dp"
         android:fitsSystemWindows="true">

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fillViewport="true"
        android:isScrollContainer="false">

   // add edittext here...

 </ScrollView>
 </RelativeLayout>
Kishor N R
  • 1,521
  • 1
  • 17
  • 23
3

Use the below code where:

InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                ipmm.hideSoftInputFromWindow(url.getWindowToken(), 0);

where url in my code is:

url = (EditText) findViewById(R.id.eT_webbrowser);

or Try this:

InputMethodManager ipmm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    ipmm.hideSoftInputFromWindow(null, 0);

As another option try this: This always hides the soft input mode such that your EditText is visible

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Ramakishna Balla
  • 1,020
  • 1
  • 8
  • 12
2

I handled that by adding ScrollView around View with EditText inside.

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        // Other views
        <EditText ... />
</ScrollView>

Adding tags to Android Manifest didn't help me.

Kirill Smirnov
  • 131
  • 2
  • 12
0

Put this line in your application tag:-

android:windowSoftInputMode="stateHidden|adjustPan"

example:-

<application
    android:name=".application.MyApplication"
    android:allowBackup="false"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:largeHeap="true"
    android:networkSecurityConfig="@xml/network_security_config"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="false"
    android:theme="@style/AppTheme"
    android:windowSoftInputMode="stateHidden|adjustPan"
    tools:ignore="GoogleAppIndexingWarning"
    tools:replace="android:supportsRtl,android:allowBackup"
    tools:targetApi="n">
M. H.
  • 960
  • 11
  • 13
0

Add this line on your manifest file on your activity and see the magic.

android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

Vijendra patidar
  • 1,444
  • 1
  • 14
  • 24
0

Try this, it might be help you.

Add the following line to your EditText

android:paddingVertical="16dp"
Razz
  • 452
  • 6
  • 9
0

For users using adjustPan and Fullscreen below is the answer.

// Set the OnApplyWindowInsetsListener to handle window insets changes

    ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, insets ->

        // Check if the bottom inset (keyboard) is non-zero and adjust the layout accordingly

        if (insets.isVisible(WindowInsetsCompat.Type.ime())) {
// Keyboard height if you need

    //          val keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
                val windowRect = Rect()
                view.getWindowVisibleDisplayFrame(windowRect)

        val editTextRect = Rect()
        mEditText.getGlobalVisibleRect(editTextRect)

        // Calculate the difference between the bottom of the editText and the windowRect
        val diff = editTextRect.bottom - windowRect.bottom

        if (diff > 0) {
            view.scrollBy(0, diff.plus(SET_YOUR_DESIRE_MARGIN_HERE))
        }
    } else {
        view.scrollTo(0, 0)
    }

    // Return the insets to keep the system default behavior
    insets
}

For API level 29 and below you have to set SOFT_INPUT_ADJUST_RESIZE in your Activity or Fragment. You can reset it in lifecycle event call back based on your need. Like below for Fragment, set it in onAttach ->

override fun onAttach(context: Context) {
        super.onAttach(context)
        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.R)
            requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
    }

and reset it in onDetach.

override fun onDetach() {
        if(Build.VERSION.SDK_INT<Build.VERSION_CODES.R)
            requireActivity().window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
        super.onDetach()
    }

Hope this will help future users.

Pravin Divraniya
  • 4,223
  • 2
  • 32
  • 49
-1

The solution for me was, simply, adding this to the parent layout : android:fitsSystemWindows="true"