Textfield that changes readOnly value - android-jetpack-compose

I'm making a TextField with ExposedDropDownMenu for selecting a Country.
When i select something i use mutable state variable 'isReadOnly' for setting the readOnly parameter of TextField composable.
So when I select a country, i want it to be readOnly, but when I click on dismiss icon, I want it to not be readOnly.
I works very well, except one part, when I click on dismiss icon, I can type to my TextField, but software keyboard is not showing up. Do you know the solution to this problem?
val selectedFromController = selected.value
var selectedItem by remember {
mutableStateOf(
selectedFromController.ifEmpty { "" }
)
}
var isReadOnly by remember { mutableStateOf(false) }
ExposedDropdownMenuBox(
expanded = expanded.value,
onExpandedChange = { expanded.value = !expanded.value }
) {
TextField(
value = selectedItem,
onValueChange = {
selectedItem = it
expanded.value = true
},
readOnly = isReadOnly,
modifier = Modifier
.menuAnchor()
.fillMaxWidth(),
label = { Text(stringResource(id = label)) },
trailingIcon = { IconButton(
onClick = {
selectedItem = ""
selected.value = ""
savedId.value = -1
isReadOnly = false
keyboardController.show()
},
content = {
Icon(Icons.Filled.Clear, null)
}
) },
colors = ExposedDropdownMenuDefaults.textFieldColors()
)
val filteringOptions =
menuItems.filter { it.contains(selectedItem, ignoreCase = true) }
if (filteringOptions.isNotEmpty()) {
ExposedDropdownMenu(
expanded = expanded.value,
onDismissRequest = {
expanded.value = false
}
) {
filteringOptions.take(5).map {
DropdownMenuItem(
text = { Text(text = it) },
onClick = {
selectedItem = it
selected.value = it
expanded.value = false
isReadOnly = true
},
contentPadding = ExposedDropdownMenuDefaults.ItemContentPadding
)
}
}
}
}
I tried to bring it up with keyboardController, but it didn't help.

Related

Loading progress bar does not appear when background task runs

I want to show a circular progress bar while the shopping list items are being retrieved from the database. I have a Lazycolumn that displays the retrieved shopping list items, but the circular progress bar is never displayed, and the message "You don't have any items in this shopping list." is displayed briefly before the list is shown. This behavior is not desired. In the viewmodel, placing loading.value = false after the database call in the viewModelScope coroutine does not work. How can I fix this?
ShoppingListScreen Composable
fun ShoppingListScreen(
navController: NavHostController,
shoppingListScreenViewModel: ShoppingListScreenViewModel,
sharedViewModel: SharedViewModel
) {
val scope = rememberCoroutineScope()
val focusManager = LocalFocusManager.current
val screenHeight = LocalConfiguration.current.screenHeightDp.dp
val allItems = shoppingListScreenViewModel.shoppingListItemsState.value?.collectAsLazyPagingItems()
val showProgressBar = shoppingListScreenViewModel.loading.value
Scaffold(
topBar = {
CustomAppBar(
title = "Shopping List Screen",
titleFontSize = 20.sp,
appBarElevation = 4.dp,
navController = navController
)
},
floatingActionButton = {
FloatingActionButton(
onClick = {
shoppingListScreenViewModel.setStateValue(SHOW_ADD_ITEM_DIALOG_STR, true)
},
backgroundColor = Color.Blue,
contentColor = Color.White
) {
Icon(Icons.Filled.Add, "")
}
},
backgroundColor = Color.White,
// Defaults to false
isFloatingActionButtonDocked = false,
bottomBar = { BottomNavigationBar(navController = navController) }
) {
Box {
LazyColumn(
modifier = Modifier
.fillMaxWidth()
.height(screenHeight)
) {
if (allItems?.itemCount == 0) {
item { Text("You don't have any items in this shopping list.") }
}
items(
items = allItems!!,
key = { item ->
item.id
}
) { item ->
ShoppingListScreenItem(
navController = navController,
item = item,
sharedViewModel = sharedViewModel
) { isChecked ->
scope.launch {
shoppingListScreenViewModel.changeItemChecked(item!!, isChecked)
}
}
}
item { Spacer(modifier = Modifier.padding(screenHeight - (screenHeight - 70.dp))) }
}
ConditionalCircularProgressBar(isDisplayed = showProgressBar)
}
}
}
ShoppingListScreenViewModel
#HiltViewModel
class ShoppingListScreenViewModel #Inject constructor(
private val getAllShoppingListItemsUseCase: GetAllShoppingListItemsUseCase
) {
private val _shoppingListItemsState = mutableStateOf<Flow<PagingData<ShoppingListItem>>?>(null)
val shoppingListItemsState: State<Flow<PagingData<ShoppingListItem>>?> get() = _shoppingListItemsState
val loading = mutableStateOf(false)
init {
loading.value = true
getAllShoppingListItemsFromDb()
}
private fun getAllShoppingListItemsFromDb() {
viewModelScope.launch {
_shoppingListItemsState.value = getAllShoppingListItemsUseCase().distinctUntilChanged()
loading.value = false
}
}
}
put the CircularProgressIndicator inside a Surface or Box, maybe you are using Scaffold with navigation, in my case I was using padding top 60.dp in my circular progrees then it was kind of hinding.

