Disable the keyboard (API 11 to current)
This is the best answer I have found so far to disable the keyboard (and I have seen a lot of them).
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(false);
} else { // API 11-20
editText.setTextIsSelectable(true);
}
There is no need to use reflection or set the InputType
to null.
Re-enable the keyboard
Here is how you re-enable the keyboard if needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(true);
} else { // API 11-20
editText.setTextIsSelectable(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
}
See this Q&A for why the complicated pre API 21 version is needed to undo setTextIsSelectable(true)
:
This answer needs to be more thoroughly tested.
I have tested the setShowSoftInputOnFocus
on higher API devices, but after @androiddeveloper's comment below, I see that this needs to be more thoroughly tested.
Here is some cut-and-paste code to help test this answer. If you can confirm that it does or doesn't work for API 11 to 20, please leave a comment. I don't have any API 11-20 devices and my emulator is having problems.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="@android:color/white">
<EditText
android:id="@+id/editText"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="enable keyboard"
android:onClick="enableButtonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="disable keyboard"
android:onClick="disableButtonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
}
// when keyboard is hidden it should appear when editText is clicked
public void enableButtonClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(true);
} else { // API 11-20
editText.setTextIsSelectable(false);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setClickable(true);
editText.setLongClickable(true);
editText.setMovementMethod(ArrowKeyMovementMethod.getInstance());
editText.setText(editText.getText(), TextView.BufferType.SPANNABLE);
}
}
// when keyboard is hidden it shouldn't respond when editText is clicked
public void disableButtonClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21
editText.setShowSoftInputOnFocus(false);
} else { // API 11-20
editText.setTextIsSelectable(true);
}
}
}