-6

I am trying to implement a simple Rich Text editor. The code works fine for alphabets and numbers but the last character typed before I press space/enter/delete is repeated (appended) before the space or enter is appended.

example :if i type (hello world.) it displays (helloo world.).

Is there a way i can prevent this problem from occurring?

My Activity code is :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView) findViewById(R.id.tv);
    textMessage = (EditText) findViewById(R.id.editText1);

    textMessage.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {
            char c = s.charAt(s.length() - 1);

            Log.d("Value of c::", "" + c);
            if (c == '\n') {
                content.append("<br>");
                tv.setText(Html.fromHtml(content.toString()));
            } else {
                content.append(c);
                tv.setText(Html.fromHtml(content.toString()));
            }
        }

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

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

        }
    });

i had also tried onKey() but it does not work for the soft keyboard.

public boolean onKeyDown(int keyCode, KeyEvent event) {

System.out.println("pressed" + keyCode);

if (keyCode == KeyEvent.KEYCODE_DEL){ 
      content.deleteCharAt(content.length()); } 
else if (keyCode == KeyEvent.KEYCODE_ENTER){ 
      content.append("</br>");
      System.out.println("enter pressed"); }
else { char c = (char) keyCode;
      Log.d("Keycode cast to char:::::", "" + c);
      content.append(); } 
return super.onKeyDown(keyCode, event);
   }


@Override 
public boolean onKey(DialogInterface dialog, int keyCode,
 KeyEvent event) { 
// TODO Auto-generated method stub

System.out.println("pressed" + keyCode); 
if (keyCode == KeyEvent.KEYCODE_DEL){ 
      content.deleteCharAt(content.length()); }
else if (keyCode == KeyEvent.KEYCODE_ENTER){
      content.append("</br>");
      System.out.println("enter pressed"); } 
else 
     { char c = (char) keyCode;
     Log.d("Keycode cast to char:::::", "" + c);
     content.append(); } 
     return false; }
sisir
  • 111
  • 2
  • 13
  • 2
    See http://meta.stackexchange.com/questions/40164/should-we-close-fix-my-program-questions – sashoalm May 21 '14 at 06:40
  • @sashoalm sir what is the use of this site if a beginner cannot put up the problems he is facing and experts try to help him?? – sisir May 21 '14 at 06:51
  • 3
    This site is not a helpdesk for beginner's questions. We're not here to fix your code. It's more like a Wikipedia of programming, a huge collection of good questions and answers. It's meant to be useful primarily for Google searchers. It is not a forum, also. – sashoalm May 21 '14 at 06:57
  • "Stack Overflow is a question and answer site for professional and enthusiast programmers. It's built and run by you as part of the Stack Exchange network of Q&A sites. With your help, we're working together to build a library of detailed answers to every question about programming." this is what is mentioned in http://stackoverflow.com/tour please change that line – sisir May 21 '14 at 07:02
  • "This is not a place for beginners " – sisir May 21 '14 at 07:09
  • @sashoalm sir i dont mean disrespect but coud you please tell me where i should have gone with the above issue(if you dont mind)?clearly i am in the wrong place. – sisir May 21 '14 at 07:14
  • 2
    But the FAQ does not say it's a place for beginners. It says it's a place for "professional and enthusiast programmers". And yes, you're in the wrong place, if you want people to fix your code, better go to rentacoder.com. Or go to experts-exchange, perhaps? – sashoalm May 21 '14 at 07:20
  • 10
    @sashoalm Stack Overflow is also for beginners *who want to learn and showing efforts towards this*. – Shadow The GPT Wizard May 21 '14 at 07:33
  • @ShadowWizard Of course, if they want to learn and showing efforts towards this. Let's hope that's what OP wants, instead of, say, for others to fix his code. His [post on meta](http://meta.stackoverflow.com/questions/255629/not-geting-a-solution-for-my-question?lq=1) where he's complaining he's not getting an answer fast enough might have made me misunderstand. – sashoalm May 21 '14 at 07:46
  • 1
    @sashoalm of course, I was just negating your absolute statement "This is not a place for beginners" – Shadow The GPT Wizard May 21 '14 at 07:54
  • @sashoalm i am sorry but i never meant disrespect. the question i put was like "please give me some hint to solve issue" and not " please debug it for me" .and the post in meta was merely a request for more information on the site's working,i never thought it will qualify as spam. sincerely sorry. – sisir May 21 '14 at 08:04
  • 1
    I think showing your debugging efforts would make this question better. – kapa May 21 '14 at 08:06
  • @ShadowWizard sir i can assure you that i am sincerely working to solve this issue – sisir May 21 '14 at 08:07
  • 4
    Just giving assurances isn't nearly as useful as saying, "I tried X, but Y happened. I've observed Z which I don't understand" etc. It would also really help if you'd format your code in a more conventional way. Your IDE should be able to do this for you automatically. – Jon Skeet May 21 '14 at 08:56

2 Answers2

1

assume that you have a string like Shoe , so the statement

s.length() - 1 

will return 3 , keep in mind that array index in java will start from 0 , so

s.charAt(s.length() - 1)

will return character "e" again. then in your if else clause you append "e" to your String and the result would be "Shoee"

Edit: according to your comment,assume an empty String , so you enter first letter "s" so the s would be append to your content as expected,you enter second word "h" so that the character append to your content would be "h" so on to letter "e", but if you put space after "shoe" you have "shoe" in your content and your if statement would never be run and your last word would be repeated because of you check your char with == which is incorrect for String or charSequence . you should use equal() instead :

if(("\n").equals(c))

Arash GM
  • 10,316
  • 6
  • 58
  • 76
  • the string is initially empty and values are appended only in content.append(c); if the length was an issue , the problem should have repeated for anything i insert.. but it happens only for space/delete/enter buttons.. – sisir May 05 '14 at 05:54
  • `code` if(("\n").equals(c)) enter does not satisfy this condition.it just re-appends the last character and a space after that. – sisir May 05 '14 at 08:48
  • @sisir did you find solution for that because i am facing same issue – varun Jul 04 '14 at 05:28
0

finally solved it using a different approach. instead of appending each character , i made the changes using Span and Typeface for saving it using Html.toHtml(). instead of generating the Html document to save it manually, i used the predefined HTML class to generate it automatically.

sisir
  • 111
  • 2
  • 13