Software Development Exam  >  Software Development Notes  >  How to Build a Simple Notes App in Android?

How to Build a Simple Notes App in Android? - Software Development PDF Download

How to Build a Simple Notes App in Android?

Last Updated: 13 Oct, 2020

Notes app is a tool used for creating, updating, and discarding short text notes. It serves various purposes such as maintaining to-do lists, jotting down important information for future reference, and more. This app proves to be highly useful when quick access to notes is needed.

Let's delve into creating an Android application to understand the process of developing a simple Notes App. In this tutorial, we will build a Notes App that enables users to add, remove, and edit data. The project will be implemented using the Java programming language.

The user can add any data, remove any data as well as edit any dataA sample GIF illustrating the functionality of the app is provided below to give you an overview of what we aim to achieve in this tutorial. Note that Java will be the language used for this project.JavaHow to Build a Simple Notes App in Android? - Software Development

Steps for Creating a Notes App

Step 1: Create a New Project

To initiate a new project in Android Studio, follow the steps outlined in "How to Create/Start a New Project in Android Studio." Ensure that you choose Java as the programming language for this project.

Step 2: Working with the activity_main.xml file

In the activity_main.xml file, incorporate a ListView and a TextView. The ListView will display the list of automatically saved notes, while the TextView will exhibit the GFG text. Below is the complete code snippet for the activity_main.xml file.

activity_main.xml
  • XML

Android UI Layout

  • ConstraintLayout in Android
  • ListView Implementation
  • TextView Implementation

ConstraintLayout in Android

  • ConstraintLayout is a powerful layout for designing a user interface in Android apps.
  • It allows you to create complex layouts with a flat view hierarchy.
  • Constraints define the position of UI elements relative to other elements or the parent layout.

ListView Implementation

  • ListView is used to display scrollable lists of items in Android.
  • It efficiently displays large datasets by recycling views.
  • Each item in a ListView is drawn by an adapter that pulls content from a data source.

TextView Implementation

  • TextView is used to display text to the user in an Android app.
  • It can also be used to display simple graphics using Spannables.
  • Text color, size, and alignment can be easily customized in a TextView.

Creating Menu and Layouts in Android Studio

  • Creating a new layout to display the menu:
  • Navigate to app > res and right-click to create a new directory named "menu".
  • Inside the menu directory, create a new Menu resource file named add_note_menu.xml.

Code for add_note_menu.xml:

How to Build a Simple Notes App in Android? - Software Development

Creating Empty Activity for Note Editing

  • Creating a new empty activity for note editing:
  • Navigate to app > java and right-click to create a new Empty Activity named "NoteEditorActivity".

Code for activity_note_editor.xml:

Storing Data and Using SharedPreferences

  • In the NoteEditorActivity.java file, write code to store data.
  • Add SharedPreferences to the app to store data in the device's memory.

Working with SharedPreference in Android Development

  • Storing Data with SharedPreference: In the NoteEditorActivity.java file, the code is written to store data using SharedPreference. SharedPreference is utilized to store data in the phone's memory. For instance:
    • Setting Values in SharedPreference: SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); editor.putString("name", "Elena"); editor.putInt("idName", 12); editor.apply();
  • Retrieving Data from SharedPreference: Data can be retrieved from SharedPreference as well. For example:
    • Retrieve data from SharedPreference: SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); // "No name defined" is the default value. String name = prefs.getString("name", "No name defined"); // 0 is the default value. int idName = prefs.getInt("idName", 0);
  • NoteEditorActivity.java Code: Below is the full code for the NoteEditorActivity.java file, with comments provided within the code for enhanced understanding.

NoteEditorActivity.java Code

import android.content.Context;import android.content.Intent;
import android.content.SharedPreferences;import android.os.Bundle;
import android.text.Editable;import android.text.TextWatcher;
import android.widget.EditText;import androidx.appcompat.app.AppCompatActivity;
import java.util.HashSet;

Inside NoteEditorActivity Class:

  • Initialization and Setup: The NoteEditorActivity class extends AppCompatActivity and includes methods like onCreate for setting up the activity. EditText and other necessary components are initialized here.
  • Text Change Listener: A TextWatcher is added to monitor text changes in the EditText field. When text is modified, the data is updated and stored using SharedPreferences.

Working with MainActivity.java

Step 5: Working with the MainActivity.java file

MainActivity.java Overview

  • MainActivity.java is a Java file that serves as the core component of an Android application.
  • It contains methods and logic for handling the main functionalities of the app.

Key Components of MainActivity.java

  • onCreateOptionsMenu(Menu menu): This method is responsible for creating the options menu in the app.
  • onOptionsItemSelected(MenuItem item): Handles the selection of menu items in the app.
  • ListView listView: Represents the ListView in the app UI for displaying notes.
  • ArrayAdapter arrayAdapter: Used for populating data into the ListView.

Functionality Overview

  • The app allows users to add, view, and delete notes.
  • Shared Preferences are used for storing and retrieving notes data.
  • Users can click on a note to edit it or long-press to delete it.
import android.content.Context;import android.content.DialogInterface;
import android.content.Intent;import android.content.SharedPreferences;
import android.os.Bundle;import android.view.Menu;
import android.view.MenuInflater;import android.view.MenuItem;
import android.view.View;import android.widget.AdapterView;
import android.widget.ArrayAdapter;import android.widget.ListView;
import androidx.annotation.NonNull;import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;import java.util.ArrayList;
import java.util.HashSet;

Now set up all the things in the MainActivity.java file. Calling the NoteEditorActivity.java code, join all the XML code to Java and run the app. Below is the complete code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.

MainActivity.java
  • Static ArrayList notes: Stores the notes added by the user.
  • ArrayAdapter arrayAdapter: Binds the notes data to the ListView.
  • onCreate(Bundle savedInstanceState): Initializes the activity, sets the layout, and populates the ListView with notes.
  • onCreateOptionsMenu(Menu menu): Inflates the options menu for adding a new note.
  • onOptionsItemSelected(MenuItem item): Handles the selection of menu items, directing to the NoteEditorActivity.
  • listView.setOnItemClickListener(): Navigates to NoteEditorActivity on clicking a note for editing.
  • listView.setOnItemLongClickListener(): Allows users to delete a note on long-press.

Overall, MainActivity.java forms the backbone of the note-taking app, managing note creation, editing, and deletion functionalities.

Output: Run on Emulator

  • Running on an Emulator allows you to test software applications on a simulated environment.

Video Player

  • The Video Player facilitates the playback of multimedia content.
  • Timestamps like 00:00 and 00:30 denote specific points in the video timeline.
  • Instructions may include using the Up/Down Arrow keys to adjust the volume.

Please Login to comment...

  • In order to leave a comment, users are required to log in using their credentials.
  • Interactions such as 'Like' can be performed after logging in.
The document How to Build a Simple Notes App in Android? - Software Development is a part of Software Development category.
All you need of Software Development at this link: Software Development
Download as PDF

Top Courses for Software Development

Related Searches

video lectures

,

mock tests for examination

,

Exam

,

Objective type Questions

,

Previous Year Questions with Solutions

,

How to Build a Simple Notes App in Android? - Software Development

,

Summary

,

MCQs

,

Extra Questions

,

shortcuts and tricks

,

Sample Paper

,

ppt

,

Viva Questions

,

How to Build a Simple Notes App in Android? - Software Development

,

Free

,

study material

,

practice quizzes

,

past year papers

,

How to Build a Simple Notes App in Android? - Software Development

,

Important questions

,

Semester Notes

,

pdf

;