I'm creating a notes app and I keep running into a problem with Spannable strings
What I'm trying to achieve is when I'm typing in a note and i click a button, i want it to add a big, bold string into the EditText field which has been achieved with the help of spannable strings. the problem im having now is when i save the note and exit, then go back into the note, the spannable string are still there but are no longer big and bold.
Is there a way to save the spannable strings? if not, is there any way to get this same effect?
also side note: i was wondering if theres a way to make the text of the spannable string one object. so when i erase it, it erases all together. not each letter like regular text.
Here's the button in my NoteActivity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_note:
save_note();
return true;
case android.R.id.home:
save_note();
finish();
return true;
case R.id.add_intro:
SpannableString intro;
if (note_content.length() == 0) {
intro = new SpannableString("(Intro:)\n");
intro.setSpan(new StyleSpan(Typeface.BOLD), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
intro.setSpan(new RelativeSizeSpan(1.25f), 0, 8, 0);
} else {
intro = new SpannableString("\n\n(Intro:)\n");
intro.setSpan(new StyleSpan(Typeface.BOLD), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
intro.setSpan(new RelativeSizeSpan(1.25f), 0, 11, 0);
}
note_content.append(intro);
return true;
and here's how the notes being saved and loaded Utilities
public class Utilities {
public static final String FileExtention = ".bin";
public static boolean save_note(Context context, Note note) {
String fileName = String.valueOf(note.getDateTime()) + FileExtention;
FileOutputStream fos;
ObjectOutputStream oos;
try {
fos = context.openFileOutput(fileName, context.MODE_PRIVATE);
oos = new ObjectOutputStream(fos);
oos.writeObject(note);
oos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static ArrayList<Note> getAllSavedNotes(Context context) {
ArrayList<Note> notes = new ArrayList<>();
File filesDir = context.getFilesDir();
ArrayList<String> noteFiles = new ArrayList();
for(String file : filesDir.list()) {
if(file.endsWith(FileExtention)) {
noteFiles.add(file);
}
}
FileInputStream fis;
ObjectInputStream ois;
for(int i = 0; i < noteFiles.size(); i++) {
try{
fis = context.openFileInput(noteFiles.get(i));
ois = new ObjectInputStream(fis);
notes.add((Note)ois.readObject());
fis.close();
ois.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}
return notes;
}
heres the NoteAdapter
public class NoteAdapter extends ArrayAdapter<Note> {
public NoteAdapter(Context context, int resource, ArrayList<Note> notes) {
super(context, resource, notes);
}
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
//return super.getView(position, convertView, parent);
if(convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.item_note, null);
}
Note note = getItem(position);
if(note != null) {
TextView title = (TextView) convertView.findViewById(R.id.list_note_title);
TextView content = (TextView) convertView.findViewById(R.id.list_note_content);
TextView date = (TextView) convertView.findViewById(R.id.list_note_date);
Typeface music_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/melodymakernotesonly.ttf");
Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf");
title.setTypeface(music_font);
content.setTypeface(scribble_card);
title.setText(note.getTitle());
date.setText(note.getDateTimeFormatted(getContext()));
if(note.getContent().length() > 25) {
content.setText(note.getContent().substring(0,25) + "...");
} else {
content.setText(note.getContent());
}
if(note.getContent().length() <= 0) {
content.setText("(Empty Note..)");
} else {
content.setText(note.getContent());
}
if (note.getTitle().length() <= 0) {
title.setText("(Untitled)");
} else {
title.setText(note.getTitle());
}
}
return convertView;
}
and here my Note class
public class Note implements Serializable {
private long mDateTime;
private String mTitle;
private String mContent;
public Note(long dateTime, String title, String content) {
mContent = content;
mTitle = title;
mDateTime = dateTime;
}
public void setDateTime(long dateTime) {
mDateTime = dateTime;
}
public void setTitle(String title) {
mTitle = title;
}
public void setContent(String content) {
mContent = content;
}
public long getDateTime() {
return mDateTime;
}
public String getTitle() {
return mTitle;
}
public String getContent() {
return mContent;
}
public String getDateTimeFormatted(Context context) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:SS", context.getResources().getConfiguration().locale);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(new Date(mDateTime));
}