Ali Rizwan
Ali Rizwan

Reputation: 182

Prevent Softkeyboard from opening on EditText Focus

I need to prevent softkeyboard from showing up when Edit Text gains focus or if user taps on the edit text because user has to use a barcode scanner as an input method. But I need is open the keyboard when user clicks/taps a specific button.

I have tried focusableInTouchMode property and it works fine in preventing the softkeyboard from popping but it won't let the edit text gain focus.

Any hint on how to achieve this?

Upvotes: 1

Views: 1246

Answers (3)

Harsh Patel
Harsh Patel

Reputation: 1086

myEditText.setInputType(InputType.TYPE_NULL);

myEditText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // showMyDialog();
    }
});

myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            // showMyDialog();
        }
    }
});

Try this.... Just return null or anything on onclicklistner..

Njoy.. :) And m using it so don't tell me its not working.. LOL..

I have one condition that if user clicks on edittext then it ll pops DatePicker Dialog and its working. So njoy dude...: ;)

Upvotes: 1

ova89
ova89

Reputation: 1

You should use stateAlwaysHidded. Either in runtime as in the first answer, or directly in your project Manifest:

android:windowSoftInputMode="stateAlwaysHidden"

This will prevent keyboard from appearing during onCreate/onResume.

To prevent keyboard appearing after clicking EditText, you can place some blank view with transparent backround right on top of your EditText:

<View
    android:id="@+id/view"
    android:layout_width="editTextWidth"
    android:layout_height="editTextWidth"
    android:background="@android:color/transparent"
    android:clickable="true"/>

The key attribute here is android:clickable="true". It will prevent from gaining focus on click (actually users will click on this blank view, not EditText).

Then, when user clicks special button you've mentioned, you just remove this blank view and call editText.requestFocus()

Upvotes: 0

P-RAD
P-RAD

Reputation: 1341

you can hide the keyboard using this code

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

Upvotes: 0

Related Questions