22

Let say I have the following layout:

enter image description here

when I click on the edit text at the bottom I get this:

enter image description here

As you can notice the image from the top goes upper and the whole layout moves. This is my xml for this layout:

<?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:id="@+id/header"
       android:layout_width="fill_parent"
       android:layout_height="75dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true"
       android:layout_alignParentTop="true"        
       android:isScrollContainer="true"
       android:background="@drawable/header" >
   </LinearLayout>

   <ScrollView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:id="@+id/scrollauthentication"
       android:layout_below="@+id/header"
       android:background="#FFFFFF"
       android:fillViewport="true" >

       <RelativeLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:background="#FFFFFF" 
           android:id="@+id/authenticationrelativelayout">

           <TextView
               android:id="@+id/login"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentLeft="true"
               android:layout_below="@+id/header"
               android:layout_marginLeft="30dp"
               android:layout_marginTop="20dp"
               android:text="login"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/lighter_orange"
               android:textSize="28dp" />

           <TextView
               android:id="@+id/usernameTextView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/login"
               android:layout_below="@+id/login"
               android:layout_marginTop="16dp"
               android:text="username"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/dark_gray" />

           <EditText
               android:id="@+id/user"
               android:layout_width="260dp"
               android:layout_height="42dp"
               android:layout_alignLeft="@+id/usernameTextView"
               android:layout_below="@+id/usernameTextView"
               android:background="@drawable/edittext_selector"
               android:imeOptions="actionDone"
               android:lines="1"
               android:paddingLeft="10dp" />

           <TextView
               android:id="@+id/passwordTextView"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/user"
               android:layout_below="@+id/user"
               android:text="password"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/dark_gray" />

           <EditText
               android:id="@+id/password"
               android:layout_width="260dp"
               android:layout_height="42dp"
               android:layout_alignLeft="@+id/passwordTextView"
               android:layout_below="@+id/passwordTextView"
               android:background="@drawable/edittext_selector"
               android:imeOptions="actionDone"
               android:inputType="textPassword"
               android:lines="1"
               android:paddingLeft="10dp" >

               <requestFocus />
           </EditText>

           <CheckBox
               android:id="@+id/remembercheckBox"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignLeft="@+id/password"
               android:layout_below="@+id/password"
               android:layout_marginTop="37dp"
               android:button="@drawable/checkbox_selector"
               android:focusable="true" />

           <TextView
               android:id="@+id/rememberText"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignBaseline="@+id/remembercheckBox"
               android:layout_alignBottom="@+id/remembercheckBox"
               android:layout_toRightOf="@+id/remembercheckBox"
               android:text="Remember me"
               android:textAppearance="?android:attr/textAppearanceMedium"
               android:textColor="@color/dark_gray" />
       </RelativeLayout>
   </ScrollView>

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginLeft="30dp"
       android:layout_below="@+id/scrollauthentication"
       android:orientation="horizontal" >

       <Button
           android:id="@+id/loginButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:background="@color/button_selected"
           android:text="log in"
           android:textColor="@drawable/textblack_selected"
           android:textStyle="bold" >
       </Button>

       <Button
           android:id="@+id/forgotten"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_marginLeft="10dp"
           android:background="@color/button_selected"
           android:text="forgotten password"
           android:textColor="@drawable/textblack_selected"
           android:textStyle="bold" />
   </LinearLayout>

</RelativeLayout>

And I have also set this in manifest file for this activity:

android:windowSoftInputMode="adjustPan"

So is there a way to stop my layout from moving when the keyboard is displayed? Thank you!!!!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
adrian
  • 4,574
  • 17
  • 68
  • 119
  • Check this link: http://stackoverflow.com/questions/9989130/page-scroll-when-soft-keyboard-poped-up – Yogesh Somani Jul 10 '12 at 09:26
  • I think this link will help you. [link][1] [1]: http://stackoverflow.com/questions/5516216/how-to-avoid-soft-keyboard-pushing-up-my-layout Thanks... – user4232 Jul 10 '12 at 09:27
  • @YogeshSomani there is nothing in there I haven't tried. and it is not working – adrian Jul 10 '12 at 09:32
  • well, I have not mentioned any "windowSoftInputMode" in my app's manifest and it runs fine. Screen does not move up when the keyboard opens up. Try this approach - just remove the "windowSoftInputMode" parameter from your manifest . – Yogesh Somani Jul 10 '12 at 09:43
  • @YogeshSomani it works. Post it as an answer, to accept it and upvote it – adrian Jul 10 '12 at 10:04

6 Answers6

32

Try using

android:windowSoftInputMode="adjustPan|adjustResize"

for the activity.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
SALMAN
  • 2,031
  • 1
  • 20
  • 18
  • 5
    try this for your ScrollView android:isScrollContainer="false" this may help you. – SALMAN Jul 10 '12 at 10:06
  • try this one also android:windowSoftInputMode="stateHidden|adjustResize|adjustPan" – SALMAN Jul 10 '12 at 10:08
  • Don't use adjustPan and adjustResize simultaneously, only one of them. – CoolMind Aug 13 '15 at 15:59
  • @CoolMind why we shouldnt use them simultaneously? – D4rWiNS Dec 17 '15 at 12:47
  • 1
    @D4rWiNS, sorry, I don't remember. Already tested it, and they work simultaneously, though adjustPan is enough. If I didn't forget, a problem was in a layout with a background picture - it was also resized. But I can be mistaken. – CoolMind Dec 17 '15 at 13:39
  • Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 04 '17 at 14:32
12

well, I have not mentioned any "windowSoftInputMode" in my app's manifest and it runs fine. Screen does not move up when the keyboard opens up. Try this approach - just remove the "windowSoftInputMode" parameter from your manifest .

Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34
3

Try this:

android:windowSoftInputMode="adjustNothing"
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Rahul
  • 76
  • 7
  • 1
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Apr 04 '17 at 14:32
2

My EditText kept moving up as well.

Adding gravity to the Edit Text seems to solve the problem. The Keyboard was pushing my EditText up, so I added gravity to the bottom of the Edit Text in the XML.

android:gravity=bottom
Xan
  • 74,770
  • 16
  • 179
  • 206
1

Try this, for me it worked...

<CoordinatorLayout
android:fitsSystemWindows="true">
<AppBarLayout>
    <Toobar>
        <!--...-->
    </Toobar>
</AppBarLayout>
<include layout="@layout/content_main_message" /> <!--EditText here-->
</CoordinatorLayout>
Victor Sam VS
  • 139
  • 3
  • 4
0

I already had a windowsoftInputMode to prevent the keyboard from opening, so just add the comment code

android:windowSoftInputMode="stateHidden|adjustPan|adjustResize"

Charlie
  • 349
  • 4
  • 8