android - Circular progressbar as trailing icon - Stack Overflow

I have a OutlinedTextField with a trailing icon which changes according to a state.Let's say, idl

I have a OutlinedTextField with a trailing icon which changes according to a state. Let's say, idling state in the beginning and then when trailing icon is pressed goes to loading and when the task is , state success, icon is a cross/cancel which cleans the text field. What I want is to have a progressbar like an icon when the state is loading. I currently use a CircularProgressbar composable but it does not look/feel like an icon.

Question

Is there a way to make the CircularProgressbar look like an icon in a generic way which works for any phones and sizes? Or is there an easier way to do it?

Animating some icon could also be interesting.

Here is what I have done:

trailingIcon = {
                when(status) {
                    Status.IDLING -> IconButton(
                        onClick = {
                            viewModel.filterData(iNeedInput)
                        }
                    ) {
                        Icon(imageVector = Icons.Outlined.Search, contentDescription = "search")
                    }
                    Status.SUCCESS -> IconButton(
                        onClick = {
                            viewModel.updateSearchUi("")
                        }
                    ) {
                        Icon(imageVector = Icons.Outlined.Clear, contentDescription = "clear")
                    }
                    Status.LOADING ->
                        IconButton(
                            onClick = {
                                viewModel.updateSearchUi("")
                            }
                        ) {
                            CircularProgressIndicator()
                        }
                    Status.FAILED ->  Icon(imageVector = Icons.Outlined.Info, contentDescription = "error")
                }
            }

I have a OutlinedTextField with a trailing icon which changes according to a state. Let's say, idling state in the beginning and then when trailing icon is pressed goes to loading and when the task is , state success, icon is a cross/cancel which cleans the text field. What I want is to have a progressbar like an icon when the state is loading. I currently use a CircularProgressbar composable but it does not look/feel like an icon.

Question

Is there a way to make the CircularProgressbar look like an icon in a generic way which works for any phones and sizes? Or is there an easier way to do it?

Animating some icon could also be interesting.

Here is what I have done:

trailingIcon = {
                when(status) {
                    Status.IDLING -> IconButton(
                        onClick = {
                            viewModel.filterData(iNeedInput)
                        }
                    ) {
                        Icon(imageVector = Icons.Outlined.Search, contentDescription = "search")
                    }
                    Status.SUCCESS -> IconButton(
                        onClick = {
                            viewModel.updateSearchUi("")
                        }
                    ) {
                        Icon(imageVector = Icons.Outlined.Clear, contentDescription = "clear")
                    }
                    Status.LOADING ->
                        IconButton(
                            onClick = {
                                viewModel.updateSearchUi("")
                            }
                        ) {
                            CircularProgressIndicator()
                        }
                    Status.FAILED ->  Icon(imageVector = Icons.Outlined.Info, contentDescription = "error")
                }
            }
Share Improve this question asked Nov 20, 2024 at 15:27 msc87msc87 1,0394 gold badges19 silver badges44 bronze badges 2
  • One thing I do not understand is that why have you wrapped the CircularProgressIndicator() inside an IconButton? You could have just used CircularProgressIndicator(). – Ahsan Zia Commented Nov 20, 2024 at 15:38
  • The reason was to make sure that the paddings and sizes are not affected by the IconButton composable. I removed it and it still looks the same. – msc87 Commented Nov 21, 2024 at 9:04
Add a comment  | 

1 Answer 1

Reset to default 1

Icon uses LocalContentColor to draw, so you can use it too to set the color of the CircularProgressIndicator. Trailing icon size is 24dp by specs, you can also add padding(2.dp) to make it match the size of the other icons.

CircularProgressIndicator(
    color = LocalContentColor.current,
    strokeWidth = 2.dp,
    modifier = Modifier.size(24.dp),
)

Default:

Error:

Disabled:

Dark:

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742349257a4427178.html

相关推荐

  • android - Circular progressbar as trailing icon - Stack Overflow

    I have a OutlinedTextField with a trailing icon which changes according to a state.Let's say, idl

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信