gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
create my own edittext in jetpack compose very customized, rounded border blue…
- User
create my own edittext in jetpack compose very customized, rounded border blue background, no padding
- Assistant
create my own edittext in jetpack compose very customized, rounded border blue background, no padding
Creating a custom EditText (TextField) in Jetpack Compose with specific styling such as rounded borders, a blue background, and no padding involves using BasicTextField to gain more control. Here's an example of how you can achieve this:
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.input.TextRange
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyCustomTextField()
}
}
}
@Composable
fun MyCustomTextField() {
val textState = remember { mutableStateOf(TextFieldValue()) }
BasicTextField(
value = textState.value,
onValueChange = { textState.value = it },
modifier = Modifier
.fillMaxWidth()
.background(Color.Blue, shape = RoundedCornerShape(8.dp))
.border(2.dp, Color.Blue, shape = RoundedCornerShape(8.dp)),
textStyle = androidx.compose.ui.text.TextStyle(
color = Color.White,
fontSize = 16.sp
),
decorationBox = { innerTextField ->
// Optionally add some padding and other decorations here
// Example: Padding of 8dp
// You can leave this empty if you want absolutely no padding
innerTextField()
}
)
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
Surface {
MyCustomTextField()
}
}
BasicTextField for a low-level composable, which allows for more customization compared to TextField.remember and mutableStateOf are used to manage the state of the text input.background() and border() modifiers.TextStyle, specifying color, font size, etc.Feel free to modify styling parameters like colors, corner radii, and font sizes to better fit your requirements.