0

I'm making a calculator. As we all know every calculator uses a keyboard which is developed by a developer itself. I'm using editText to output based on which button the user clicks in. That's not a problem. The problem is I want the user to move the cursor around, select some part of the text entered but not edit it using the android keyboard. I used the focusable and cursorenabled method but nothing helped me. I hope someone of you can help me. I'm using kotlin. Thanks.

MAK24
  • 3
  • 2
  • possible duplicate https://stackoverflow.com/questions/6384004/make-edittext-readonly – Imtiyaz Khalani Oct 18 '20 at 07:37
  • try with android:inputType="none", android:textIsSelectable="true", android:focusable="false" – Imtiyaz Khalani Oct 18 '20 at 07:38
  • @ImtiyazKhalani well that question is not exactly the same as mine, he only wanted to make edittext readonly, whereas my case is different as in the question description. As for your second reply, Thanks! but my problem has been solved. – MAK24 Oct 18 '20 at 11:05

2 Answers2

0

You can set whether the keyboard shows for an EditText using the setShowSoftInputOnFocus(Boolean) like so:

editText.showSoftInputOnFocus = false

This way the android keyboard won't show and the user can still move the cursor around and select text. Documentation here

Note: This method is only available for API 21 and above (more than 94% of active devices as of October 2020).

Xid
  • 4,578
  • 2
  • 13
  • 35
0

You want to have all the usual selection and editing stuff available, but you don't want the contents to actually change if the user types a thing?

You can add an InputFilter to the EditText, and override the filter method:

This method is called when the buffer is going to replace the range dstart … dend of dest with the new text from the range start … end of source. Return the CharSequence that you would like to have placed there instead, including an empty string if appropriate, or null to accept the original replacement.

There's a few more details in the link, but basically you can just return dstart...dend of dest, i.e. the original part that's meant to be getting replaced, so there's no actual change to the contents. Like this

editText.filters += InputFilter { _, _, _, dest, dstart, dend -> dest.subSequence(dstart, dend) }

edit: setting showSoftInputOnFocus can be useful, but it literally just means "don't show the soft (on-screen) keyboard when the view is focused". You can still edit the text in a few ways:

  • type on a hardware keyboard (some devices have them, you can test in the emulator by typing on your computer keyboard)
  • paste something in by tapping on the cursor
  • leave the EditText focused, switch away from the app, and come back - the soft keyboard might pop up

and so on. Plus Accessibility services might override things, and allow the user to have their choice of input appear, even if you're saying "I don't want the keyboard to show"

The only way (that I know of) to reliably stop the user from changing the contents of the EditText is with an InputFilter that literally blocks all changes. You can set showSoftInputOnFocus to false as well, to try and avoid the keyboard appearing - but like I said, there are still some situations where it will appear. The InputFilter will stop it having any effect

cactustictacs
  • 17,935
  • 2
  • 14
  • 25
  • Your method is also right, but it's better to use when you want to filter part of the text. @Siddharth Sharma method is the best, it works for me as my minSDK version is 21. I will use your method to filter what the user pastes as pasting is allowed in a calculator. – MAK24 Oct 18 '20 at 10:59
  • I edited with some more info - trying to hide the keyboard won't guarantee the user can't change the contents, you can't really rely on it if that's important – cactustictacs Oct 18 '20 at 15:43