Skip to content Skip to sidebar Skip to footer

Select Text In To Edittext

I am working on an Android application. In my app I am facing a strange problem.Please have a look on my screen. when i select any thing from selction box then it will add twice t

Solution 1:

You are doing a think which can causes a Exception due to recycling. You are watching for text change event of edittext and if there is a text change then you are again setting a text on the EditText so again your text change event will be occurred and you will again set the text so it will continue and it will probably give you Stack overflow exception or hang your UI.

You should use beforeTextChanged event and modify the CharSequence which you are getting in argument. Probably this will work for you.

Solution 2:

Solved my problem by changing my code.

snd_txt.addTextChangedListener(newTextWatcher() {
        @OverridepublicvoidonTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stubif (start>0) {
                 int m=snd_txt.getText().toString().lastIndexOf(".");
         makeColorText(snd_txt.getText().toString().length(),count);    
            }
        }

        @OverridepublicvoidbeforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @OverridepublicvoidafterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });

publicvoidmakeColorText(int last,int count) {

    int start=last-44;
    if (start<1){
        start=0;
    }

    String s=snd_txt.getText().toString();

    if (count==before_val&count!=1) {

        s=s.substring(0, last-before_val);
        last=last-before_val;
    }

     SpannableStringss=newSpannableString(s);

     ss.setSpan(newForegroundColorSpan(R.color.Gray_Light), 0, start,0);
     ss.setSpan(newForegroundColorSpan(Color.BLACK), start, last, 0); 
    // customize ss here// ...



         snd_txt.setText(ss);


     before_val=count;


    snd_txt.setSelection(last);
}

Post a Comment for "Select Text In To Edittext"