Jetpack Compose PreviewActivity is not an activity subclass or alias - android

As I am exploring JetPack Compose and Tooling for it, I came across the wonderful PreView feature of it. Although I can see the preview in Split and Design window, when tried to deploy it in an emulator, I found this error.
androidx.compose.ui.tooling.preview.PreviewActivity is not an activity
subclass or alias
I am using
Android Studio Arctic Fox| 2020.3.1 RC1
activityComposeVersion = "1.3.0-rc01"
composeVersion = "1.0.0-rc02"
kotlinVersion = "1.5.10"
I was trying to deploy preview of GradientTintedIconButtonPreview()
#Preview
#Composable
fun SquareComposablePreview() {
Text("Hello World")
}
#Preview
#Composable
fun ComposablePreview() {
Text("Hello World")
}
These two Composable is identical. For SquareComposablePreview the preview runs without an issue, but for ComposablePreview is am getting the error.
Am I doing something wrong of there are issue with the tool !

Related

Why does Jetpack Compose preview show the wrong string resources?

Sometimes when I use a string resource in my composable, the preview will show the wrong string. It always works fine for literal strings, only string resources are wrong. The bug isn't consistent.
For example if I have this strings.xml:
<resources>
<string name="app_name">Violit</string>
<string name="load_topic_failure_message">Something went wrong loading the topic</string>
</resources>
And I have this composable:
#Composable
fun TopicFailureContent() {
Text(stringResource(R.string.load_topic_failure_message))
}
#Preview(showBackground = true)
#Composable
fun TopicFailureContentPreview() {
TopicFailureContent()
}
It might render something like "Partially checked" or "Navigation menu" instead of "Something went wrong loading the topic".
If I change the composable to this:
#Composable
fun TopicFailureContent() {
Text("Something went wrong loading the topic")
}
it renders the preview correctly.
It looks like the preview might be rendering nearby strings instead of the one I want. String resources work fine in tests and running the app. It's just preview that is not always working.
I'm using Android Studio Electric Eel 2022.1.1 but I was having the same problem on the previous version as well. This fails on both Compose UI version 1.2.1 and 1.3.3.
Any idea why string resources don't always work in preview and how to fix it?

How to enable click preview in jetpack compose preview function in android studio?

I have this composable function:
#Composable
fun GreetingText(name: String) {
Text(text = "Hello $name!", modifier = Modifier
.padding(24.dp)
.clickable { }
)
}
And this preview function:
#Preview(showBackground = true)
#Composable
fun DefaultPreview() {
SampleTextTheme {
GreetingText("world")
}
}
This is my version of android studio: Android Studio Arctic Fox | 2020.3.1 Patch 2
This is the preview design that android studio gives me:
I believe that by inserting the ciclckable {} function into my Modifier android studio should provide me with a tool in the preview design that would make it easier to see what would happen visually when I click on the element.
You can do it in Interactive mode.
Right not this feature is experimental, so you should enable it within Android Studio preferences.

Android Composable Text breaks preview when it receives a Color parameter. Is there a workaround?

On any Android Studio version up to Bumblebee 2011.1.1 Canary 11, the following View does not render and in fact breaks the Previewer in unexpected ways.
#Preview
#Composable
fun ColoredText(color: Color = Color.Red) = Text("text")
Stable version of Arctic Fox throws a MethodNotFoundError while the canary throws a warning saying it can't find the View. How can I get the preview to work again?
With #Preview Composables the main restriction is that the Preview Composable functions must not take any parameters.
Your ColoredText composable takes color: Color = Color.Red as a parameter hence doesn't render. You will also see the #Preview Annotation on your code highlighted in red.
To preview your code you can make a preview composable named ColoredTextPreview() which doesn't accept any parameter. Use this to preview the ColoredText() and pass in the Color Parameter
#Preview
#Composable
//preview doesn't accept parameters
fun ColoredTextPreview() = ColoredText(Color.Red)
#Composable
//create a 2nd non-preview composable that accepts parameters
fun ColoredText(color1: Color = Color.Red) {
Text(
text = "text",
color = color1,
modifier = Modifier.fillMaxWidth()
)
}
Be sure to include this line of code on your import statements to help with the Colors.
import androidx.compose.ui.graphics.Color
Thanks,

Unresolved reference for Modifier.preferredSize and Modifier.prefferredHeight [closed]

I am following this YouTube Tutorial where they are using Modifier.preferredSize() on a box and Modifier.preferredHeight() on a Spacer Composable - all other chained Modifiers are fine.
However Android Studio is not recognizing these 2 options.
Here is the code that I am working with:
Column() {
var isBlue by remember { mutableStateOf(false) }
val color = if(isBlue) Color.Blue else Color.Green
Button(onClick = { isBlue = !isBlue }) {}
Spacer(modifier = Modifier.preferredHeight(128.dp))
Box(modifier = Modifier.preferredHeight(128.dp).background(color = color)){}
}
The Editor is high-lighting preferredHeight as unresolved.
This is the image from the IDE for perspective.
I am using compose_version = '1.0.1' and I'm on AS Arctic Fox
preferredSize was renamed to size and preferredHeight to height
If I face some old video/article with invalid api, I'm searching through compose-samples(official samples from the maintainers) commits to find place where this method was deprecated/renamed, it's the easiest way to know if it just was renamed or I need to change more logic. In this case change was on this commit
Modifier.preferredWidth/preferredHeight/preferredSize were renamed to width/height/size starting from 1.0.0-beta01
I resolved it by following the mouse over suggestion, pressing the alt+Enter to import automatically the required referenced packages
I am following the lessons for andoid and kotlin and had stacked with this error.

Remove jetpack compose testtag for release builds

I am searching a way to remove jetpack compose testtags in release builds.
Not sure if there's something out-of-the-box on rc02.
But you can do the following:
fun Modifier.testTagDebug(tag: String): Modifier =
if (BuildConfig.DEBUG) this.testTag(tag) else this

Categories

Resources