0

I'm making an app in which I'm using Spannble class to create a highlighter mechanism in an EditText, which works fine. Now, I want to store the span position(s) in a SQLiteDatabase, so that I can reload them and have the Spanned Text to show it in a TextView.

Here's my TextView alongwith the highlighter mechanism -

package com.Swap.RR;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.graphics.Typeface;

import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;

import android.text.style.BackgroundColorSpan;

import android.view.Menu;
import android.view.View;

import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.EditText;
import android.widget.ToggleButton;

public class Add_New_Note extends Activity {

private EditText note;
private int mStart;
private Typeface Roboto_lt;
private int end;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add__new__note);

    note = (EditText) findViewById(R.id.notecontent);
    ToggleButton highlighter = (ToggleButton) findViewById(R.id.highlightToggle);
    Roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
    note.setTypeface(Roboto_lt);

    mStart = -1;    
    final TextWatcher highlightWatcher = new TextWatcher() {


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

       public void onTextChanged(CharSequence s, int start, int before, int count)
    {
        if(mStart > 0)
        {
            end = note.getText().length();
            note.getText().setSpan(new BackgroundColorSpan(getResources().getColor(R.color.highlighter_green)), mStart, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
       public void afterTextChanged(Editable s) {
       }
    };

    highlighter.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton button, boolean isChecked) {


            if (isChecked == true) {  
                //start highlighting when the ToggleButton is ON
                if(mStart == -1) mStart = note.getText().length();
                else mStart = -1;
                mStart = note.getText().length();

                note.addTextChangedListener(highlightWatcher);
            } else {
                //stop highlighting when the ToggleButton is OFF
                note.removeTextChangedListener(highlightWatcher);
            }
        }
    });

} 


public void noteDone(View v) {

    String noteContent = note.getText().toString();

    Intent i = new Intent("com.Swap.Notes_page");

    i.putExtra("NOTE", noteContent);

    setResult(RESULT_OK, i);
    finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_add__new__note, menu);
    return true;
}
}

Please help me with some suggestions on how to do it. Thanks!

Swap
  • 480
  • 2
  • 7
  • 21

1 Answers1

1

Your best bet is to use Html.toHtml() to convert the Spannable into HTML. You can then use Html.fromHtml() to convert the HTML back into a Spannable when you query the database later on. You will want to do adequate testing, though, as toHtml() and fromHtml() are not guaranteed to support the same HTML tags.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491