Saturday, April 24, 2021

jetpack compose - TextField size

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.layout.Column
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.text.input.TextFieldValue
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 {
            MainContent()
        }
    }


    @Composable
    fun MainContent(){
        Column(
            Modifier
                .background(Color(0xFFEDEAE0))
                .fillMaxSize()
                .padding(32.dp),
            verticalArrangement = Arrangement.spacedBy(24.dp)
        ) {
            var textState by remember { mutableStateOf(TextFieldValue()) }
            var textState2 by remember { mutableStateOf(TextFieldValue()) }
            var textState3 by remember { mutableStateOf(TextFieldValue()) }
            var textState4 by remember { mutableStateOf(TextFieldValue()) }

            TextField(
                value = textState,
                onValueChange = { textState = it },
                label = { Text(text = "Put your name here") },
                modifier = Modifier
                    .fillMaxWidth()
            )

            TextField(
                value = textState2,
                onValueChange = { textState2 = it },
                label = { Text(text = "Put your name here") },
                modifier = Modifier
                    .width(250.dp)
                    .wrapContentHeight(align = Alignment.CenterVertically)
                ,
                maxLines = 5
            )

            TextField(
                value = textState3,
                onValueChange = { textState3 = it },
                label = { Text(text = "Put your name here") },
                modifier = Modifier
                    .requiredWidth(315.dp)
                    .height(150.dp)
            )

            OutlinedTextField(
                value = textState4,
                onValueChange = { textState4 = it },
                label = { Text(text = "Put your name here") },
                modifier = Modifier
                    .fillMaxWidth(0.8F)
                    .height(100.dp)
            )
        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //MainContent()
    }
}