0

I have been reading the answers from the post How to set cursor position in EditText? I do have a specific problem in my EditText which might be the cause of the problem. In my xml layout, I define an EditText called "edit" whose inputType = none. According to the android developer documents, if the inputType = none, the text in not editable. The reason why is "none" is caused this is the way I prevent the android general keyboard from popping up over my User-Interface , which works very well. I want to do something like this: user entered : 5 + sin( ) , now the cursor editText cursor position should be right after the ) symbol, but there's no cursor shown since the inputType = none. What I need to do is move the position of where the next text input will be placed in between the ( ) symbols. What happens is that the next text input goes right after the ) symbol. So if the next text input is 4, then: 5 + sin( )4 , I don't want this. I rather have the following: 5 + sin(4 )

I am using a custom made keyboard which has the button "( )". Here's what I have tried without any success:

//once the user presses the "( )" button:
Selection.setSelection(edit.getText(),edit.getText().length() - 2);

Another way I have tried:

//once the user presses the "( )" button:
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setSelection(edit.getText().length() - 2);
edit.setInputType(InputType.TYPE_NULL);

Neither of the two ways has worked for me. I've also tried to set the inputType of the editText to something else besides "none" on the xml file. But, the android keyboard pops up on top of my custom made keyboard.

Any thoughts or ideas would be helpful

i_o
  • 611
  • 8
  • 24

1 Answers1

1

I don't know how exactly does your () button work but you can give a try this one.

yourEditText.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if( s.length()>0 && s.charAt(s.length()-1) == ')' ){
            yourEditText.setSelection(s.length()-1);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
Ismail
  • 121
  • 2
  • 6