1

I have an edit text and i want to know if the cursor has moved to the next line or not, if it has moved to next line i want to call a new function. how can i do this? Is there a way or a function from which i can know that the cursor has moved to the next line?

PS: I am using a text watcher to implement Upper case and lower case functionality

scene.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int i, int j, int k) {
                if(flag!=0)
                {
                    flag=1;
                    strcheck = s.toString().charAt(s.length()-1);

                    int line = getCurrentCursorLine(scene);


                    lineno = line;




                    if(strcheck=='\n' && ib1==true){
                        ib22(scene, lineno);

                    }   else if(strcheck=='\n' && ib2==true){

                        ib22(scene, lineno);

                    }   else if(strcheck=='\n' && ib3==true){

                        ib55(scene, lineno);

                    }   else if(strcheck=='\n' && ib4==true){

                        ib55(scene, lineno);

                    }   else if(strcheck=='\n' && ib5==true){

                        ib33(scene, lineno);

                    }   else if(strcheck=='\n' && ib6==true){

                        ib11(scene, lineno);

                    }   else if(strcheck=='\n' && ib7==true){

                        ib22(scene, lineno);

                    }   else if(strcheck=='\n' && ib1==false && ib2==false && ib3==false && ib4==false && ib5==false && ib6==false && ib7==false){

                        ib22(scene, lineno);

                    }



                    //int start = scene.getLayout().getLineStart(lineno);
                    //int end = scene.getLayout().getLineEnd(lineno);
                    //String previous = (start<1 && lineno==0)?"":scene.getText().toString().substring(0, start);


                    if (nowUpper){
                        flag = 0;
                        strcheck = Character.toUpperCase(strcheck);
                        scene.setText(scene.getText().toString().substring(0,scene.length()-1) + strcheck);
                        //scene.setText(previous + scene.getText().toString().substring(start, end).toUpperCase());
                        scene.setSelection(scene.getText().length());
                    }


                    else if (nowLower){
                        strcheck = Character.toLowerCase(strcheck);
                    }

                }
                else{
                    flag=1;
                }
            }

            public void afterTextChanged(Editable editable) {

            }

            public void beforeTextChanged(CharSequence cs, int i, int j, int k) {
            }
        });
Hassaan Rabbani
  • 2,469
  • 5
  • 30
  • 55

3 Answers3

0

Use a TextWatcher to detect that the user has added a newline to the EditText

Check this answer for more info.

Community
  • 1
  • 1
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

The TextWatcher may be what you are looking for. Further to your usage it does notify you when the user moves the cursor. Try to check the given position in the beforeTextChanged() callback, so that you can react on it, before the text changes it's value.

http://developer.android.com/reference/android/text/TextWatcher.html

Example:

txtEdit.addTextChangedListener (new TextWatcher() {

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

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

          }

          public void afterTextChanged(Editable s) {
          }
     });

You will get the new position of the cursor within these callbacks, this might be what you want.

Good Luck.

Thkru
  • 4,218
  • 2
  • 18
  • 37
0

TextWatchers does not tell you about selecrion changes, use a SpanWatcher instead, set it as a span to your Editable text with start == 0, end == Editable.length() and flags == Spannable.SPAN_INCLUSIVE_INCLUSIVE

pskink
  • 23,874
  • 6
  • 66
  • 77