Project 1: Notes App with Room and Compose

Android Development 20 min min read Updated: Nov 16, 2026 Intermediate
Project 1: Notes App with Room and Compose
Intermediate Topic 1 of 4

What We Build

  • List of notes with search
  • Add, edit and delete notes
  • Pin important notes to the top
  • Undo after delete
  • Data saved with Room, structure with MVVM and Hilt

1. Data Layer

kotlin @Entity(tableName = "notes") data class NoteEntity( @PrimaryKey(autoGenerate = true) val id: Int = 0, val title: String, val content: String, val pinned: Boolean = false, val updatedAt: Long = System.currentTimeMillis() ) @Dao interface NoteDao { @Query("SELECT * FROM notes WHERE title LIKE '%' || :query || '%' OR content LIKE '%' || :query || '%' ORDER BY pinned DESC, updatedAt DESC") fun observe(query: String): Flow<List<NoteEntity>> @Query("SELECT * FROM notes WHERE id = :id") suspend fun getById(id: Int): NoteEntity? @Upsert suspend fun upsert(note: NoteEntity): Long @Delete suspend fun delete(note: NoteEntity) } @Database(entities = [NoteEntity::class], version = 1, exportSchema = true) abstract class AppDatabase : RoomDatabase() { abstract fun noteDao(): NoteDao } interface NoteRepository { fun notes(query: String): Flow<List<NoteEntity>> suspend fun get(id: Int): NoteEntity? suspend fun save(note: NoteEntity) suspend fun delete(note: NoteEntity) } class DefaultNoteRepository @Inject constructor(private val dao: NoteDao) : NoteRepository { override fun notes(query: String) = dao.observe(query) override suspend fun get(id: Int) = dao.getById(id) override suspend fun save(note: NoteEntity) { dao.upsert(note.copy(updatedAt = System.currentTimeMillis())) } override suspend fun delete(note: NoteEntity) = dao.delete(note) }

2. Hilt Modules

kotlin @Module @InstallIn(SingletonComponent::class) object DatabaseModule { @Provides @Singleton fun provideDb(@ApplicationContext context: Context): AppDatabase = Room.databaseBuilder(context, AppDatabase::class.java, "notes.db").build() @Provides fun provideDao(db: AppDatabase): NoteDao = db.noteDao() } @Module @InstallIn(SingletonComponent::class) abstract class RepositoryModule { @Binds abstract fun bindRepo(impl: DefaultNoteRepository): NoteRepository } @HiltAndroidApp class NotesApp : Application()

3. List ViewModel

kotlin @HiltViewModel class NoteListViewModel @Inject constructor(private val repository: NoteRepository) : ViewModel() { val query = MutableStateFlow("") private var lastDeleted: NoteEntity? = null @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) val notes: StateFlow<List<NoteEntity>> = query .debounce(250) .flatMapLatest { repository.notes(it.trim()) } .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList()) fun onQueryChange(value: String) { query.value = value } fun togglePin(note: NoteEntity) = viewModelScope.launch { repository.save(note.copy(pinned = !note.pinned)) } fun delete(note: NoteEntity) = viewModelScope.launch { lastDeleted = note; repository.delete(note) } fun undoDelete() = viewModelScope.launch { lastDeleted?.let { repository.save(it) } lastDeleted = null } }

4. List Screen

kotlin @OptIn(ExperimentalMaterial3Api::class) @Composable fun NoteListScreen( onAdd: () -> Unit, onOpen: (Int) -> Unit, viewModel: NoteListViewModel = hiltViewModel() ) { val notes by viewModel.notes.collectAsStateWithLifecycle() val query by viewModel.query.collectAsStateWithLifecycle() val snackbar = remember { SnackbarHostState() } val scope = rememberCoroutineScope() Scaffold( topBar = { TopAppBar(title = { Text("Notes") }) }, floatingActionButton = { FloatingActionButton(onClick = onAdd) { Icon(Icons.Default.Add, "Add note") } }, snackbarHost = { SnackbarHost(snackbar) } ) { padding -> Column(Modifier.padding(padding)) { OutlinedTextField( value = query, onValueChange = viewModel::onQueryChange, placeholder = { Text("Search notes") }, singleLine = true, leadingIcon = { Icon(Icons.Default.Search, null) }, modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp) ) if (notes.isEmpty()) { Box(Modifier.fillMaxSize(), Alignment.Center) { Text("No notes yet. Tap + to add one.") } } else { LazyColumn(contentPadding = PaddingValues(16.dp), verticalArrangement = Arrangement.spacedBy(8.dp)) { items(notes, key = { it.id }) { note -> NoteCard( note = note, onClick = { onOpen(note.id) }, onPin = { viewModel.togglePin(note) }, onDelete = { viewModel.delete(note) scope.launch { val result = snackbar.showSnackbar("Note deleted", "Undo", duration = SnackbarDuration.Short) if (result == SnackbarResult.ActionPerformed) viewModel.undoDelete() } }, modifier = Modifier.animateItem() ) } } } } } } @Composable fun NoteCard(note: NoteEntity, onClick: () -> Unit, onPin: () -> Unit, onDelete: () -> Unit, modifier: Modifier = Modifier) { Card(onClick = onClick, modifier = modifier.fillMaxWidth()) { Row(Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically) { Column(Modifier.weight(1f)) { Text(note.title.ifBlank { "Untitled" }, style = MaterialTheme.typography.titleMedium, maxLines = 1, overflow = TextOverflow.Ellipsis) Text(note.content, maxLines = 2, overflow = TextOverflow.Ellipsis, color = MaterialTheme.colorScheme.onSurfaceVariant) } IconButton(onClick = onPin) { Icon(if (note.pinned) Icons.Filled.PushPin else Icons.Outlined.PushPin, "Pin") } IconButton(onClick = onDelete) { Icon(Icons.Default.Delete, "Delete") } } } }

