Last active
August 30, 2023 14:02
-
-
Save kozaxinan/77dfbc9a0e1adf729e16a984d5af0da5 to your computer and use it in GitHub Desktop.
A Jetpack compose implementation for displaying counter for hidden word becuase of text overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package foo | |
import androidx.compose.foundation.BorderStroke | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.Row | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.layout.size | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.material.Card | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.getValue | |
import androidx.compose.runtime.mutableStateOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.setValue | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.text.style.TextOverflow | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.unit.dp | |
@Composable | |
private fun LocationLabelArea(locations: String) { | |
Row( | |
modifier = Modifier | |
.height(48.dp) | |
.padding(horizontal = 16.dp), | |
verticalAlignment = Alignment.CenterVertically | |
) { | |
var additional by remember { mutableStateOf(10) } | |
Box( | |
modifier = Modifier.weight(1f) | |
) { | |
Text( | |
text = locations, | |
modifier = Modifier.fillMaxWidth(), | |
overflow = TextOverflow.Ellipsis, | |
maxLines = 1, | |
onTextLayout = { textLayoutResult -> | |
if (textLayoutResult.hasVisualOverflow) { | |
val lineEndIndex = textLayoutResult.getLineEnd( | |
lineIndex = 0, | |
visibleEnd = true | |
) | |
additional = locations | |
.substring(lineEndIndex) | |
.count { it == ',' } | |
} | |
}, | |
) | |
} | |
if (additional != 0) { | |
CounterBadge(additionalLocationsCount = additional) | |
} | |
} | |
} | |
@Composable | |
private fun CounterBadge(additionalLocationsCount: Int) { | |
Box( | |
modifier = Modifier | |
.padding(start = 8.dp) | |
.size(32.dp) | |
.clip(RoundedCornerShape(100)) | |
.background(MaterialTheme.colors.onBackground.copy(alpha = 0.3f)) | |
) { | |
Text( | |
text = "+$additionalLocationsCount", | |
overflow = TextOverflow.Visible, | |
maxLines = 1, | |
modifier = Modifier | |
.align(Alignment.Center), | |
) | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
private fun Preview() { | |
MaterialTheme { | |
Card( | |
Modifier.padding(16.dp), | |
border = BorderStroke(1.dp, MaterialTheme.colors.onSurface), | |
) { | |
LocationLabelArea( | |
locations = "Some location, Some, Some location, Some location, Some location" | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment