How to use material3 custom dialog in jetpack compose - android

I want to build Alert Dialog with options selection with rounded corners. I tried Material 3 library Alert Dialog code. I was looking this answer but getting weird errors. I have BOM version is compose_bom = "2022.11.00"
#Composable
fun DialogOptionsView() {
AlertDialog(
onDismissRequest = {}
) {
Surface(
modifier = Modifier
.wrapContentWidth()
.wrapContentHeight(),
shape = MaterialTheme.shapes.large
) {
}
}
}
Error I am getting
Type mismatch.
Required:
DialogProperties
Found:
() → Unit
Error Image

The BOM package contains only stable versions of the different Compose libraries.
In particular the BOM 2022.11.00 contains the androidx.compose.material3:material3:1.0.1
To use the content parameter with AlertDialog you have to use at least the version 1.1.0-alpha04 of M3.
In your build.gradle use:
dependencies {
// Import the Compose BOM
implementation platform('androidx.compose:compose-bom:2023.01.00')
// Override Material Design 3 library version with a pre-release version
implementation 'androidx.compose.material3:material3:1.1.0-alpha05'
//...
}
Also check the BOM to libraries mapping.

Related

Is it possible to call a donut routine within the MainActivity using different parameters?

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Row() {
donut(_size = 100.dp, _padding = 10.dp)
}
}
}
#Preview
#Composable
fun donut(_size: Dp = 120.dp, _padding: Dp = 5.dp) {
Box(modifier = Modifier
.size(_size)
.clip(CircleShape)
.background(Color.Green)
.padding(_padding)
.clip(CircleShape)
.background(Color.Red))
}
}
Unable to find #Preview 'com.pelicancolder.chapter1_03.MainActivity.donut'
The preview will display after rebuilding the project
Rebuilding the project revealed the same error message.
I am using compose 1.1.1 and Kotlin 1.6.10 so there appears to be no incompatibilities there.
build.gradle file:
buildscript {
ext {
compose_ui_version = '1.1.1'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.3.1' apply false
id 'com.android.library' version '7.3.1' apply false
id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
}
jvm_target is 1.8
About:
Android Studio Dolphin | 2021.3.1 Patch 1
Build #AI-213.7172.25.2113.9123335, built on September 29, 2022
Runtime version: 11.0.13+0-b1751.21-8125866 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 12
Registry:
external.system.auto.import.disabled=true
ide.text.editor.with.preview.show.floating.toolbar=false
The #Preview is not supposed to be applied like this and I see a couple of problems in your code.
a #Composable function is supposed to be a top level function, not within a class, so move your donut function outside MainActivity. Also for code structure and if you don't mind them being public to your whole project I would move the composables to a different file named with the same name as the primary composable within the file. If you would like though, you can keep it in the same file as the activity and make them private.
Composables naming convention wants their name to start with a capital letter unlike what we are used to with Kotlin or Java functions/methods.
Your composable is supposed to be used by your code as you do in your MainActivity but no code used by your code should have the #Preview annotation. Remove the #Preview annotation from your donut function.
Create another function to be your previewable composable to call your donut function like this (it can be private)
#Preview
#Composable
private fun DonutPreview() {
Donut()
}

Jetpack Compose update to 1.2.0 leads to ClassCastException: ComposableLambdaImpl to Function

I updated a project from Jetpack compose 1.0.0 to 1.2.1. Therefore I increased the version to 1.2.1 for following dependencies:
// ***** UI & Jetpack Compose *****
implementation("androidx.compose.ui:ui:${SharedVersions.jetpackCompose}")
// ViewBinding support to integrate xml views via AndroidView()
implementation("androidx.compose.ui:ui-viewbinding:${SharedVersions.jetpackCompose}")
// Tooling support (Previews, etc.)
implementation("androidx.compose.ui:ui-tooling:${SharedVersions.jetpackCompose}")
// Foundation (Border, Background, Box, Image, Scroll, shapes, animations, etc.)
implementation("androidx.compose.foundation:foundation:${SharedVersions.jetpackCompose}")
// Material Design
implementation("androidx.compose.material:material:${SharedVersions.jetpackCompose}")
// Integration with observables
implementation("androidx.compose.runtime:runtime-livedata:${SharedVersions.jetpackCompose}")
implementation("androidx.compose.runtime:runtime-rxjava2:${SharedVersions.jetpackCompose}")
and set
composeOptions {
kotlinCompilerExtensionVersion = "1.3.2"
}
Due to the dependencies/requirements of jetpack compose 1.2.1 I had to update following parts in my application to get it to compile:
compileSdk from 31 to 32
targetSdk from 31 to 32
gradle android plugin from 7.2.2 to 7.3.0
gradle 7.3.3 to 7.4.0
kotlin from 1.5.10 to 1.7.20
and minor code migrations to support update the new kotlin version.
After that the application compiles but it crashes on first start with:
ClassCastException: androidx.compose.runtime.internal.ComposableLambdaImpl cannot be cast to kotlin.jvm.functions.Function
The crash occurs when calling a function similiar to this:
fun <D> NavGraphBuilder.createAppNavGraphComposableDataAware(
route: String,
arguments: List<NamedNavArgument> = emptyList(),
deepLinks: List<NavDeepLink> = emptyList(),
content: #Composable (backStackEntry: NavBackStackEntry, data: D) -> Unit
) {
addDestination(Destination(provider, content as as #Composable (NavBackStackEntry, Any?) -> Unit)
}
Any clues what changed inside the jetpack compose libs or compiler? Does anyone else has problems similiar to this when using Composable Lambdas?

androidx.compose.ui.tooling.preview.PreviewActivity is not an activity subclass or alias

I updated my compose and kotlinCompilerExtensionVersion to 1.0.0-rc02 from beta09, Kotlin version to 1.5.21, Gradle version to 7.1 and Gradle plugin version to 7.1.0-alpha04
Ever since - on trying to run MyScreen - I'm getting this error:
androidx.compose.ui.tooling.preview.PreviewActivity is not an activity
subclass or alias
I am not able to resolve this. How do I resolve this?
I am using Compose 1.0, Kotlin 1.5.10 and Android Studio 2020.3.1, and I run into the same problem on most of my previews (not on others).
On the top bar menu, clicking on "Edit configurations" and manually creating a new configuration with the same parameters as those in the invalid configuration with warning (" androidx.compose.ui.tooling.PreviewActivity is not an Activity subclass or alias") did the trick for me.
I'm experiencing this error using Bumblebee (AKA, 2021.1.1) Canary 6, 7, and 8, using compose version 1.0.0, 1.0.1, and 1.1.0-alpha01. I've just raised a bug on the Studio issue tracker:
https://issuetracker.google.com/issues/196248459
To quote myself:
It seems clear this is a "left hand doesn't know what right hand is doing" thing, because PreviewActivity is not in that package, but rather androidx.compose.ui.tooling – it hasn't been in android.compose.ui.tooling.preview since compose 1.0.0-beta09.
UPDATE
I was able to get previews working again by clearing off all the preview run configurations. Here were my steps:
Click on the "run configurations" selector and select Edit Configurations...
Expand the Compose Preview grouping
Select all the preview configurations (you can select the first and shift-select the last)
Click the - button to remove all the collections
Click Ok
Run Invalidate Caches... in the file menu
After AS restarts and your cache repopulates, run a preview. It should work now.
Apparently AS was caching "junk" in the temporary run configurations it was creating every time I launched a preview. The "junk" was valid for earlier versions of AS, but broke later versions. Clearing out this cache got me working again.
I've got the same error with Compose 1.0.0 and Kotlin 1.5.10 (which is recommended).
I fixed these with adding the following dependence debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" in my gradle file.
Don't forget to set the version of the dependence to your current compose version.
After a rebuild and reload of the preview everything works fine for me.
The Preview of the example looks like this:
package com.example.compose.basics.ui
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Card
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import de.db.dbs.travio.compose.basics.themeing.ComposeBasicsTheme
#Preview
#Composable
fun ScreenContent() {
ComposeBasicsTheme {
Card(modifier = Modifier
.padding(8.dp)
.shadow(8.dp, RoundedCornerShape(32.dp))
) {
Text(
text = "Hello World!",
modifier = Modifier.padding(16.dp)
)
}
}
}
The thing that worked for me was downgrading the ui-tooling version to beta09, i.e using this dependency for ui-tooling:
implementation "androidx.compose.ui:ui-tooling:1.0.0-beta09"
Updating the compose_version to 1.0.1 , and kotlin-gradle-plugin to 1.5.21 fixes the issue if the versions used are lower than mentioned here.

java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion;

I'm using Jetpack Compose for the first time but I'm getting this error. I haven't figured out where the problem really is but I'm using single-activity architecture. If more information is needed, kindly inform me.
According to the error, the problem seems to be coming from the Scaffold.
val scaffoldState = rememberScaffoldState()
Scaffold(
scaffoldState = scaffoldState,
snackbarHost = {
SnackbarHost(hostState = it)
}
) {
java.lang.NoSuchFieldError: No field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion; in class Landroidx/compose/foundation/layout/BoxScope; or its superclasses (declaration of 'androidx.compose.foundation.layout.BoxScope' appears in /data/app/com.octagon_technologies.scafe-0B8-dDpbnRqa6fydxFPekw==/base.apk)
at androidx.compose.material.SurfaceKt$Surface$1.invoke(Surface.kt:149)
at androidx.compose.material.SurfaceKt$Surface$1.invoke(Surface.kt:105)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
at androidx.compose.material.SurfaceKt.Surface-F-jzlyU(Surface.kt:102)
at androidx.compose.material.ScaffoldKt$Scaffold$child$1.invoke(Scaffold.kt:168)
at androidx.compose.material.ScaffoldKt$Scaffold$child$1.invoke(Scaffold.kt:167)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:118)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.material.ScaffoldKt.Scaffold-J67Y1T8(Scaffold.kt:197)
at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1$1.invoke(MainActivity.kt:54)
at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1$1.invoke(MainActivity.kt:47)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
at androidx.compose.material.TextKt.ProvideTextStyle(Text.kt:246)
at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:78)
at androidx.compose.material.MaterialThemeKt$MaterialTheme$1.invoke(MaterialTheme.kt:77)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.runtime.CompositionLocalKt.CompositionLocalProvider(CompositionLocal.kt:193)
at androidx.compose.material.MaterialThemeKt.MaterialTheme(MaterialTheme.kt:69)
at com.octagon_technologies.scafe.presentation.ui.theme.ThemeKt.ScafeTheme(Theme.kt:46)
at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1.invoke(MainActivity.kt:47)
at com.octagon_technologies.scafe.presentation.MainActivity$onCreate$1.invoke(MainActivity.kt:46)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
at androidx.compose.ui.platform.ComposeView.Content(ComposeView.android.kt:346)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:202)
at androidx.compose.ui.platform.AbstractComposeView$ensureCompositionCreated$1.invoke(ComposeView.android.kt:201)
at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:109)
I had the same error and solved it by updating the compose version. The new project template generated by Android Studio comes with compose version 1.0.0-beta01
Open build.gradle file and update
compose_version = '1.0.0-beta05'
or newer if you prefer.
I got this error after adding androidx.navigation:navigation-compose:1.0.0-alpha10 as a dependency.
What worked for me was to downgrade to androidx.navigation:navigation-compose:1.0.0-alpha09.
I got the same error for this code
java.lang.NoSuchFieldError:
No static field Companion of type Landroidx/compose/foundation/layout/BoxScope$Companion;
in class Landroidx/compose/foundation/layout/BoxScope;
or its superclasses (declaration of 'androidx.compose.foundation.layout.BoxScope'
appears in /data/app/ink.iamt.demo-l1DoSMoQUFGcF5KYI5RU0w==/base.apk)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Card {
Text("Hello, Compose")
}
}
}
}
You know my code cannot be simpler. So I thought it should not be my fault.
I checked my Android Studio version, it's 2020.3.1 Canary 14, which is the latest version. And checked the build.gradle for Jetpack compose, it's
val compose_version by extra("1.0.0-beta01")
So the latest Android Studio creates a new Compose project with 1.0.0-beta01 compose version, while the latest compose version is 1.0.0-beta04. Then I try to update the version to beta04 and rerun my App. Then it works!
So please try to update your compose version to the latest and see if you have the luck.
This answer is based on CommonsWare comment which helped me fix the error.
The solution was to use a consistent set of Compose dependencies. beta04 made a change to BoxScope in the compose-foundation artifact. Perhaps you are getting beta04 of that artifact but are getting an older compose-material artifact.

How to use AndroidAnnotation #SharedPref with Kotlin

I'm trying to use AndroidAnnotations #SharefPref within kotlin, but Iget following error
org.androidannotations.annotations.sharedpreferences.Pref can only be used on an element that extends org.androidannotations.api.sharedpreferences.SharedPreferencesHelper
What am I doing wrong?
//Interface
#SharedPref(SharedPref.Scope.APPLICATION_DEFAULT)
open interface MyPreferences {
#DefaultInt(-1)
fun someIntValue():Int
}
//Fragment
#Pref
lateinit open var sharedPref:CongressPreferences_
//usage within fragment
val get: Int = sharedPref.selectedEventId().get()
This is due to a bug in the Kotlin annotation processor.
To fix this, you must add correctErrorTypes = true to your kapt block.
kapt {
correctErrorTypes = true
}
Also make sure you are using the latest Kotlin version (as of this moment: 1.1.3).
I just wanna extend on #WonderCsabo 's answer.
His answer almost saved me, but not fully.
After adding this to my app label build gradle.
kapt {
correctErrorTypes = true
}
I wasn't able to run my app.
Then I closed my android studio and then run Android studio again as administrator.
Voila! it works like charm.
Thank you #WonderCsabo

Categories

Resources