5. Editor ViewModel and Screen

kotlin data class EditorState(val title: String = "", val content: String = "", val pinned: Boolean = false, val saved: Boolean = false) @HiltViewModel class NoteEditorViewModel @Inject constructor( private val repository: NoteRepository, savedStateHandle: SavedStateHandle ) : ViewModel() { private val noteId: Int = savedStateHandle["noteId"] ?: 0 private val _state = MutableStateFlow(EditorState()) val state = _state.asStateFlow() init { if (noteId != 0) viewModelScope.launch { repository.get(noteId)?.let { n -> _state.value = EditorState(n.title, n.content, n.pinned) } } } fun onTitle(v: String) = _state.update { it.copy(title = v) } fun onContent(v: String) = _state.update { it.copy(content = v) } fun save() = viewModelScope.launch { val s = _state.value if (s.title.isBlank() && s.content.isBlank()) { _state.update { it.copy(saved = true) }; return@launch } repository.save(NoteEntity(id = noteId, title = s.title.trim(), content = s.content.trim(), pinned = s.pinned)) _state.update { it.copy(saved = true) } } } @OptIn(ExperimentalMaterial3Api::class) @Composable fun NoteEditorScreen(onDone: () -> Unit, viewModel: NoteEditorViewModel = hiltViewModel()) { val state by viewModel.state.collectAsStateWithLifecycle() LaunchedEffect(state.saved) { if (state.saved) onDone() } Scaffold(topBar = { TopAppBar( title = { Text("Note") }, navigationIcon = { IconButton(onClick = onDone) { Icon(Icons.AutoMirrored.Filled.ArrowBack, "Back") } }, actions = { TextButton(onClick = viewModel::save) { Text("Save") } } ) }) { padding -> Column(Modifier.padding(padding).padding(16.dp), verticalArrangement = Arrangement.spacedBy(12.dp)) { OutlinedTextField(state.title, viewModel::onTitle, label = { Text("Title") }, modifier = Modifier.fillMaxWidth()) OutlinedTextField(state.content, viewModel::onContent, label = { Text("Content") }, modifier = Modifier.fillMaxWidth().weight(1f)) } } }

6. Navigation

kotlin @Serializable object ListRoute @Serializable data class EditorRoute(val noteId: Int = 0) @Composable fun NotesNavHost() { val nav = rememberNavController() NavHost(nav, startDestination = ListRoute) { composable<ListRoute> { NoteListScreen(onAdd = { nav.navigate(EditorRoute()) }, onOpen = { nav.navigate(EditorRoute(it)) }) } composable<EditorRoute> { NoteEditorScreen(onDone = { nav.popBackStack() }) } } }
SavedStateHandle and type-safe routes: Hilt's SavedStateHandle receives navigation arguments by their property names, so noteId in the route matches savedStateHandle["noteId"].
Concept usedFrom lesson
Room, DAO, FlowRoom database
Hilt modulesDependency injection
StateFlow and collectAsStateWithLifecycleViewModel
LazyColumn, Card, ScaffoldCompose lists and Material 3
Snackbar with UndoDialogs and snackbars
Extend it: Add colors per note, tags with a relationship table, a home screen widget with Glance, and ViewModel unit tests with a fake repository.

Conclusion

You built a full app with local persistence, clean architecture, search, undo and navigation. This is the base of many real apps.

Quick Summary: Room stores notes, a repository hides it, Hilt wires everything, ViewModels expose StateFlow and Compose shows it.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators