0

I tried to hide keyboard on onCreateView in my fragment (where there is my viewPager) with these solutions:

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);



getActivity().getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);



InputMethodManager mgr = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(windowToken, 0);


mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
    final InputMethodManager imm = (InputMethodManager)getSystemService(
        Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
}

@Override
public void onPageScrolled(int position, float offset, int offsetPixels) {
}

@Override
public void onPageScrollStateChanged(int state) {
}

});

but all these solution doesn't works, and, when editText is focused, the keyboard appears.

UPDATE: i try with these codes in the onCreatedView of the fragment

mViewPager.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View arg0, MotionEvent arg1) {
            if (mViewPager.getCurrentItem() == 0) {
                final EditText initialValue = (EditText) mViewPager.findViewById(R.id.initialValueEditText);
                final EditText increase = (EditText) mViewPager.findViewById(R.id.increaseEditText);

                initialValue.setOnTouchListener(new View.OnTouchListener() {
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        evalue = "1";
                        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);

                        getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

                        InputMethodManager imt = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                        imt.hideSoftInputFromWindow(initialValue.getWindowToken(), 0);
                        return true;
                    }
                });

                increase.setOnTouchListener(new View.OnTouchListener() {
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        evalue = "2";
                        return false;

                    }
                });
            }
            return true;
        }
    });

and

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

            getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        }
    });

but unfortunatly when I focus the editText, the keyboard appears

  • Why are you using `EditText`? If you don't use it's functionality, then use `TextView`. – Ozgur Nov 10 '15 at 22:14
  • @Ozgur I want to use EditText because I'm able to copy/past text of the EditText. (I use a button to write inside the editText, so for this I want to disable the keyboard) – Gabriele Marcozzi Nov 11 '15 at 07:55
  • That editText is read-only, right? You don't want use to interact with it I guess. You may try to `setOnTouchListener` for it and `return true`. This should consume touch events for editText but I didn't test it. – Ozgur Nov 11 '15 at 12:10
  • @OzgurGUL I have tried the code that you can see in the Update, however, whe I focus the editText of the viewPager, the keyboard is not disabled and it appears.. – Gabriele Marcozzi Nov 11 '15 at 18:02

1 Answers1

0

Add android:enabled="false" on the editText. But then the whole editText wont be editable is this what you want?

X3Btel
  • 1,408
  • 1
  • 13
  • 21
  • @GabrieleMarcozzi other idea is to set onFocusChangedListener on the editText and there use the code to hide the softInput. – X3Btel Nov 11 '15 at 09:16
  • As you ca see in my update, I have tried to add `view.setOnFocusChangeListener` but this does not resolve my problem :(... – Gabriele Marcozzi Nov 11 '15 at 18:10
  • @GabrieleMarcozzi take a look in this answer http://stackoverflow.com/questions/5803193/android-disable-soft-keyboard-at-all-edittexts Using custom view should do it. Then again you might neet to do your own context view for copy&paste – X3Btel Nov 12 '15 at 08:48
  • Using custom view, when I focus the EditText, the keyboard does not appears, but the cursor is not visible (I also tried to set CursorVisible true) – Gabriele Marcozzi Nov 12 '15 at 13:06
  • @GabrieleMarcozzi how will you copy/paste into it? If you make your own buttons. You can just return true from onTouchListener, that way keyboard wont show. – X3Btel Nov 12 '15 at 13:42
  • Yes I make my own buttons. If I return true from onTouchListener, the editText is not selectable so I can't paste the text when I press my buttons (because my buttons paste the text in the selected editText, and I have two ediText) – Gabriele Marcozzi Nov 12 '15 at 13:57
  • The idea is to disable the keyboard from onFocusCangeListener, but I don't know how to do this.. infact all those methods in my question doesn't work – Gabriele Marcozzi Nov 12 '15 at 14:02