0

I am trying to show an CircularProgressIndicator in my login screen. When I enter username and password and click the login button I need to show a CircularProgressIndicator at the center of screen with the login screen still visible. I am able to show CircularProgressIndicator but it's covering the entire screen with white background. I want it to have a transparent background.

CircularProgressIndicator(
    modifier = Modifier
        .fillMaxSize()
        .wrapContentSize(Alignment.Center)
)
2
  • The CircularProgressIndicator's background color already seems to be transparent. Are you sure the white background isn't coming from something else? With the code provided I cannot reproduce the problem.
    – Leviathan
    Commented Jul 10 at 6:53
  • 1
    @ser7029573 It probably just seems that the CircularProgressindicator has a white background, but instead, your CircularProgressindicator is pushing the contents of your login page off the screen. See my answer below. If you found it useful, please mark it as accepted answer by clicking the checkmark at the left side of my answer.
    – BenjyTec
    Commented Jul 10 at 14:44

2 Answers 2

1

It seems that the CircularProgressIndicator has a white background because it is pushing away the content instead of displaying above it.

To show a CircularProgressIndicator above the LoginScreen, you can use Box Composables. Please try out the following code:

@Composable
fun LoadingScreen() {
    Box(
        modifier = Modifier.fillMaxWidth(),
        contentAlignment = Alignment.Center,
    ) {

        var isLoading by remember {
            mutableStateOf(true)
        }

        Column(
            verticalArrangement = Arrangement.spacedBy(12.dp)
        ) {
            TextField(value = "Username", onValueChange = {})
            TextField(value = "Password", onValueChange = {})
            Button(onClick = { /*TODO*/ }) {
                Text(text = "Login")
            }
        }
        if (isLoading) {
            // This Box Composable makes a light overlay, it is optional
            Box(
                modifier = Modifier.matchParentSize().background(Color.Gray.copy(alpha = 0.5f))
            )
            CircularProgressIndicator()
        }
    }
}

Output:

enter image description here

0

Create a new Composable ProgressOverlay like this:

@Composable
fun ProgressOverlay() {
    Box(
        modifier = Modifier
            .fillMaxSize()
            .clickable {  }
            .background(MaterialTheme.colorScheme.background.copy(alpha = 0.5f)),
        contentAlignment = Alignment.Center
    ) {
        CircularProgressIndicator()
    }
}

Then in you screen:

@Composable
fun MyLoginScreen() {
    Surface(
        modifier = Modifier
            .fillMaxSize()
    ) {
        //MyLoginScreenContent()
        
        ProgressOverlay()
    }
}

Note: The ProgressOverlay should be added at the end of the page content to always show above the other components.

I have also added an empty clickable in the ProgressOverlay so that the user can't interact with the rest of the UI while the circular progress indicator is shown.

3
  • This seems overly contrived to address a problem that doesn't even seem to exist in the first place. What you do is explicitly define a background color where it already was transparent, only to make it half-transparent afterwards. Why not just remove .background(MaterialTheme.colorScheme.background.copy(alpha = 0.5f)) entirely?
    – Leviathan
    Commented Jul 10 at 7:20
  • If i add at the end of my login page its showing at the bottom of the page. I need CircularProgressIndicator at the center of screen above the login page. Commented Jul 10 at 8:48
  • The ProgressOverlay composable should not go inside a column or row. Instead, placing it directly under the Surface or a Box would be best. Just make sure it shouldn't be in a Column or a row scope. Commented Jul 11 at 11:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.