0

I am developing an Android application in which, the user has to input an answer to an edittext view. In order to make things simpler I want the edit text to keep showing the rest of the edittext hint. For example, if the correct answer is: "Big joe". I generate the following hint:"--- ---" which is 3 dashes then a space and then 3 more dashes . Setting it as a hint is no problem, however, as soon as the user presses on the edittext view the hint vanishes. I want it to stay put and get runned over by the user input. Continuing the example, id user only entered (Wrongly) "Ji" (but wasn't yet done!) then I want the edittext view to contain the text:"Ji- ---". and not just "Ji" as it regulerly would. The hint should stay like a "shdow text" or like "Sit fillers", so as soon as the "sit" is filled it disapears. So for correct answer what would happen would be the folowing with # marking the cursor: begin: #-- --- 1st: B#- --- 2nd: Bi# --- 3rd: Big #-- 4th: Big j#- 5th: Big jo# 6th: Big joe#. at which point I am not so sure there is even a reason to enable the user to enter more letters.

yz101
  • 51
  • 9

1 Answers1

0

First set the dashes using .setText() instead setting hint. To your EditText instance add TextWatcher by using .addTextChangedListener(). Override the TextWatcher methods and use them to manipulate the EditText as user types.

https://developer.android.com/reference/android/text/TextWatcher.html See Selection class https://developer.android.com/reference/android/text/Selection.html

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • I have tried that, but did not succeed. It says "It is an error to attempt to make changes to s from this callback" on both beforeTextChanged and onTextChanged where as in afterTextChanged it puts the whole thing into an infinite loop. Is there an example I could follow somewhere? – yz101 Jan 22 '13 at 09:47
  • in afterTextChanged method remove the listener, change the text re-add the listener again. This works, please find this in one of my answers about stackoverflow exception when you do infinite changes in text. – Nikola Despotoski Jan 22 '13 at 11:22
  • Sorry, I couldn't reach my laptop last night to link you, here it is the link http://stackoverflow.com/questions/7222944/changing-text-in-android-on-text-change-causes-overflow-error – Nikola Despotoski Jan 22 '13 at 21:00
  • I've tried, for hours. Best case scenario is that I get thing dubbled in a strange way. For example, if I put for -- -- and I enter a character (for example h), then it puts on h- -- which is good, but if I put another sign it all gets messed up. for example if I anter an i or a second h, instead of getting hi --, I get hhi--. I can't seen to see the reason why. I can't believe there is no simple solution to what I am trying to do. – yz101 Jan 23 '13 at 01:11
  • Thank you Nikola, I have searched and found the post you gave the link for and got stuck where I just explained in the above comment. – yz101 Jan 23 '13 at 01:15
  • Yep, you are moving forward. You need to improve the logic. Try saving the existing *correct* entered letters. – Nikola Despotoski Jan 23 '13 at 01:17