0

I created a custom keyboard using CardView, so I don't need the software keyboard when I have an EditText. The thing now is that when I click on EditText the virtual keyboard pops up, is there a way to disable the virtual keyboard?

Here is my fragment code:

public class FragmentQuestion1 extends Fragment {

    private EditText mEditTextQuestion1;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_question_1, container, false);

        mEditTextQuestion1 = view.findViewById(R.id.edit_text_question_1);
        MyKeyboard keyboard = view.findViewById(R.id.keyboard);
        mEditTextQuestion1.setRawInputType(InputType.TYPE_CLASS_TEXT);
        mEditTextQuestion1.setTextIsSelectable(true);

        InputConnection ic = mEditTextQuestion1.onCreateInputConnection(new EditorInfo());
        keyboard.setInputConnection(ic);

        mEditTextQuestion1.addTextChangedListener(new NumberTextWatcher(mEditTextQuestion1));

mEditTextQuestion1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
                } else {

                }
            }
        });

        return view;
    }

Here is the way I used the EditText in my xml file:

<EditText
            android:id="@+id/edit_text_question_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:backgroundTint="#FFFFFF"
            android:inputType="number"
            android:gravity="center"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:textIsSelectable="true"/>
Daniel Spatz
  • 67
  • 1
  • 9
  • Possible duplicate of [Close/hide the Android Soft Keyboard](https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard) – Avi Parshan Oct 06 '19 at 11:56

3 Answers3

1

Edit: Sorry it seems on Focus change is to early as this method is about hiding a softKeyboard not preventing it from showing.

Use an onClickListener works better but you will probably see the keyboard briefly.

And then hide the keyboard with InputMethodManager

See Close/hide the Android Soft Keyboard

Even onClick Seem to be unreliable due to timing issues.

But some example code that works most of the time.


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText message = (EditText) findViewById(R.id.message);

        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftKeyboard(v);
            }
        });

    }

    public void hideSoftKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }
}
Andrew
  • 3,791
  • 2
  • 10
  • 19
  • I did some changes in my code as suggested in your provided links, but its still popping up, have I implemented this code right? – Daniel Spatz Oct 03 '19 at 18:00
  • I implemented this code in my fragment but the keyboard still shows up – Daniel Spatz Oct 04 '19 at 08:11
  • Ok now I understand it. The code works as you described it, the soft-keyboard pops up and after clicking a second time on the EditText, the keyboard disappears. Is there somehow a way to disable this behavior entirely? Isn't there a method to "delete" the soft-keyboard in this activity? – Daniel Spatz Oct 04 '19 at 09:16
  • The question is why do you need to use EditText? Your on screen buttons can change the text in a TextView widget and a TextView won't try and open up a softkeyboard – Andrew Oct 04 '19 at 10:01
  • I actually don't know how to implement this and wouldn't there be performance issues? – Daniel Spatz Oct 04 '19 at 10:16
  • Without more details on why you want your own onscreen keyboard and what it does it is hard to say if using an Textview is suitable. As for how to implement, it should be just changing the XML from Edittext to Textview as Edittext is derived from TextView – Andrew Oct 04 '19 at 10:34
  • I am programming a quiz app where the user types his answer in an EditText. For consistency over all devices I made a custom keyboard to have the same design. Also I have a TextWatcher on the EditText so it would be hard not to use an EditText I guess – Daniel Spatz Oct 04 '19 at 10:58
  • What benefit to the user is this consistency? Are they expected to use this on lots of different devices? While it might have consistency it removes familiarity with the devices normal keyboard, while adding a lot of effort. Most keyboards follow similar design are the differences worth worrying about? You could create a proper softKeyboard of your own as and InputMethodService e.g. https://code.tutsplus.com/tutorials/create-a-custom-keyboard-on-android--cms-22615 – Andrew Oct 04 '19 at 15:19
  • but then the user would be able to click away the keyboard which I don't want – Daniel Spatz Oct 04 '19 at 15:26
0

I found a solution that works fine and is pretty easy:

Just use mEditTextQuestion1.setEnabled(false); in the inCreateView method to make the EditText not clickable. In my case then the color of the EditText changed to a brighter dark so I added android:textColor="#000"to my xml file.

Daniel Spatz
  • 67
  • 1
  • 9
0

I had a very similar issue and was able to solve it as follows.

In the Android Manifest, you should add these lines to the activity

   <activity
        android:name=".activities.YourActivity"
        android:windowSoftInputMode="adjustResize|stateAlwaysHidden" />

Within the Activity Class (Java), add this to the onResume method. This will prevent the keyboard from popping up when the activity is resumed

@Override
    protected void onResume() {
        super.onResume();
        this.getWindow().setSoftInputMode(WindowManager.
                LayoutParams.SOFT_INPUT_STATE_HIDDEN); //hide keyboard when your activity is in use
    }

And I added this method to modify the EditText, and called it from onCreate, and whenever I modified my custom keyboard

private void setupSpinItemsCustomKeyboard() {
        Helper.hideAllKeyboard(YourActivity.this);
        fromEditText.setShowSoftInputOnFocus(false); //no response on keyboard touch
        fromEditText.requestFocus();
    }

and this to actually hide the keyboard

public static void hideAllKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            assert imm != null;
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
Avi Parshan
  • 1,427
  • 2
  • 19
  • 23