Skip to main content

Posts

Showing posts with the label Box

Jetpack Compose: Box gravity example

Introduction This example demonstrates the use of Jetpack Compose’s Box composable in an Android app to explore alignment and gravity within a UI. The Kotlin-based example showcases how to structure a simple user interface, positioning elements within a Box using different alignment settings. The sample app is designed to help Android developers understand how Box works in Jetpack Compose and how to apply various layout and alignment options effectively. It uses simple components, including a Scaffold , to wrap the layout and provide a consistent structure with a top bar. Structure and Setup The starting point of this project is the MainActivity , which extends AppCompatActivity . Upon creation of the activity, the setContent function is called to initiate the composable functions. The core layout is defined by the GetScaffold() composable, which is responsible for organizing the entire screen content. Within this scaffold, there is a TopAppBar that gives the app a standard navig...

jetpack compose - Box content alignment

Compose Box Content Alignment The Box is a layout widget of the android jetpack compose library. The Box layout size itself fits the content. When a Box layout size is bigger than the content size, then we have to align the content. Such as a Box layout may be filled the screen size, in this situation we need to show the content widget at the specified position of the Box layout. The following jetpack compose tutorial will demonstrate to us how we can align Box content in an android application. The Box widget constructor has an argument name ‘contentAlignment’ whose data type is ‘Alignment’. So, using this argument we can set the Box content alignment. Alignment is an interface to calculate the position of a sized box inside an available space. The ‘Alignment’ is often used to define the alignment of a layout inside a parent layout. The default value of this ‘contentAlignment’ argument is ‘Alignment.TopStart’. There are many available values for the ‘contentAlignment’ argume...

Jetpack Compose: Box rounded corners shape

Introduction Jetpack Compose is a modern toolkit for building native UI in Android, simplifying and accelerating UI development by using Kotlin code. One of its key strengths is the flexibility it provides in designing user interfaces, including easily customizing shapes, layouts, and components. In this example, we focus on using Box components in Jetpack Compose to create UI elements with rounded corners and circular shapes, illustrating how to handle custom shapes in your UI. In this article, we will break down an example Android app that demonstrates how to implement different box shapes such as boxes with rounded corners and circular shapes. The components are composed using Jetpack Compose’s powerful layout system, and the app includes a top app bar, background colors, and neatly arranged elements. We will walk through the core components and techniques used to achieve these shapes. Scaffold Layout and AppBar Setup The app's UI is built inside a Scaffold composable, which p...

jetpack compose - Box gradient background color

Compose Box Gradient Background Color The Box is a layout widget of the android jetpack compose library. The Box is a single-child container. The Box layout is helpful to align the child element in a specific position. The following jetpack compose tutorial demonstrates to us how we can set a gradient background color to a Box layout. The Box constructor has no specific argument to set or change its background color. So how do we set a background color to it? As with other layout widgets, we can set a background color to a Box widget by using its ‘modifier’ argument’s ‘background’ element. The modifier’s ‘background()’ function element has several constructors to specify a background color or brush for the widget. The ’background()’ element has a constructor that accepts a ‘brush’ argument. The ‘brush’ argument data type is ‘Brush’. So we can pass a Brush instance to the Box widget to draw its background. We can create any type of gradient brush to draw a gradient background ...

jetpack compose - Box center alignment

Compose Box Center Alignment The Box is a very popular layout widget in a jetpack compose android application. Box allows us to put a single element inside it. So, Box is a single-child container. When Box’s child widget is much smaller than the Box widget itself, the developer needs to align it inside the Box component. Such as we have a Box widget whose size is fill the available area and we have a Button widget inside the Box widget, in this situation we have to align our Box child widget as per our requirement. This android development tutorial shows you how you can center align a component inside a Box widget. Box center alignment is so easy to implement in a jetpack compose application. Let us describe the code, and how we dot it. At first, we put a Box layout widget in a composable function. Next, we set the Box modifier to fill the maximum available size. We also put a padding value and clip to make it rounded corners. Then we set the Box’s ‘contentAlignment’ property...

jetpack compose - Box vs Surface

MainActivity.kt package com.cfsuman.jetpackcompose import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.compose.setContent import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { GetScaffold() } } @Composable fun GetScaffold(){ Scaffold( topBar = {TopAppBar( title = {Text( "Compose - Box vs Surface", color ...

jetpack compose - Box elevation

Compose Box Elevation The Box is a layout widget in android jetpack compose. Box container shows a single widget at a time in a specified position inside its area. Sometimes android app developers draw a rectangle or a circle or a rounded corners rectangle using the Box widget. This is a very helpful widget to do that type of drawing. Now the question is how can an android app developer add an elevation around the Box widget when they draw a circle shape or a rectangular area? This android application development tutorial demonstrates to us how we can add an elevation to a Box widget in jetpack compose. But there is no parameter/argument/property of the Box widget to draw an elevation. So, we have to do a simple trick to achieve this feature in the Box component. We know the Surface is a material design component of the jetpack compose library. This Surface layout widget can hold another layout and it has a property named ‘elevation’. So we can simply set an elevation for the ...

jetpack compose - Box alignment

MainActivity.kt package com.cfsuman.jetpackcompose import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { GetScaffold() } } @Composable fun GetScaffold(){ Scaffold( topBar = {TopAppBar( title = {Text( "Compose - Box Alignment", color = Color.White)}, backgroundColor ...

jetpack compose - Box center

MainActivity.kt package com.cfsuman.jetpackcompose import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { GetScaffold() } } @Composable fun GetScaffold(){ Scaffold( topBar = {TopAppBar( title = {Text( "Compose - Box Center", ...

jetpack compose - Box rounded corners

MainActivity.kt package com.cfsuman.jetpackcompose import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { GetScaffold() } } @Composable fun GetScaffold(){ Scaffold( topBar = {TopAppBar( title = {Text( "Compose - Box rounded corners", color = Color.White)}, ...

jetpack compose - Box background color

MainActivity.kt package com.cfsuman.jetpackcompose import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { GetScaffold() } } @Composable fun GetScaffold(){ Scaffold( topBar = {TopAppBar( title = {Text( "Compose - Box background color", color = Color.White)}, backgroundColor = Color(0xFF333399)) }, ...

Jetpack Compose: How to use Box layout

Understanding the Box Layout in Jetpack Compose: An Introduction Jetpack Compose is Google's modern toolkit for building native Android UIs, allowing developers to write more expressive and concise code. One of the core features in Jetpack Compose is its versatile set of layout components, which help structure user interfaces efficiently. Among these components, the Box layout stands out for its simplicity and flexibility, making it ideal for stacking, positioning, and aligning elements. In this article, we'll delve into how to use the Box layout in Jetpack Compose, demonstrating its ability to position UI elements precisely within a confined space. We will explore an example where multiple buttons are placed at different corners and alignments within a single Box container. This practical use case will help you understand how Box can simplify UI design, particularly when elements need to overlap or be positioned relative to one another. What is the Box Layout? The Box la...