0

I have a programmatically view with an EditText as a main feature. I need to be able to edit back the contents on it, but whenever i click the EditText everything gets deleted. How can i avoid everything being deleted on the EditText when the user back to edit it?(the user enters some other view in the same activity, and then go back to edit the contents on the Edit text)

For others functionalities i have TextWatcher implemented.

Where should i look to change this behaviour (look the image please)? I though that on the text watcher could change this, but i'm out ideas.

class CoolEditText{    
    textField = new EditText(c);
        textField.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        textField.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
        textField.setLongClickable(false);
        textField.setTextIsSelectable(false);
    //more init here...

    textField.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //some code needed
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //some code needed
        }

        @Override
        public void afterTextChanged(Editable editable) {
            //some code needed
        }//....

Needed and current behaviour

Already solved:

Already solved this. The default or common behavior of Android's EditText is allow edit it's contents. On my problem i have an instance of CoolEditText somewhere else in the code where onFocusChange was implemented and assigned to that instance. So inside the onFocusChage the mText value was being reset to empty string.

class AnotherClass{ 
CoolEditText te = new CoolEditText();//...

te.setOnFocusChangeListener(new OnFocusChangeListener(){
                        @Override
                        public void onFocusChange(View view, boolean hasFocus) {
                            if(!te.equals("")){
                                te.setText(""); //i just delete this
                            }
                        }
                    });

So, briefing, EditText allows by default edit the contents inside, to change this behavior you should assign an OnFocusChangeListener and overwrite onFocusChange where you can change this behavior.

Xaren
  • 491
  • 1
  • 4
  • 15
  • Whenever the user leaves your activity, it may get destroyed (and recreated when they come back). In fact, even changing the screen orientation does this. You need to look into how to store the data somewhere, and re-load it. – jpaugh Feb 01 '18 at 22:51
  • User always stay on the same activity, the problem is when he go back to edit again the EditText – Xaren Feb 01 '18 at 22:57
  • It's still [the same problem](https://stackoverflow.com/q/6045361/712526), but my memory of Android is a little fuzzy. See linked (linked) solution. – jpaugh Feb 01 '18 at 23:04
  • No, the activity never pass for on pause(), resume etc, its on the same activity, i have already have solved that – Xaren Feb 01 '18 at 23:06
  • Already solved this. The default or common behavior of Android's EditText is allow edit it's contents. On my problem i have an instance of CoolEditText somewhere else in the code where onFocusChange was implemented and assigned to that instance. So inside the onFocusChage the mText value was being reset to empty string. something like next comment: – Xaren Feb 02 '18 at 19:44
  • class AnotherClass{ CoolEditText te = new CoolEditText();...te.setOnFocusChangeListener(new OnFocusChangeListener(){ @Override public void onFocusChange(View view, boolean hasFocus) { if(!te.equals("")){ te.setText(""); //i just delete this } } }); – Xaren Feb 02 '18 at 19:45

0 Answers0