Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove character from edittext?

Tags:

android

I want to know is there any method by which i can remove the specific character from the EditText. I want to remove charter from EditText by specifying position in edit text is there something like removeAt() for EditText?

    public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button b1=(Button)findViewById(R.id.button1);
        final EditText ed1=(EditText)findViewById(R.id.editText1);
        b1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

             //i want something like this
                    ed1.removeAt(5);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }


}

EXTRA INFORMATION

the main problem is i want to make text bold inside edittext but not all the text. The text that is typed after setting bold button on(bold is my toggle button). ok here is the code

     ed1.setOnKeyListener(new OnKeyListener() {

        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            int i = 0;

            switch(event.getAction())
            {
            case KeyEvent.ACTION_UP:
                    if(bold.isChecked())
                    {
                         i=ed1.getSelectionStart();

                         ed1.append(Html.fromHtml("<b>"+ed1.getText().toString().charAt(i-1)+"</b>"));

                    }
                    return true;

            }
            return false;
        }
    });

the problem is that i get double character's one is normal character and then again the bold one so i want to remove that normal characters by specifying there position

like image 340
maddygoround Avatar asked Aug 10 '12 16:08

maddygoround


3 Answers

You have to change it to string first because getText returns editable, e.g:

int charIndex;
String text = ed1.getText().toString();
text = text.substring(0, charIndex) + text.substring(charIndex+1);
ed1.setText(text);

To remove a Character and keep the BOLD characters the same :

ed1.setText((Spanned)ed1.getText().delete(indexStart , indexEnd));

This will remove the Characters from index "indexStart" to "indexEnd -1". e.g. if you use delete(0,1); it will delete the first Character.

I hope that helps :)

like image 142
Eddy K Avatar answered Nov 13 '22 12:11

Eddy K


My Implementation is given below. I hope it will be helpful

int cursorPosition = mDialerEditText.getSelectionStart();
    if (cursorPosition > 0) {
          mDialerEditText.setText(mDialerEditText.getText().delete(cursorPosition - 1, cursorPosition));
        mDialerEditText.setSelection(cursorPosition-1);
    }
like image 37
Ajit Kumar Dubey Avatar answered Nov 13 '22 13:11

Ajit Kumar Dubey


EditText is just like any other TextView, but it can also be edited. Therefore you can use the getText() method, store it as a String, remove the desired char, and call setText(str) method on the EditText to change the text.

like image 1
RE6 Avatar answered Nov 13 '22 13:11

RE6