Good Morning, I'm developing a new Wear OS application. The thing is, I can't find anywhere how to make a good pagination.
I would like to change activity on swipe (no matter if it's vertical or horizontal) but can't find anywhere how to do it...
Maybe I should not navigate in my app like that ?
Can you help me if you have any answer on how to do it ?
thanks ! (for some reasons I can't say hello on my post ?)
You can use HorizontalPager or VerticalPager from Accompanist.
https://google.github.io/accompanist/pager/
HorizontalPager(
modifier = Modifier.fillMaxSize(),
count = 10,
state = state
) { page ->
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text(text = "Screen $page")
}
}
An example https://github.com/google/horologist/blob/e2741cef87774b18d58bb1b0f78bd5b60901f20d/sample/src/main/java/com/google/android/horologist/scratch/ScratchActivity.kt#L79
If you want something more custom, then it's probably a serious investment in a component. Especially if it needs to work with the system Swipe to Dismiss.
Related
i want to share image of my component , but cannot accses view of it, to share.
how i can accses view of my component or share image of it.
my component is a card
#Composable
fun CardItem(modifier: Modifier=Modifier,card: Card, onClick:()->Unit) {
Box(
modifier
.background(
if (card.card_number == AllCardsActivity.NOCARD) MaterialTheme.colorScheme.secondary
else MaterialTheme.colorScheme.primary,
RoundedCornerShape(20.dp)
)
.width(CardWidth)
) {
Column(Modifier.padding(25.dp)) {....
i want to share image of my component
If you mean that you want to capture a Bitmap of a composable for the purposes of sharing it, there are at least three libraries for this:
https://github.com/PatilShreyas/Capturable
https://github.com/KaustubhPatange/kapture
https://github.com/SmartToolFactory/Compose-Screenshot
And all three are open source, so if you do not like them for some reason, you can examine their implementations, contribute back changes, etc.
I have already viewed other posts on the site. They suggest using the focusRequestor modifier and I've tried that.
val scope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
Text(
modifier = Modifier
.focusable()
.focusTarget() //Tried Adding/Removing
.onFocusChanged {
if (it.hasFocus || it.isFocused) {
Log.i("Test", "Text Focused")
}
} // Tried Adding Visual Elements to delete it as well.
.focusRequester(focusRequester),
text = "MARSK"
)
LaunchedEffect(scope) {
focusRequester.requestFocus()
}
I'm taking Text in this example, but actually I need to implement this on Canvas. This approach is not working on Box too. I'm afraid the same is the case for other containers as well.
For background, this is being built for my TV, so I wish to make this element clickable by the OK button on the remote's D-Pad. My approach is to make it focused first, then assuming that once it is focused, it will automatically detect the press of the OK button as the 'click'. If there is something wrong with my approach, feedback for improvements is also welcome.
PS: The solution by Abhimanyu works like a charm on Mobile Devices. However, since I mentioned a TV above, a TV is the device in consideration. On the TV, I have to press a button (Any button on the DPAD works, even the OK button, strangely) to get it focused up. Any idea how to get round this issue?
Thanks
Issue
The onFocusChanged should be added BEFORE the focusTarget or focusable that is being observed.
Source
Changes
Remove focusTarget and move onFocusChanged before focusable.
Also, note that focusRequester must be before focusable.
This would hopefully work for all comopsables. I have tested using Text and the example from Docs was using Box.
Extra details.
Prefer focusable over focusTarget.
From focusTarget docs,
Note: This is a low level modifier. Before using this consider using Modifier.focusable(). It uses a focusTarget in its implementation. Modifier.focusable() adds semantics that are needed for accessibility.
Order of Modifiers matter
Layouts in Jetpack Compose Codelab
Check Order Matters section
order matters when chaining modifiers as they're applied to the composable they modify from earlier to later,
Sample code
#Composable
fun FocusableText() {
val scope = rememberCoroutineScope()
val focusRequester = remember { FocusRequester() }
var color by remember { mutableStateOf(White) }
LaunchedEffect(scope) {
focusRequester.requestFocus()
}
Text(
modifier = Modifier
.background(color)
.onFocusChanged {
color = if (it.hasFocus || it.isFocused) {
LightGray
} else {
White
}
}
.focusRequester(focusRequester)
.focusable(),
text = "Test Text"
)
}
The Text background is LightGray which indicates the text is focused.
world!
I've been playing with Jetpack Compose for a while, and I've had really good results.
However, I found this problem: I have a list of items, and I use a LazyColumn to show them to the user. But when I scroll up and down, some items are not displayed correctly and they show up like there is no information in them.
I don't know if I'm missing something or this behavior will be corrected in the future.
How it looks
My code:
LazyColumn(
modifier = modifier
.fillMaxHeight(),
) {
items(expenses, Expense::id) { expense ->
ExpenseItem(expense = expense, areExpensesSelectable,
onExpenseLongClick = { onExpenseLongClick?.invoke() },
onExpenseSelected = { onExpenseSelected?.invoke(it) },
onExpenseDeselected = { onExpenseDeselected?.invoke(it) })
}
}
Self response: this was a bug present in Jetpack Compose until fixed in version beta08.
I hear this bug is fixed in the latest version of Compose.
I am trying to have a way to show a BottomSheet from everywhere within my app, for that I use the BottomSheetScaffold and a LiveData object that holds the current composable function which is observed as state:
val sheetContent by MusicHub.state.bottomSheet.getContent().observeAsState()
BottomSheetScaffold(
modifier = Modifier
.fillMaxSize(),
scaffoldState = bottomSheetScaffoldState,
sheetPeekHeight = 0.dp,
sheetContent = sheetContent!!
)
I use the BottomSheet as a context menu in my app. For example when i longClick on a playlist in my app it sets the content of the BottomSheet and shows it like this:
PlaylistItem(
// ...
onLongClick = {
// Set the LiveData composable
MusicHub.state.bottomSheet.setContent {
PlaylistContextMenuTest(playlist!!, viewModel)
}
// Expand BottomSheet
scope.launch {
MusicHub.state.bottomSheet.expand()
}
}
)
In general this works but the first time the BottomSheet gets expanded it shows for a split second before it disappears at the bottom again. Here is a small GIF:
My guess is that somehow the size of the BottomSheet is not recalculated yet and hence it only works in the next recomposition. Coming from web dev i would say its a typical case of requestAnimationFrame but i don't quite know how to solve this issue in compose.
Edit:
PlaylistContextMenuTest code:
#Composable
fun PlaylistContextMenuTest(playlist: Playlist, viewModel: LibraryViewModel = activityViewModel()){
val scrollState = rememberScrollState()
Column(
modifier = Modifier
.fillMaxWidth()
.navigationBarsPadding()
// TODO: Replace that with a percentage of the screen height
.heightIn(max = 384.dp)
.verticalScroll(scrollState),
content = {
ContextMenu {
repeat(4){
addOption(R.drawable.ic_delete_black_24dp, "Delete"){
Timber.d("Delete Playlist")
viewModel.deletePlaylist(playlist)
}
}
}
}
)
}
Full ContextMenu source: (https://pastebin.com/sg4ed96L)
You seem to be right. The first time it seems to be re-calculating the BottomSheet's height.
As this does not change on the second try, it will then be displayed.
Does this answer help you?
It seems to work out quite okay for BottomSheetScaffold, but not for ModalBottomSheetLayout (the thing I was looking for as you can see in the comments of the linked answer).
Edit: Accompanist's Navigation Material is also worth a look.
This issue has been resolved with a new release of jetpack compose. I am not sure if it was beta 8 or 9 but everything works as intended now.
I am using android compose jetpack version "0.1.0-dev14". According to application requirement, I want to auto focus TextField once screen is visible. and another scenario is like I want to focus next TextField in same screen on next ImeAction.Next action. I could not find any method or solution for this problem. If Anyone can help me to sole this problem, It will be really appreciated.
Thank you..!!
With 1.0.0 to achieve autofocus you can use:
DisposableEffect(Unit) {
focusRequester.requestFocus()
onDispose { }
}
In this way the system grants focus to the component associated with this FocusRequester.
For example:
val focusRequester = FocusRequester()
TextField(
//...
modifier = Modifier
// add focusRequester modifier
.focusRequester(focusRequester)
)
This sample shows how to request focus on the next view.
To achieve autofocus on first view on the screen, I believe this can be used:
// ... composable context...
onActive(callback = {
focusModifiers.first().requestFocus()
})
// ... composable context...