1. 程式人生 > 其它 >How to create a Notes application

How to create a Notes application

Introduction

We will create a Notes app

  • Allow the user to record their thoughts and ideas.

  • Allow the user to save and delete notes.

  • Allow the user to personalise the app by switching between light and dark colour themes.

  • Allow The user to adding dividing lines between the note previews on the home screen.

Final result

Getting started

Android Studio offers several ready-made project templates to help get you started.
For the Notes app, select the Basic Activity template.

The Basic Activity template provides your app with an action bar and a floating action button.
In this app:

  • The action bar will allow the user to navigate to the settings page.

  • The floating action button will enable the user to create a new note.

Next,You will be taken to a New Project window, which invites you to define a few details about the project.

  1. Add a name for the project (e.g. Notes).

  2. Ensure the language is set to Kotlin and the API level is set to 30.
    Currently, all Android apps must target at least API 30 to be published in the Google Play store.

Projects created using the Basic Activity template will automatically contain two fragments and a navigation graph.
We will not use fragments or navigation graphs in the Notes app so you can delete the following files and folders if
you wish:

  • FirstFragment.kt (Project > app > java > folder with the name of the project)

  • SecondFragment.kt (Project > app > java > folder with the name of the project)

  • fragment_first.xml (Project > app > res > layout)

  • fragment_second.xml (Project > app > res > layout)

  • navigation directory (Project > app > res)

Tip:
Android Studio offers a handy alternative called Auto Import, which will attempt to generate the requisite import statements automatically while you code.
File > Settings > Editor > General > Auto
Import
select ‘Add unambiguous imports on the fly’ and Optimise imports on the fly’ for both Java and Kotlin then press Apply and OK.

Defining the string resources used in the app

  • Each item of text that the app will display should be stored as a string resource.
  • A single string resource can be used across multiple locations in the app.
  • String resources help the app support multiple languages because you can define translations for each string
    strings.xml:Project > app > res > values
<resources>
   <string name="app_name">Notes</string>
   <string name="action_settings">Settings</string>
   <string name="title">Title</string>
   <string name="contents">Contents</string>
   <string name="cancel">Cancel</string>
   <string name="ok">OK</string>
   <string name="delete">Delete</string>
   <string name="select_theme">Switch to night theme?</string>
   <string name="add_dividers">Add dividing lines between notes?</string>
   <string name="add_new_note">Add a new note...</string>
   <string name="note_empty">Check the title and contents fields are not empty.</string>
   <string name="note_saved">Note saved!</string>
   <string name="note_deleted">Note deleted!</string>
</resources>
  • The name attribute is what you will use to reference the string else where in the app.

  • he text that will be displayed to the user is input between the opening and closing tags.