Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having Trouble with Soft Keyboard

Hi Everyone iam new to Android and stuck in really silly problem in my project i have one EditText which is defined in Header View of a List View whenever users touches the EditText soft keyboard will be displayed. i have something called clear button which clears the EditText After clearing soft keyboard is not displaying in 2.3 devices.Whenever i press lock/power button lock the device and unlock then soft keyboard is displaying

<activity
        android:name=".pl.Mobile.AddJobNew"
        android:configChanges="orientation|keyboardHidden"
        android:screenOrientation="landscape"
        android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"
         >

Here is My Activity Declaration in Android for android:windowSoftInputMode : 'adjustResize' option working perfectly for Android 2.3 devices but failed in 4.0 devices where soft keyboard overlapping over edit text . option 'adjustPan' is workin good in 4.0 devices but failed in 2.3 devices

Please help me get out of this problem.

Thanks in Advance

like image 476
Software Sainath Avatar asked Jan 16 '13 11:01

Software Sainath


2 Answers

Try this in the activity with the edit text:

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
// Do something for 4.0 and above versions
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
} 
else{
// do something for phones running an SDK before 4.0
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

Also remove,

android:windowSoftInputMode="stateVisible|adjustResize|adjustPan"

From your activity in the manifest file.

Build.Version

like image 162
Karthik Balakrishnan Avatar answered Nov 15 '22 09:11

Karthik Balakrishnan


I think that the problem is that when you click the clear button, the editText looses its focus so the keyboard hides itself.

I would try 2 things to solve it;

  1. after you clean the text, do:

    yourEditTextField.requestFocus()

  2. manually control the keyboard visibility:

    public void showKeyboard(){ View view = getWindow().getCurrentFocus(); if (view==null) return;

    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
    

    }

    public void hideKeyboard(){ View view = getWindow().getCurrentFocus(); if (view==null) return;

    IBinder binder = view.getWindowToken();
    if (binder == null)
        return;
    
     // prevent a bug in some keyboards that makes the text hang on the screen
    if (view instanceof EditText) 
        ((EditText)view).setText(((EditText)view).getText().toString());
    
    
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);
    

    }

like image 22
Asaf Pinhassi Avatar answered Nov 15 '22 08:11

Asaf Pinhassi