简体   繁体   中英

Android prevent layout from moving up on focus on edit field

Hi I searched so much but nothing could solve my problem. This is the Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LoginFragment_Layout_Out"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_background"
android:gravity="center"
android:orientation="vertical" 
android:focusable="false"
android:focusableInTouchMode="false"
>

<RelativeLayout
    android:id="@+id/login_input_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 

    >

    <EditText

        android:id="@+id/LoginFragment_Password_Password"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_above="@+id/LoginFragment_Button_Login"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="14dp"
        android:background="@color/grau"
        android:ems="10"
        android:gravity="center"
        android:fontFamily="sans-serif"
        android:hint="@string/LoginFragment_Password_Hint"
        android:inputType="textPassword"
        android:textColor="@color/black"
        android:textColorHint="@color/black" 
        />

    <EditText
        android:id="@+id/LoginFragment_EditText_UserName"
        android:layout_width="300dp"
        android:layout_height="40dp"
        android:layout_above="@+id/LoginFragment_Password_Password"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="12dp"
        android:background="@color/grau"
        android:ems="10"
        android:gravity="center"
        android:inputType="text"
        android:hint="@string/LoginFragment_EditText_UserName_Hint"
        android:textColor="@color/black"
        android:textColorHint="@color/black" >

    </EditText>

    <Button
        android:id="@+id/LoginFragment_Button_Login"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignEnd="@+id/LoginFragment_Password_Password"
        android:layout_alignParentBottom="true"
        android:background="@color/grau"
        android:hint="@string/LoginFragment_Button_Login"
        android:textColor="@color/black"
        android:textColorHint="@color/black" />

    <Button
        android:id="@+id/LoginFragment_Button_Registration"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:layout_alignStart="@+id/LoginFragment_Password_Password"
        android:layout_alignBottom="@+id/LoginFragment_Button_Login"
        android:background="@color/grau"
        android:onClick="startRegister"
        android:text="@string/LoginFragment_Button_Registration"
        android:textColor="@color/black"
        android:textColorHint="@color/black" />
</RelativeLayout>

And I dont want my layout to resize because of the Background Image.. So I will move the layout part wich contains the edit field up with this code

   rootView.getViewTreeObserver().addOnGlobalLayoutListener(new    ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            View rootLay = rootView.findViewById(R.id.LoginFragment_Layout_Out);
            RelativeLayout inputLay = (RelativeLayout) rootView.findViewById(R.id.login_input_layout);
            rootLay.getWindowVisibleDisplayFrame(r);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            int height = rootLay.getRootView().getHeight();
            int heightDiff = rootLay.getRootView().getHeight() - (r.bottom - r.top);
             if (heightDiff > 100) {
                 lp.setMargins(0, 0, 0, heightDiff);
                 inputLay.setLayoutParams(lp);
             }else{
                 lp.setMargins(0, 0, 0, 0);
                 inputLay.setLayoutParams(lp);
             }
        }
    });

this also works finds but when I click an edit field for the first time also the the rootlayout is moving up when the keybpard appears but when I close the keyboard and click the edit field for a second time everything works fine.. why please help

Why don't you just set the adjustResize so the window doesn't shift?

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustResize"
    android:label="@string/app_name" >
</activity>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM