Open In App

How to Add Margin in Android using Jetpack Compose?

Improve
Improve
Like Article
Like
Save
Share
Report

In Android, Padding is used to offset the content of the view by a specific number of pixels from either direction, i.e., padding from left, right, top and bottom. Using Padding, we can create multiple borders to a view by applying a combination of multiple padding and border.

Add Margin in Android using Jetpack Compose

 

So in this article, we will show you how you could apply multiple borders using padding in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jcmultipadding
  
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
  
            // Creating a Simple Scaffold 
            // Layout for the application
            Scaffold(
  
                // Creating a Top Bar
                topBar = { TopAppBar(title = { Text("GFG | Multi-Padding", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
  
                // Creating Content
                content = {
  
                    // Creating a Column Layout
                    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
  
                        // Creating a Text with Multiple
                        // padding and borders
                        Text(
                            text = "Hello Geek!",
                            fontSize = 20.sp,
                            modifier = Modifier
                                .padding(8.dp) // margin
                                .border(2.dp, Color.Black) // outer border
                                .padding(8.dp) // space between the borders
                                .border(2.dp, Color.Green) // inner border
                                .padding(8.dp) // padding
                        )
                    }
                }
            )
        }
    }
}


Output:

You can see that we could apply two borders to the Text element.

Output

 



Last Updated : 20 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads