2

I am starting to code an app for taking notes, like Evernote or a basic preinstalled memo app, so that I can learn and practice coding.

Currently I save the user input in a .txt file, so that every note would have an own text file in the storage, with the note content. What are other methods of saving user input in storage (you don't need to explain it, keyword would be appropriate) and what are the advantages or disadvantages of doing so? What can be cons of saving text files like I'm now doing?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 3
    You can save them in a sqlite database. Pro: you can easily search them. Con: ? – Johannes Kuhn Oct 03 '19 at 12:46
  • You can check all possible options here: https://developer.android.com/guide/topics/data/data-storage – MrVasilev Oct 03 '19 at 12:53
  • Thanks, but my question was more about HOW to save user input, and not where. I am fine with saving notes on the internal storage, but what are other methods of saving strings instead of writing them in .txt files? – CodeStallion Oct 03 '19 at 12:56
  • Please send details. How you get input from the user? Do you use EditText? If not, what do you use to gather the data from the user? – HF_ Oct 03 '19 at 13:01
  • Yes I am currently using EditText and then FileOutputStream – CodeStallion Oct 03 '19 at 13:04
  • I edited my answer. Please review it. – HF_ Oct 03 '19 at 13:27

1 Answers1

1
  1. Save the content to a file in your app's cache
  2. If the content is plain text (and not too long), you can easily use SharedPreferences to save the content
  3. You can use a database

Note that if the content is rich text, you can format that (for example, using HTML, JSON or XML and save files (like images) in a specified folder and write the location of the files to the formatted text) and then save to a database.

Useful links to get started:

Using databases:

Rich Text Editors:

How to get cache directory?

File cacheDir = this.getCacheDir();

or

File cacheDir = this.getApplicationContext().getCacheDir();

Note that if the content is important, you can create a new folder in the storage (like "My App Name Files") and save the content to that folder.


If you are using EditText:

I name the EditText uinput. Here we go:

private void saveContent() {
    String content = uinput.getText().toString();
    String name = "Note 1"; // You can create a new EditText for getting name
    // Using SharedPreferences (the simple way)
    SharedPreferences sp = this.getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();
    editor.putString(name, content);
    editor.apply();
}

private Map<String, ?> getAllNotes() {
    SharedPreferences sp = this.getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
    return sp.getAll();
}

private String getNoteContent(String noteName) {
    SharedPreferences sp = this.getApplicationContext().getSharedPreferences("notes", Context.MODE_PRIVATE);
    return sp.getString(noteName, "Default Value (If not exists)");
}

Don't save other things in SharedPreferences "notes".

HF_
  • 689
  • 3
  • 9
  • 22
  • Thanks for the detailed answer. So this works only for plain text, as only Strings are saved? If I am looking to add multiple functionalities like bold text, images, check boxes and in general text formatting in the note, which method for saving content would you suggest? – CodeStallion Oct 03 '19 at 13:31
  • Maybe there are some libraries to edit, and save rich text to a file, but you can manually use spans and format them with HTML, JSON or XML. – HF_ Oct 03 '19 at 13:43
  • An example of JSON (custom formatting, not based on a standard): `{ "name":"Note 1","content":[ {"blocktype":"text", "content":"A simple text"},{"blocktype":"text","style":"bold","content":"And a bold text"} ] }` To add a image you can use `{"blocktype":"img","imgid":"img14.png","extraParamsForImage_LikeSize":"blah blah"}` and save all of the images to a specific folder, and to load this image, use that folder and the `imgid` parameter (`\storage\PathToTheImages\img14.png`) – HF_ Oct 03 '19 at 13:43
  • Please take a look at https://developer.android.com/guide/topics/text/spans if you didn't already read that. Spans are very good, there is also `ImageSpan`. Take a look at https://developer.android.com/reference/android/text/style/package-summary section Classes to see the most spans available. – HF_ Oct 03 '19 at 13:44
  • If you don't know `JSON`, please read https://en.wikipedia.org/wiki/JSON It's very very simple, and easy to learn and use. It's almost available in the most famous languages like Java, PHP and... How to use `JSON` in Android: https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java – HF_ Oct 03 '19 at 14:01