Disable soft-keyboard from EditText but still allow copy/paste?
After hours and hours of research, I finally found a solution that works for all API versions. Hope this saves someone's time.
If you are developing for API >= 11, the solution is simple, either:
1) Add the two properties below in the xml file of EditText
android:inputType="none"
android:textIsSelectable="true"
or
2) Programatically do the below
myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);
And you're done.
If you want to cater for API < 11 as well, I found that there is no way to disable to keyboard from popping out if you wanted to select the text for copy paste purpose. Setting focusable to false will disable the keyboard but it doesn't help because it disables your ability to select text too. Any other solutions I found in stackoverflow all either doesn't work or disables text selection at the same time too.
One ugly way to solve this is as such..
First, add this property in the xml file of EditText
android:editable="false"
Yes this is deprecated, but necessary for making the EditText not editable in API version < 11.
Next, we will need to hide the keyboard as soon as it shows up, so that we can continue selecting text without the keyboard blocking the way.
Use this code below to detect keyboard showing up (solution obtained from https://stackoverflow.com/a/9108219/1241783), and hide it immediately.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
//Hide the keyboard instantly!
if (getCurrentFocus() != null)
{
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
}
});
}
It works for my case. Though you can see the keyboard showing up in a split second (which is the ugly part) but I can't think of any other way to get this to work at the time of writing. If you have a better solution, please leave a comment!
Let me know too if this saves someone's time :)
Android: Disable soft keyboard at all EditTexts
create your own class that extends EditText
and override the onCheckIsTextEditor()
:
public class NoImeEditText extends EditText {
public NoImeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false;
}
}
How to disable Android Soft Keyboard for a particular activity?
Just thought it might be possible to extend EditText and handle the access to the softkeyboard through there and came across this question which has a very elegant solution to my problem.
Just followed the steps and this handled the softkeyboard perfectly for me. Hope it helps others that come across this issue.
Disable keyboard on EditText
Here is a website that will give you what you need
As a summary, it provides links to InputMethodManager
and View
from Android Developers. It will reference to the getWindowToken
inside of View
and hideSoftInputFromWindow()
for InputMethodManager
A better answer is given in the link, hope this helps.
here is an example to consume the onTouch event:
editText_input_field.setOnTouchListener(otl);
private OnTouchListener otl = new OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
return true; // the listener has consumed the event
}
};
Here is another example from the same website. This claims to work but seems like a bad idea since your EditBox is NULL it will be no longer an editor:
MyEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});
Hope this points you in the right direction
How to disable the soft keyboard to show up when the user clicks on EditText?
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.
How to hide soft keyboard on android after clicking outside EditText?
The following snippet simply hides the keyboard:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager =
(InputMethodManager) activity.getSystemService(
Activity.INPUT_METHOD_SERVICE);
if(inputMethodManager.isAcceptingText()){
inputMethodManager.hideSoftInputFromWindow(
activity.getCurrentFocus().getWindowToken(),
0
);
}
}
You can put this up in a utility class, or if you are defining it within an activity, avoid the activity parameter, or call hideSoftKeyboard(this)
.
The trickiest part is when to call it. You can write a method that iterates through every View
in your activity, and check if it is an instanceof EditText
if it is not register a setOnTouchListener
to that component and everything will fall in place. In case you are wondering how to do that, it is in fact quite simple. Here is what you do, you write a recursive method like the following, in fact you can use this to do anything, like setup custom typefaces etc... Here is the method
public void setupUI(View view) {
// Set up touch listener for non-text box views to hide keyboard.
if (!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(MyActivity.this);
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
That is all, just call this method after you setContentView
in your activity. In case you are wondering what parameter you would pass, it is the id
of the parent container. Assign an id
to your parent container like
<RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>
and call setupUI(findViewById(R.id.parent))
, that is all.
If you want to use this effectively, you may create an extended Activity
and put this method in, and make all other activities in your application extend this activity and call its setupUI()
in the onCreate()
method.
Hope it helps.
If you use more than 1 activity define common id to parent layout like<RelativeLayout android:id="@+id/main_parent"> ... </RelativeLayout>
Then extend a class from Activity
and define setupUI(findViewById(R.id.main_parent))
Within its OnResume()
and extend this class instead of ``Activity in your program
Here is a Kotlin version of the above function:
@file:JvmName("KeyboardUtils")
fun Activity.hideSoftKeyboard() {
currentFocus?.let {
val inputMethodManager = ContextCompat.getSystemService(this, InputMethodManager::class.java)!!
inputMethodManager.hideSoftInputFromWindow(it.windowToken, 0)
}
}
Related Topics
How to Detect When the User Has Changed the Clock Time on Their Device
Android: Unable to Instantiate Activity/Classnotfoundexception
Connecting to Wifi Using Adb Shell
Pass Arraylist<? Implements Parcelable> to Activity
How to Store SQLite Database Directly on Sdcard
Android: How to Center Title in Toolbar
Wrong Requestcode in Onactivityresult
Android SQLite: Update Statement
How to Know When the Recyclerview Has Finished Laying Down the Items
Apply Custom Filters to Camera Output
How to Get the Current Orientation (Activityinfo.Screen_Orientation_*) of an Android Device
Hide Ics Back Home Task Switcher Buttons
How to Use Android Progressbar in Determinate Mode
Horizontal Scrolling Grid View
Filter Listview with Arrayadapter
Overriding Referenced Style Attributes
Android: Crop an Image After Taking It with Camera with a Fixed Aspect Ratio