when opening DropdownMenu, how can I cancel keyboard auto-close behavior

when I open DropdownMenu, my keyboard auto-close. This behavior is not my expect.
I expect my keyboard to remain open when I open the DropdownMenu.
current behavior:
want behavior:
Thanks to my friends in the compose community for helping me with this on slack.
Just set the properties.focusable property to false
var content by remember { mutableStateOf("") }
var deadLineMenuExpanded by remember { mutableStateOf(false) }
Column(modifier = Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) {
TextField(value = content, onValueChange = {content = it})
Box() {
TextButton(onClick = { deadLineMenuExpanded = true }) {
Text("open Menu")
}
DropdownMenu(
expanded = deadLineMenuExpanded,
onDismissRequest = { deadLineMenuExpanded = false },
properties = PopupProperties(focusable = false)
) {
DropdownMenuItem(onClick = {
deadLineMenuExpanded = false
}) {
androidx.compose.material.Text("today")
}
}
}
}

Keyboard does not hide, when focused TextField leaves the LazyColumn

Perhaps this is the normal behaviour, but i wish it was different. I had tried to google the solution, but did not find anything suitable (or merely missed it).
Sample code (for simplicity i hold mutable states right here, not using ViewModel):
#Composable
fun Greeting() {
Scaffold(topBar = {
TopAppBar(title = { Text(text = "Some title") })
}) {
val focusManager = LocalFocusManager.current
LazyColumn(
contentPadding = PaddingValues(all = 16.dp),
verticalArrangement = Arrangement.spacedBy(space = 16.dp)
) {
items(count = 20) { index ->
val (value, onValueChange) = rememberSaveable { mutableStateOf("Some value $index") }
TextField(
value = value,
onValueChange = onValueChange,
modifier = Modifier.fillMaxWidth(),
label = { Text(text = "Some label $index") },
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
keyboardActions = KeyboardActions(onNext = {
if (!focusManager.moveFocus(FocusDirection.Down))
focusManager.clearFocus()
}),
singleLine = true
)
}
}
}
}
Compose version 1.0.5
You could try just hiding the keyboard whenever scrolling occurs. This is okay as long as you don't have a large set of items. But since you're using TextFields, it isn't likely that you'll have such a large number. This sample illustrates hiding the keyboard when scrolling occurs:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(intent)
setContent {
Greeting(this)
}
}
}
#Composable
fun Greeting(activity: Activity) {
Scaffold(topBar = {
TopAppBar(title = { Text(text = "Some title") })
}) {
val lazyListState = rememberLazyListState()
val ctx = LocalContext.current
LaunchedEffect(lazyListState.firstVisibleItemIndex) {
val inputMethodManager = ctx.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity.window?.decorView?.windowToken, 0)
}
LazyColumn(
state = lazyListState,
contentPadding = PaddingValues(all = 16.dp),
verticalArrangement = Arrangement.spacedBy(space = 16.dp)
) {
items(
count = 20,
key = { index ->
// Return a stable + unique key for the item
index
}
) { index ->
val (value, onValueChange) = rememberSaveable { mutableStateOf("Some value $index") }
TextField(
value = value,
onValueChange = onValueChange,
modifier = Modifier
.fillMaxWidth(),
label = { Text(text = "Some label $index") },
singleLine = true
)
}
}
}
}

Cannot Type into OutlinedTextField when dropdownmenu opens below it

