1

Im trying to make it so when the user clicks away from the keyboard (any where on the page) it closes down (to be able to view page without it there as I have a key edit texts on the page they may want to fill out before..) ive tried lots of solutions and many answers on stack overflow but non seem to be working for me ...

public class OneFragment extends Fragment implements BlockingStep {

public interface PassingData { //create interface to pass data
    void passData (String data);
}

String stepOne;
EditText _fromET;
EditText _toET;
EditText _refET;
EditText _numET;

private PassingData PD_listener; // declare member variable of interface

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    PD_listener = (PassingData) context; //initialize interface using onAttach method
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmentone, container, false);

    _fromET = (EditText) view.findViewById(R.id.FROM);
    _toET = (EditText) view.findViewById(R.id.TO);
    _refET = (EditText) view.findViewById(R.id.REF_NUM);
    _numET = (EditText) view.findViewById(R.id.CONTACTNUM);
    return view;
}

I tried using inputmethodmanager and also a focusListener but both failed...

Any help would be great, thank you! Abbie

A.Turner
  • 196
  • 1
  • 13

3 Answers3

2

you can use OnFocusChangeListener

yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
        }
    });

you need to use this on your EditText, and whenever you click outside of the EditText anywhere it will hide the SoftKeyboard for you.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
1

Add this to your parent activity:

@Override
public boolean dispatchTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        View v = getCurrentFocus();
        if ( v instanceof EditText) {
            Rect outRect = new Rect();
            v.getGlobalVisibleRect(outRect);
            if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                v.clearFocus();
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
            }
        }
    }
    return super.dispatchTouchEvent(event);
}

Add this to your layout of the fragment:

android:focusableInTouchMode="true"

Hope it helps.

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
0

you may try this way

 public static void forceHideKeyboard(AppCompatActivity activity, EditText editText) {
        try {
            if (activity.getCurrentFocus() == null
                    || !(activity.getCurrentFocus() instanceof EditText)) {
                editText.requestFocus();
            }
            InputMethodManager imm = (InputMethodManager) activity
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            activity.getWindow().setSoftInputMode(
                    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

use like this

forceHideKeyboard(getActivity(),editText);
Adil
  • 812
  • 1
  • 9
  • 29