Android Day 13 When to choose spacer and when to choose padding, in my experience.

Some of you guys may think that padding is good and convinent to use than spacer, but in fact, in my experience, is the contrary.

Let me show you why:

package com.williamjiamin.ourowncomposable

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.ui.Alignment
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowForward
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.williamjiamin.ourowncomposable.ui.theme.OurOwnComposableTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            OurOwnComposableTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    StackOfGreetings()
                }
            }
        }
    }
}

@Composable
fun StackOfGreetings() {

    Column (
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally
    )
    {
        UserInputField()
    }

}

@Composable
fun UserInputField() {

    Column(
        modifier = Modifier.fillMaxSize(),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        Text("Please Enter the Text You need to Translate:")
        Spacer(modifier = Modifier.height(16.dp))
        OutlinedTextField(value = "My name is William!", onValueChange = {

        })

        Spacer(modifier = Modifier.height(16.dp))

        val context = LocalContext.current

        Row(
            modifier = Modifier,
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Button(
                onClick = { Toast
                    .makeText(context,
                        "You have subscribed to William Jiamin channel!",
                        Toast.LENGTH_LONG).show() },
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.LightGray,
                    contentColor = Color.Black
                )
            ) {
                Text("Original")
            }

            // Spacer(modifier = Modifier.width(16.dp))

            Icon(
                imageVector = Icons.Filled.ArrowForward,
                contentDescription = "Arrow Forward",
                modifier = Modifier.padding(16.dp),
            )

            Button(
                onClick = { Toast
                    .makeText(context,
                        "You have subscribed to William Jiamin channel!",
                        Toast.LENGTH_LONG).show() },
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.LightGray,
                    contentColor = Color.Black
                )
            ) {
                Text("Translated")
            }
        }

        Spacer(modifier = Modifier.height(16.dp))

        OutlinedTextField(value = "私は William Jiaminです!", onValueChange = {

        })
    }
}

@Preview(showBackground = true)
@Composable
fun StackOfGreetingsPreview() {
    OurOwnComposableTheme {
    StackOfGreetings()
    }
}

In the updated version of my design.you can see I heavily use spacer for the spancing .

image-20240202073824698

when you use padding, in a complicated UI, you may encounter something like this:

image-20240202075014861

Because the padding of one element can effect other element ,and sometimes you forget about change other element ,and things and get really messy really quickly

            Button(
                onClick = { Toast
                    .makeText(context,
                        "You have subscribed to William Jiamin channel!",
                        Toast.LENGTH_LONG).show() },
                colors = ButtonDefaults.buttonColors(
                    containerColor = Color.LightGray,
                    contentColor = Color.Black
                )
            ) {
                Text("Original", modifier = Modifier.padding(16.dp))
            }