I am creating a small application which has a good real world usage , I need some help as I am a junior dev.
I am using Lazy Column to display the name of a person in a vertical style so that it could be seen amongst the crowd easily.
But I want the font size of the characters in the list to dynamically decrease according to the height of the device so that the complete name is displayed on the screen and there should be no need to scroll the name.
I don’t know whether even the approach I am using is good or not.
I request my fellow developers to please help and guide me.
package com.example.screenshout
import AutoResizedText
import android.app.Activity
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
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.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.modifier.modifierLocalConsumer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.view.WindowCompat
import com.example.screenshout.ui.theme.ScreenShoutTheme
class NameActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ScreenShoutTheme {
// A surface container using the 'background' color from the theme
val hexColor= intent.getStringExtra("hexColor")
val color= Color(hexColor!!.toLong(16))
val enteredText= intent.getStringExtra("name")
val textList= enteredText?.toCharArray()?.map { it.toString() }?: listOf()
LazyColumn(modifier= Modifier
.fillMaxSize()
.background(color)
,
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
items(textList){char->
Text(text = char,color=Color.Black, fontSize = 150.sp)
}
}
SideEffect {
val window = (this as Activity).window
WindowCompat.setDecorFitsSystemWindows(window, false)
window.statusBarColor = color.toArgb()
window.navigationBarColor = color.toArgb()
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview2() {
ScreenShoutTheme {
Greeting("Android")
}
}
I expect the font size of the items in the list should dynamically decrease or increase within the height of the current device and with the maximum font size possible so that we’re using maximum height of the screen to display the name.
Mohammed Mustafa Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.