Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: how to permanently & completely NOT show default soft keyboard for an EditText?

Tags:

java

android

I have three EditText boxes in an activity, for two of which normal input methods (hard keys, default soft keyboard) are ok. But for one of the EditText boxes I want to send soft input only from a custom keyboard view. So in effect I want the default soft keyboard never to be shown for this EditText. I've tried adding onTouchListeners and onFocusChange listeners for the EditText with partial success like this:

public boolean onTouch(View v, MotionEvent event) {
    v.requestFocus();
    imm.toggleSoftInput(0, 0);
    return true;
}

public void onFocusChange(View v, boolean hasFocus) {
    InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive(v)) {
        imm.toggleSoftInput(0,0);
    }
}

But I have not achieved a definitive solution because

1)the default soft keyboard always briefly flashes visible before the listener hides it

2)on some occasions, such as moving focus to the EditText with hard keyboard arrow keys sometimes sets the default soft keyboard visible

and so on.

So I would love to find a simple way to tell Android never to show the default soft keyboard for this specific EditText. I would not like to extend EditText and start to override stuff, since the EditText functionality is perfect for me - I just want the default soft keyboard not to be shown.

I've spent days now trying to figure this out. Some topics (including some here) found via google have half-way attempts at this problem, but so far I haven't found a single totally functional solution.

EDIT:

I'm really starting to get annoyed. I decided I could try not to use EditText but whatever other view that will get the job done. It turns out it is freakin hard to get rid of that soft keyboard. It even shows up when I use the hard keys to move focus from an EditText to a Button! Why on earth should the soft keyboard be shown on every freakin View that happens to have focus? Even when I explicitly say inputType="none"? How do I turn that * soft keyboard OFF? Below is the xml for the Button - let's use that as an example:

<Button
    android:id="@+id/OkButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:paddingRight="5mm"
    android:paddingLeft="5mm"
    android:layout_below="@id/Volume"
    android:layout_alignParentLeft="true"
    android:text="OK"/>

EDIT2:

I have how achieved a solution that seems to work. First I get a hold of the InputMethodManager:

this.imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

and the I set OnClickListener, OnTouchListener and OnFocusChange listener all call the following method when I want the EditText to be focused and my custom KeyboardView visible, while hiding the default soft input:

private boolean makeActive(View v) {
    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    EditText e = (EditText) v;
    int iType = e.getInputType();
    e.setInputType(InputType.TYPE_NULL);
    e.requestFocus();
    showKb();
    e.setInputType(iType);
    return true;
}
like image 260
zenperttu Avatar asked Feb 02 '11 21:02

zenperttu


People also ask

Does factory reset remove all data permanently?

When you do a factory reset on your Android device, it erases all the data on your device, and returns the phone back to its original out-of-the-box state as it was from the factory.

How do I permanently delete deleted files?

Click Delete in the File Explorer Ribbon at the top of the window, or click the arrow underneath the Delete option and select Permanently delete. Clicking Delete sends the file to the Recycle Bin, while selecting the Permanently delete option deletes the file for good.


1 Answers

Simple

editText.setShowSoftInputOnFocus(false);

Some people suggested that the following might work on older versions of Android, but the behaviour is unexpected.

edittextPlay.setTextIsSelectable(true);
like image 173
oliversisson Avatar answered Oct 01 '22 02:10

oliversisson