androidlayoutandroid-edittexttext-alignmentadjustpan

windowssoftinputmode=Adjustpan is Not working properly when textalignment property center or center_horizantal


Attribute android:windowSoftInputMode="adjustPan" has been specified in the AndroidManifest.xml, but it works only the first time.

second time the software keyboard is shown on the screen, it entirely hides my EditText.

Image Description use the hyperlink for see the images. First time EditText woks Properly Click here hide and reopen the Keyboard Second time problem is occured.click here

Note:

1.AdjustResize instaed of AdjustPan is work but I need adjustPan because AdjustResize the image was resized.

2.I'm also tried with create Own Edittext(Custom Edittext) with PreIme that also not working for me.

3.If remove text Alignment center works proper.But I need Text Alignment Center.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:focusableInTouchMode="true"
tools:context="com.example.raj.testapp.MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:id="@+id/scroll">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="#00ffffff"
            android:layout_marginTop="25dp">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#ffffff"
                android:alpha=".3"
                android:textColorHint="@color/black"
                android:textColor="@color/black"
                android:hint="Username"
                android:singleLine="true"
                android:textAlignment="center"
                android:fontFamily="sans-serif-light"
                android:id="@+id/register_email"
                android:textSize="20sp"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#ffffff"
                android:alpha=".3"
                android:textColorHint="@color/black"
                android:textColor="@color/black"
                android:hint="First name"
                android:singleLine="true"
                android:textSize="20sp"
                android:id="@+id/regiter_firstname"
                android:layout_marginTop="25dp"
                android:fontFamily="sans-serif-light"
                android:textAlignment="center"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#ffffff"
                android:alpha=".3"
                android:textColorHint="@color/black"
                android:textColor="@color/black"
                android:hint="Lastname"
                android:singleLine="true"
                android:textSize="20sp"
                android:id="@+id/regiter_lastname"
                android:layout_marginTop="25dp"
                android:fontFamily="sans-serif-light"
                android:textAlignment="center"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#ffffff"
                android:alpha=".3"
                android:textColorHint="@color/black"
                android:textColor="@color/black"
                android:singleLine="true"
                android:hint="Password"
                android:textSize="22sp"
                android:textAlignment="center"
                android:id="@+id/regiter_password"
                android:inputType="textPassword"
                android:layout_marginTop="25dp"
                android:fontFamily="sans-serif-light"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#ffffff"
                android:alpha=".3"
                android:textColorHint="@color/black"
                android:textColor="@color/black"
                android:singleLine="true"
                android:hint="Confirm Password"
                android:textAlignment="center"
                android:layout_marginTop="25dp"
                android:textSize="20sp"
                android:inputType="textPassword"
                android:id="@+id/register_confirm"
                android:fontFamily="sans-serif-light"
                />

        </LinearLayout>



    </LinearLayout>

</ScrollView>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Next"
    android:layout_alignParentBottom="true"
    android:fontFamily="sans-serif"
    android:id="@+id/register_next"
    android:layout_marginTop="25dp"
    android:textColor="#ffffff"
    android:textAllCaps="false"
    android:background="@color/yellow"/>

</RelativeLayout>

Manifest file

   <activity android:name=".MainActivity"
        android:configChanges="locale"
        android:windowSoftInputMode="stateHidden|adjustPan">

Solution

  • 1.Create a Custom editText file[LockEditext.java]

    package com.raj.app.utils;
    
    //import statements
    
    public class LockEditText extends android.support.v7.widget.AppCompatEditText {
    
    public LockEditText(Context context) {
        super(context);
    }
    
    public LockEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public LockEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            clearFocus();
        }
        return super.onKeyPreIme(keyCode, event);
    }
    }
    

    2.XML file Change the com.raj.app.utils.LockEditText (Your packagename of the file.LockEditText) For Example Like this

        <com.raj.app.utils.LockEditText
                    android:id="@+id/regiter_firstname"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginTop="25dp"
                    android:layout_weight="1"
                    android:alpha=".3"
                    android:background="#ffffff"
                    android:fontFamily="sans-serif-light"
                    android:hint="@string/firstname"
                    android:lines="1"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textColorHint="@color/black"
                    android:textSize="20sp"/>
    

    instead of

        <EditText
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:background="#ffffff"
                android:alpha=".3"
                android:textColorHint="@color/black"
                android:textColor="@color/black"
                android:hint="First name"
                android:singleLine="true"
                android:textSize="20sp"
                android:id="@+id/regiter_firstname"
                android:layout_marginTop="25dp"
                android:fontFamily="sans-serif-light"
                android:textAlignment="center"/>
    

    all the editext like this.

    3.Java File

     EditText Reg_firstname = (EditText) findViewById(R.id.regiter_firstname);
    

    EditText replace with LockEditText

    Works fine and the code for when click done same problem is arise So add the following code.

    LockEditText reg_confirm=(LockEditText)findViewById(R.id.reg_confirm);
    
     reg_confirm.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                    switch (keyCode) {
                        case KeyEvent.KEYCODE_ENTER:
                            if(getCurrentFocus()!=null) {
                                Reg_confirm.clearFocus();
                                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                                inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
                            }
                            break;
                        default:
                            break;
                    }
                }
                return false;
            }
        });
    

    if you don't need to like large code then Simply use adjustResize in Manifest