Creating your own application is an exciting process that opens the door to the world of mobile development. A simple notepad is an ideal project to start with, as it covers all the fundamental aspects of working with the platform Android. You'll learn how to design a user interface, work with databases, and manage the activity lifecycle.
In this article, we'll take a closer look at the architecture of a classic notes app. You will not need deep knowledge of level MVP or MVVM frameworks at the initial stage, but an understanding of the basics of object-oriented programming is required. We will use standard development environment tools Android Studio.
The result of our work will be a fully functional application capable of creating, editing and deleting records. The finished project can be used as a basis for more complex solutions or as a portfolio to demonstrate skills to an employer.
Preparing the development environment and choosing a stack
The first step is to install and configure the integrated development environment. Today, the industry standard remains Android Studio, which provides all the necessary emulators and profilers. Download the latest stable version from the official website of the developer and make sure that it is installed Android SDK.
When creating a new project, you will need to select a programming language. Although Java still widely used in legacy code, the language is highly recommended for new projects Kotlin. It has a more concise syntax, built-in protection against NullPointerException and full compatibility with existing Java libraries.
When setting up a project, pay attention to the minimum version of the SDK (minSdkVersion). Choosing a version that is too old will limit access to modern APIs, while choosing a version that is too new will reduce the audience of users. The optimal choice for a training project would be API level 21 (Android 5.0 Lollipop) or higher.
The project structure in Android Studio may seem complicated to a beginner, but for the notepad we only need a few key directories. The main code files will be located in the java or kotlinfolder, and the interface resources will be in the resfolder. It is important to immediately define packages for data models, adapters and activities.
โ ๏ธ Attention: When you first start the emulator, the system may take several minutes to load. Make sure that virtualization is enabled in your computer's BIOS (Intel VT-x or AMD-V), otherwise the emulator will not start or will work extremely slowly.
Use the emulator with a Google Play system image if you plan to test integration with cloud services or an application store. For pure testing of notepad logic, an image without Google services is suitable.
User interface design
The application interface should be intuitive and not overloaded with unnecessary elements. For the list of notes we will use the component RecyclerView, which is a modern and productive replacement for the outdated one ListView. It efficiently manages memory by recreating only the elements visible on the screen.
Each list element will be represented by a separate layout (item_layout.xml). In the simplest case, this is TextView for the title of the note and TextView for the summary. For the editing screen you will need EditText with multi-line input and control buttons.
The home screen layout usually includes CoordinatorLayout or ConstraintLayout. This allows you to flexibly position elements relative to each other and the edges of the screen. The button to add a new note is traditionally located in the lower right corner in the form FloatingActionButton.
Don't forget about the application menu. Using Toolbar instead of the standard ActionBar gives more opportunities for customization. The menu can include functions for sorting notes by date or alphabet, as well as a design theme.
- ๐ฑ Use
ConstraintLayoutto create a flat hierarchy, which improves rendering performance. - ๐จ Adapt font sizes using units of measurement
spso that the text scales correctly when changing system settings. - ๐ Implement swipe gestures to quickly remove notes from the list using
ItemTouchHelper.
Organizing data storage in SQLite
Any notepad application requires a reliable mechanism for storing information on the user's device. The standard solution in the ecosystem Android is an embedded relational database SQLite. It is lightweight, does not require separate installation and works directly with the device file system.
To work with the database, you need to create a helper class that inherits from SQLiteOpenHelper. This class defines the onCreate and onUpgrademethods. The method onCreate is called once upon first run and contains an SQL query to create the notes table.
The table structure must include a unique identifier (_id), title, content, and creation or modification timestamp. Data type for text - TEXT, for identifier - INTEGER PRIMARY KEY AUTOINCREMENT. This will allow the database to automatically assign new IDs to records.
Interaction with the database is carried out through an object SQLiteDatabase. To insert data, the method insertis used, for sampling - query or rawQuery. It is important to always close cursors when you are finished working with them to avoid memory leaks.
| Table field | SQL data type | Purpose description | Constraints |
|---|---|---|---|
_id |
INTEGER | Unique record key | PRIMARY KEY, AUTOINCREMENT |
title |
TEXT | Note title | NOT NULL |
content |
TEXT | Body text of the note | Can be empty |
timestamp |
BIGINT | Last modified time | In Unix time format |
Alternatives SQLite
For simple key-value stores you can use SharedPreferences, but for a list of notes this is ineffective. There is also the Room library, which is a wrapper over SQLite and simplifies working with the database through annotations.
Implementation of adapter logic for a list
The adapter acts as a link between the data in the database and the visual representation on the screen. Its main task is to take data from a list of objects and fill it View in each element RecyclerView. Correct implementation of the adapter is critical to smooth scrolling.
Within the adapter class, you must define an internal class ViewHolder. It caches references to interface elements (header, text) to avoid calling the method multiple times findViewById when scrolling the list. This significantly reduces the load on the processor.
The method onBindViewHolder is called every time an element appears on the screen. Here you should take the data from your model at the current index and assign it to text fields ViewHolder. Here you can also configure click handlers for going to editing.
When data in the database changes (adding or deleting a note), the adapter must be notified about this. Calling methods notifyItemInserted, notifyItemRemoved or notifyDataSetChanged forces RecyclerView to redraw the list taking into account new data.
โ ๏ธ Attention: Never perform read or write operations to the database in the main thread (UI Thread). This will cause the interface to freeze and a system error
Application Not Responding (ANR). Use separate threads or asynchronous tasks.
โ๏ธ Adapter implementation checklist
Activity management and navigation
Navigation between the list of notes and the editing screen is implemented through the mechanism Intent. When the user clicks on a list item or create button, the application launches a second activity, passing it the necessary data.
For the mode of editing an existing note, Intent you must put the ID of the entry. The editing activity checks for the presence of this ID at startup. If it exists, it loads data from the database into the input fields; if not, it clears the fields to create a new record.
Resulting from the editing activity is carried out through the method setResult. This allows the home screen to know if changes have been made and update the list without completely restarting the application. The result code (for example, RESULT_OK) signals a successful save.
The handling of the "Back" button on the device also requires attention. The user may accidentally press it without saving the changes. It is good practice to save the draft automatically or display a dialog box confirming exit if there are unsaved edits.
Using explicit Intents (specifying the target activity class) is preferable to implicit ones for internal navigation within your application, since this ensures that your component is launched.
Testing and debugging the application
Before considering the application ready, it is necessary to conduct thorough testing. Check the application on emulators with different versions Android and different screen resolutions. Pay special attention to the behavior when rotating the device: data in the input fields should not be lost.
Use the tool Logcat v Android Studio to monitor system logs. Output information messages to the log at key stages of work (opening the database, saving a record) to quickly localize the error in case of failure.
Test edge cases: what happens if you create a note with an empty title? What happens if you try to save text that is several megabytes long? Handling such situations will increase the stability of your product.
To automate verification, you can write unit tests using the framework JUnit. They will allow you to check the logic of working with the database without launching the application itself on the device, saving the developer's time.
- ๐ Use breakpoints in the debugger to step-by-step code execution and analyze variables.
- ๐ Check the adaptability of the interface on tablets and devices with a notch in the screen.
- โก Monitor memory consumption through Android Profiler to avoid leaks during long work.
Do you need knowledge of XML to create an interface?
Yes, the traditional approach to development for Android requires knowledge of XML to layout interfaces. However, in modern versions of Android Studio, support is available for Jetpack Compose, a toolkit for creating UIdeclarative in pure Kotlin, which is gradually replacing XML.
How to save data when deleting an application?
Data stored in the applicationโs internal memory (including the SQLite database) is deleted along with the application. To save data after deletion, you must use external storage or synchronization with a cloud service, which requires additional permissions.
Is it possible to publish an application on Google Play?
Yes, the application you create can be published in the Google Play store. To do this, you will need to register a developer account (one-time fee of $25), prepare an icon, screenshots and description, and also undergo content moderation.
What is a Manifest file and why do you need it?
The file AndroidManifest.xml contains metadata about the application: list of activities, required permissions (access to the Internet, camera, etc.), application version and minimum system requirements. Without the correct manifest, the application will not compile.