2

I'm new to Android Programming so was just making a simple app.
In the app I have an EditText component which slides up as the keyboard pops up and i don't want it to slide up so i searched for it and got an work around of using,

In OnCreate method

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

But the issue was after using this line the EditText was in place after clicking on enter the keyboard didn't went away.So, I search for it and got this method for hiding the keyboard

public static void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager =
                (InputMethodManager) activity.getSystemService(
                        Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(
                activity.getCurrentFocus().getWindowToken(), 0);

But now the issue is the keyboard is hiding successfully but is not visible after i re click on EditText.So, I was searching on the net but no luck and started started searching methods in Android Studio for making the keyboard visible and figured out some what this

public static void showSoftKeyboard(Activity activity){
        InputMethodManager inputMethodManager1 =
                (InputMethodManager) activity.getSystemService(
                        Activity.INPUT_METHOD_SERVICE);
        inputMethodManager1.showSoftInputFromInputMethod(
                activity.getCurrentFocus().getWindowToken(), 0);

But it's not working is throwing NullPointerException. So please can anyone help me on this.

And also is it there any alternative for keeping the EditText on its position without sliding up. So that there is no need of applying this hide and show method and if not can you please tell me how to bring back the keyboard.

sandy560
  • 49
  • 1
  • 8

2 Answers2

1

Comment your logic, and please try with below approach,

Write below line in your activity tag in Android Manifest like below

<activity android:name=".yourActivityName"
        android:windowSoftInputMode="adjustPan">
</activity>

Then add this line to your edittext xml in your layout

android:imeOptions="actionDone"

like below

<EditText
    android:id="@+id/edt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Edittext"
    android:singleLine="true"
    android:maxLines="1"
    android:imeOptions="actionDone"/>

Or set it from your code

yourEditText.setImeOptions(EditorInfo.IME_ACTION_DONE); 

On click on done button in soft keyboard, it will be automatically close.

And below is the code of click event of Soft Keyboard done button.

yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            // write your code here
        }
        return false;
    }
});
Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24
  • Thnx a lot bro it's working. just an small query when i'm clicking on done the keyboard is hiding smoothly but the onclick event is not triggering can you please help me. – sandy560 Aug 03 '18 at 07:20
  • @sandy560, please tell me which onclick event you want to trigger. – Nirav Bhavsar Aug 03 '18 at 07:22
  • editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) {} – sandy560 Aug 03 '18 at 07:24
  • This is the listener for triggering event on the onclick of editText – sandy560 Aug 03 '18 at 07:25
  • @sandy560, if you have any define `OnTouchListener` and `OnFocusChangeListener ` on your edittext then please comment it. and set android:clickable="true" in your `edittext` or by code is `ed1.setClickable(true);` and then please check your edittext `onClick` event. – Nirav Bhavsar Aug 03 '18 at 07:28
  • It's not working bro actually my issue is first when i was not using any Pan adjustment code and when i use to create the done button or the check mark icon it used trigger the onclick event but now it's not triggering.And also i only have onClick listener no other listener is attached. – sandy560 Aug 03 '18 at 07:33
  • @sandy560, do you want done click event??, like if user click done or close keyboard then event should be fire then please refer this [link](https://stackoverflow.com/a/7584154/4748607), it will might help you. – Nirav Bhavsar Aug 03 '18 at 07:35
  • @sandy560, Welcome mate. – Nirav Bhavsar Aug 03 '18 at 07:49
0

Try this

public void showSoftKeyboard(Context context, View view){
    if(view.requestFocus()){
        InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}

or

public void showSoftKeyboard(Context context){
   if(context == null) return;
   InputMethodManager imm =(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
   imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}