Skip to main content

Posts

Showing posts with the label Compose

Jetpack Compose: IconButton from vector resource

Introduction In modern Android development, Jetpack Compose has become a popular toolkit for building user interfaces. It simplifies the UI development process by using a declarative approach, where developers describe how UI elements should look based on the app's state. One useful feature in Jetpack Compose is the ability to use vector resources for displaying icons. In this article, we will break down an Android Kotlin example that demonstrates how to create IconButton elements from vector resources using Jetpack Compose. This example showcases how to build a basic user interface with buttons containing icons from vector resources. When these buttons are clicked, they update a message on the screen. This project not only highlights the simplicity and flexibility of Jetpack Compose but also emphasizes how easily developers can integrate vector-based icons in their UI. Main Components of the Code The code begins with a typical setup for an Android activity using AppCompatActivity...

Jetpack Compose: Icon from vector resource

Introduction Jetpack Compose, the modern toolkit for building native Android UIs, is rapidly transforming how developers approach Android app development. One of its powerful features is the ability to easily manage UI elements using composable functions. In this tutorial, we will explore a simple example that demonstrates how to create icons using vector resources within a Jetpack Compose layout. The code highlights how to effectively utilize vector assets to build clean, responsive interfaces, showcasing the flexibility of Compose in managing images and icons. In Android development, vector graphics are widely preferred over raster images for their scalability and small file sizes. This example illustrates how to include vector-based icons directly in a Jetpack Compose project, enhancing the visual appearance and overall performance of the app. We will break down the code in detail, so both new and experienced developers can understand the key concepts at play. Breakdown of Code : Ma...

Compose Glance: How to Create an App Widget in Android with Kotlin

Creating an app widget in Android using Kotlin and Jetpack Compose’s Glance framework is an elegant approach to bringing interactivity and dynamic UI to a user’s home screen. This article breaks down the key components of a CounterWidget app, which demonstrates how to build an increment counter widget. We will cover how to use Glance to structure the UI, manage state using Jetpack’s DataStore, and handle interactions like button clicks. The CounterWidget example is a great starting point for understanding the fundamentals of creating app widgets using modern Android development practices. In this explanation, we will walk through the code step-by-step, beginning with the widget’s layout and composable functions, followed by how the widget’s state is updated, and how actions such as button clicks are handled. By the end, you'll have a clear understanding of the necessary components involved in creating a fully functional app widget with Kotlin and Glance. Structure of the CounterW...

Jetpack compose: ViewModel Room delete clear data

Introduction This code demonstrates a Jetpack Compose application that utilizes Room database to manage employee data. The application allows adding new employees, deleting existing ones, and clearing all data. It leverages ViewModel to manage the data state and provide a single source of truth for the UI. Breakdown The code is organized into several classes: MainActivity.kt : This is the main activity that sets the content of the app using a Composable function called MainContent . MainContent.kt : This composable function builds the UI for the app. It retrieves data from the ViewModel, displays the number of employees, and presents a list of employees. It also provides buttons for adding new employees and clearing all data. RoomSingleton.kt : This class defines a Room database named "roomdb" with a single entity - Employee . It provides a singleton instance using the getInstance method. Employee.kt : This data class represents an employee with an ID (Long) and a ...

Jetpack compose: ViewModel Room edit update data

Introduction This code demonstrates an Android application built with Jetpack Compose and Room Persistence Library. The application manages employee data, allowing users to add, update, and clear employee records. The code utilizes several key components: Jetpack Compose: A modern UI framework for building declarative and reactive user interfaces. Room: A persistence library that provides an abstraction layer for SQLite databases. ViewModel: A lifecycle-aware class that manages data for a UI component. Breakdown of the Code The code is divided into several files: MainActivity.kt: The main activity of the application. It sets up the content of the screen using setContent and utilizes EmployeeViewModel to manage employee data. RoomSingleton.kt: Defines a singleton class for accessing the Room database instance. RoomEntity.kt: Defines the data structure for an employee ( Employee ) with fields for ID and full name. RoomDao.kt: Defines the Data Access Object (DAO) inte...