user4928488
user4928488

Reputation:

Android : EditText Soft Keyboard is not hiding at first time press

In my activity I have two Fragment.Both Fragment view I have EditText.I want to hide the soft keyboard on one of the EditText in a Fragment.

I try with this code in one fragment before layout setting

getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
            WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

When I use this line of code keyboard is hiding in both Fragment.But I want to show keyboard in one Fragment View

I try another line of code at the time of EditText "OnTouchListener" and "OnClickListener"

   edt_dailNumber.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
             closeKeyboard(getActivity(),edt_dailNumber.getWindowToken());
                return false;
            }
        });

        edt_dailNumber.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                closeKeyboard(getActivity(), edt_dailNumber.getWindowToken());
            }
        });



 public static void closeKeyboard(Context c, IBinder windowToken) {
    InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
    mgr.hideSoftInputFromWindow(windowToken, 0);
}

By using this code it is hiding the Soft KeyBoard.

But when I press/touch the EditText at the first time the KeyBoard is showing. After that the second time onwards the keyboard is hiding

 <EditText
            android:id="@+id/edt_dailNumber"
            android:layout_width="0dp"
            android:layout_weight=".85"
            android:paddingLeft="5dp"
            android:layout_gravity="center_vertical"
            android:textSize="@dimen/dail_dailpad_hint_text_size"
            android:layout_marginLeft="8dp"
            android:textColor="#000"
            android:inputType="number"
            android:gravity="center"
            android:ellipsize="start"
            android:layout_height="match_parent"
            android:background="@drawable/edit_text_line_contacts"
           />

I didnt get why the Soft KeyBoard is show at the first time Press

Can any give me a solution for this problem

Thanks in advcance:)

Upvotes: 2

Views: 1974

Answers (4)

Bishal Seth
Bishal Seth

Reputation: 61

youredittextid.setShowSoftInputOnFocus(false);

** This will work for hide your keyword of edittext

**

Upvotes: 0

Krishnan
Krishnan

Reputation: 426

Go to your manifest file , which activity use to edit text put the single line android: windowSoftInputMode="stateHindden"

Upvotes: 2

Vennila
Vennila

Reputation: 250

If you want to fire the onClickListener of edittext each time you can use

android:focusable = "false"

in your edittext.. hope this will help you

Upvotes: 0

KishuDroid
KishuDroid

Reputation: 5451

Your soft keyboard hiding on second click because of , you are using an edittext. On the first click, your Edit Text will get focus. then the second click it calls the onClickListener. That's why its happening.

If you want to use edittext itself for this particalur task then it is better to use setOnFocusChangeListener() instead of onClickListner().

Change in Code :

edt_dailNumber.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus)
        {
             // Open keyboard
            ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(searchEditText, InputMethodManager.SHOW_FORCED);
        } else {
            // Close keyboard
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);            }
    }
});

Now you can manually open and hide keyboard by doing below code :

for open

setEditTextFocus(true);

And for closing :

setEditTextFocus(false);

And if you want to directly close the keyboard then write :

edt_dailNumber.setInputType(InputType.TYPE_NULL);

Upvotes: 0

Related Questions