I am trying to make autocomplete feature by using OutlinedTextField, DropdownMenu.
When I programmatically open the DropdownMenu, I can then no longer type into the OutlinedTextField.
var expanded by remember { mutableStateOf(false) }
val suggestions = remember { mutableStateListOf ("karan", "karanx", "karany") }
var selectedText by remember { mutableStateOf("") }
var dropDownWidth by remember { mutableStateOf(0) }
val focusManager = LocalFocusManager.current
Box() {
OutlinedTextField(
value = selectedText,
onValueChange = {
selectedText = it
expanded = true
},
modifier = Modifier.fillMaxWidth()
.onSizeChanged {
dropDownWidth = it.width
} .onFocusChanged {
expanded = it.isFocused
},
//why cant i do expanded = true on cliackable modifier of outlinetextfield
label = { Text("Label") }
)
DropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
focusManager.clearFocus()
},
modifier = Modifier
.width(with(LocalDensity.current) { dropDownWidth.toDp() }).border(width = 2.dp,color = Color.Blue)
) {
suggestions.forEach { label ->
DropdownMenuItem(onClick = {
selectedText = label
expanded = false
focusManager.clearFocus()
}) {
Text(text = label)
}
}
}
}
This got solved by adding focusable = false to PopupProperties for the DropdownMenu like shown below:
DropdownMenu(
expanded = suggestions.isNotEmpty(),
onDismissRequest = { suggestions.removeAll{true} },
properties = PopupProperties(focusable = false),
modifier = Modifier.fillMaxWidth()
)
Full working code for autocomplete with Jetpack Compose Below :
var expanded by remember { mutableStateOf(false) }
var masterSuggestions = listOf ("karan", "priya", "vihaan")
var suggestions = remember { mutableStateListOf <String>() }
var selectedText by remember { mutableStateOf("") }
var dropDownWidth by remember { mutableStateOf(0) }
Column() {
OutlinedTextField(
value = selectedText,
onValueChange = {
println ("inside value change")
suggestions.removeAll{true}
selectedText = it
for (name in masterSuggestions)
{
if (name.startsWith(selectedText)) {
suggestions.add(name)
}
}
},
modifier = Modifier.fillMaxWidth(),
label = {Text ("label")}
)
DropdownMenu(
expanded = suggestions.isNotEmpty(),
onDismissRequest = { suggestions.removeAll{true} },
properties = PopupProperties(focusable = false),
modifier = Modifier.fillMaxWidth()
) {
suggestions.forEach { label ->
DropdownMenuItem(onClick = {
selectedText = label
}) {
Text(text = label)
}
}
}
}

android compose bottomnavigation item is hide

If I add floating action button to the bottom bar in bottomnavigation as extended, the menu will be covered by location.
If I'm set up in the center, you can see both menus.
But if I leave it as end, you can only see one menu.
When I set it to end, I want to see both menus.
Here is my code
Scaffold(
topBar = {
if (menu.name != null) {
TopAppBarCompose(title = menu.name)
}
},
floatingActionButton = {
FloatingActionButton(
onClick = {
val route = Screen.BarcodeScan.route
onNavigateToBarcodeScreen(route)
},
shape = RoundedCornerShape(50),
backgroundColor = MaterialTheme.colors.primary
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Center
) {
Icon(
imageVector = Icons.Default.QrCodeScanner,
contentDescription = "BarcodeScan",
modifier = Modifier.padding(start = 30.dp, top = 20.dp, bottom = 20.dp, end = 5.dp)
)
Text(
text = "Scan",
fontSize = 24.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier.padding(start = 5.dp, top = 20.dp, bottom = 20.dp, end = 30.dp)
)
}
}
},
isFloatingActionButtonDocked = true,
floatingActionButtonPosition = FabPosition.End,
bottomBar = {
BottomAppBar(
cutoutShape = RoundedCornerShape(50),
content = {
BottomNavigation {
BottomNavigationItem(
selected = selectedItem.value == "send",
onClick = {
content.value = "Send Screen"
selectedItem.value = "send"
},
icon = {
Icon(Icons.Filled.SwapVert, contentDescription = "send")
},
label = { Text(text = "send") },
alwaysShowLabel = false
)
BottomNavigationItem(
selected = selectedItem.value == "input",
onClick = {
content.value = "input Screen"
selectedItem.value = "input"
},
icon = {
Icon(Icons.Filled.Create, contentDescription = "input")
},
label = { Text(text = "Input") },
alwaysShowLabel = false
)
}
}
)
}
)
When I set in center, I can see two menu
But I set extened fab button to end, It's going to be as follows.
I want to set the place of the extended fab button to end and see two menus. Is there a way to change the place?
ps.The image was simply uploaded to help understand.
I think all you need is to just offset the FAB using the offset modifier. Here's an example:
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomEnd) {
var selectedItem by remember { mutableStateOf(0) }
val items = listOf("Songs", "Artists", "Playlists")
BottomNavigation {
items.forEachIndexed { index, item ->
BottomNavigationItem(
icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
label = { Text(item) },
selected = selectedItem == index,
onClick = { selectedItem = index }
)
}
}
ExtendedFloatingActionButton(
modifier = Modifier.offset(x = -10.dp, y = -70.dp),
icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
text = { Text("ADD TO BASKET") },
onClick = { /*do something*/ }
)
}
FAB inside a scaffold
val scaffoldState = rememberScaffoldState()
val scope = rememberCoroutineScope()
var selectedItem by remember { mutableStateOf(0) }
val items = listOf("Songs", "Artists", "Playlists")
Scaffold(
scaffoldState = scaffoldState,
floatingActionButtonPosition = FabPosition.End,
floatingActionButton = {
ExtendedFloatingActionButton(
modifier = Modifier.offset(x = -10.dp, y = -70.dp),
icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
text = { Text("ADD TO BASKET") },
onClick = { /*do something*/ }
)
},
content = { innerPadding ->
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomStart) {
BottomNavigation {
items.forEachIndexed { index, item ->
BottomNavigationItem(
icon = { Icon(Icons.Filled.Favorite, contentDescription = null) },
label = { Text(item) },
selected = selectedItem == index,
onClick = { selectedItem = index }
)
}
}
}
}
)

Resources