2

I have a EditText box in which i want to control the cursor and modify the text programmatically

  • I have 12 button keypad made using 12 buttons in a GridView. With each button press I have a specific text which is to be inserted in EditText box at cursor position. For this i need cursor position so that i can insert the my custom text in the EditText view
  • I have two buttons for moving cursor position left/right by one character. Alternatively the cursor can also be set by touching EditText view (as EditText is supposed to behave)
  • Also i want the current position of cursor in EditText whenever Cursor position changes (I think i have to implement some kind of interface but i dont know how)

What i have tried so far

  • I am storing the key presses in an ArrayList<String>
  • I am setting the edittext.setText(String) everytime a key is pressed
  • can get the editable text through getText() but setText() only accepts strings.

Hence i am confused. What should i do to fulfill all my requirements.
PS: I am a Android beginner, and am making my 2nd app (which is a scientific calculator if it helps)
also if anyone volunteers to review my code, I'll be deeply obliged to him

Archit
  • 913
  • 1
  • 10
  • 20

3 Answers3

2

I dont know why you need the cursor position of the textview but take a look at this question here : Get Cursor Position in Android in Edit Text?

Actually you can edit the text in the textview on code by getting the input or if you want you can implement the TextWatcher interface to know what every input the user type in your textview like this one:

 private class CheckText implements TextWatcher {
    @Override
        public void afterTextChanged(Editable s) {
                  //triggers after the user changed the input on text
        }
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
                      //triggers before the user changed the input on text
        }
        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
                   //triggers when the user changed the input on text
        }
    }
Community
  • 1
  • 1
bean_droid
  • 117
  • 7
1

To set any number in EditText use Wrapper class then set it on EditText using toString().

For set position you can use editText.setSelection(position);

Coder
  • 186
  • 1
  • 1
  • 9
1

It is how I did something similar

  1. Stored id of all the button in a array as shown below.

    int[] kbdButtons = { R.id.button1, R.id.button2, R.id.button3,
            R.id.button4, R.id.button5, R.id.button6, R.id.button7,
            R.id.button8, R.id.button9, R.id.button10, R.id.button11,
            R.id.button12, R.id.button13, R.id.button14, R.id.button15}
    

    2.Then added custom Onclicklistner to all buttons in the kbdButtons array

     for (int i = 0; i < kbdButtons.length; i++) {
            Button buttonNum = (Button) dialoglayout
                    .findViewById(kbdButtons[i]);
            buttonNum.setOnClickListener(hindiKbdBtnsClick);
        }
    
  2. and here is the declaration of custom clicklistner

    private OnClickListener hindiKbdBtnsClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int btnId = v.getId();
            if (isShift && btnId != R.id.kbdKeyLeftShift
                    && btnId != R.id.kbdKeyRightShift) {
                sNotPressedView.setVisibility(View.VISIBLE);
                sPressedView.setVisibility(View.GONE);
                isShift = false;
            }
            if (btnId == R.id.kbdKeyLeftShift || btnId == R.id.kbdKeyRightShift) {
                if (!isShift) {
                    sNotPressedView.setVisibility(View.GONE);
                    sPressedView.setVisibility(View.VISIBLE);
                    isShift = true;
                } else {
                    sNotPressedView.setVisibility(View.VISIBLE);
                    sPressedView.setVisibility(View.GONE);
                    isShift = false;
                }
    
            } else if (btnId == R.id.kbdKeySpace || btnId == R.id.kbdKeyEnter) {
                hkEditText.append(" ");
            } else if (btnId == R.id.kbdKeyBackSpace) {
                String txt_curr_val = hkEditText.getText().toString();
                if (txt_curr_val.length() != 0)
                    txt_curr_val = txt_curr_val.substring(0,
                            txt_curr_val.length() - 1);
                hkEditText.setText(txt_curr_val);
                hkEditText.setSelection(txt_curr_val.length());
            }else if (btnId == R.id.kbdKeyHide) {
                mDialog.hide();
            }else {
                Button b = (Button) v;
                String btnText = b.getText().toString();
                hkEditText.append(btnText);
            }
    
        }
    };
    

Explanation - hkEditText is my editText view i.e.

EditText hkEditText = (EditText)FindViewById(R.id.myEdittextId);

-see I just appended the text written on the button that is pressed at that time. -you can also see the functionality of some special buttons like space, shift and enter

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Manjeet Singh
  • 4,382
  • 4
  • 26
  • 39