Adding a style to custom text view in compose - android

I have a CustomTextView, I used to add style to it using xml style attribute, but with compose I need to understand how to do it.
When using this :
AndroidView( factory = { context->
RegularAmountTextView(ContextThemeWrapper(context,R.style.TextAppearance_MyTheme_Heading_Small ),null ).apply {
text ="1234"
}
}, modifier = Modifier.fillMaxWidth().wrapContentHeight() )
I get error: Infinite cycle trying to resolve '?attr/textColorHighlight': Render may not be accurate. in preview panel.
while on running on device I get:
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
at android.content.res.TypedArray.getColor(TypedArray.java:529)
at android.widget.TextView.readTextAppearance(TextView.java:4345)
at android.widget.TextView.<init>(TextView.java:1340)
at android.widget.TextView.<init>(TextView.java:1258)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:108)
at androidx.appcompat.widget.AppCompatTextView.<init>(AppCompatTextView.java:103)
Similar error only. If I remove the style then it works. Has Android team provided any solution for this.
Another question: On facing these errors I was thinking is using compose on production a wise decision, or we should wait till android team provides proper inter-portability.

Related

Compose UI testing - How do I assert a text color?

I'm trying to test a Text that on my component I can print it in different colors, so on my test I'm verifying it gets the expected color. I was looking for a method to return the color but I did not find any.
From now I'm asserting that the text is correct and the visibility is correct, but when trying to find the method to get the colour I get too deep and I'm looking for a simpler solution.
composeTestRule.onNode(hasTestTag("testTagForButton"), true)
.assertExists()
.assertTextEquals("Testing")
I've check that I can do something like .fetchSemanticsNode().layoutInfo.getModifierInfo() to get into the Modifier and perhaps from there I can get the colour, but it's too much maybe. Also I've found this .captureToImage() that perhaps I could get the colour on it, but since I had to put the pixels I decided that it's not the way.
Is there any simple way to get that?
I am by no means a compose expert, but just looking at compose source code, you could utilize their GetTextLayoutResult accessibility semantic action. This will contain all the properties that are used to render the Text on a canvas.
Some quick and dirty extension functions I put up for convenience:
fun SemanticsNodeInteraction.assertTextColor(
color: Color
): SemanticsNodeInteraction = assert(isOfColor(color))
private fun isOfColor(color: Color): SemanticsMatcher = SemanticsMatcher(
"${SemanticsProperties.Text.name} is of color '$color'"
) {
val textLayoutResults = mutableListOf<TextLayoutResult>()
it.config.getOrNull(SemanticsActions.GetTextLayoutResult)
?.action
?.invoke(textLayoutResults)
return#SemanticsMatcher if (textLayoutResults.isEmpty()) {
false
} else {
textLayoutResults.first().layoutInput.style.color == color
}
}
Which can be then used like this:
composeTestRule.onNode(hasTestTag("testTagForButton"), true)
.assertExists()
.assertTextEquals("Testing")
.assertTextColor(Color.Black)
I am unable to comment post above, also didn't find question about checking background color, so decide to place my version here.
private fun hasBackground(node: SemanticsNode, color: Color, shape: Shape): Boolean {
return node.layoutInfo.getModifierInfo().filter { modifierInfo ->
modifierInfo.modifier == Modifier.background(color, shape)
}.size == 1
}
To test background color and don't touch debug inspection info (this isn't for testing) we are unable to test only background color, but can test whole background by comparing production background (which placed into modifier) with our testing one.
Hope it help somebody.

Unresolved references from androidx.compose.animation and androidx.compose.animation.core

Can anyone help me to find the replacement of below from compose animation and core.
androidx.compose.animation.DpPropKey
androidx.compose.animation.core.transitionDefinition
androidx.compose.animation.core.state
androidx.compose.animation.core.TransitionState
All are working fine with
compose_version = '1.0.0-alpha05'
but not working with
composeVersion = '1.0.0-beta06'
Unfortunately could not find in documentation https://developer.android.com/jetpack/androidx/releases/compose-animation
The whole set of animations were changed with a new set without a mapping 1:1.
Now you can use updateTransition or rememberInfiniteTransition.
More details here.

Android Fotoapparat library

I'm new to Android-Developement and I would like to make a Camera-app. I found this library (this is the Github page).
But I don't know how to implement a library. I followed these steps (method 2) but I'm getting an error in a popup window called 'IDE Fatal Errors'. It says: 'To investigate / fix the problem IDE wants to attach following files to the bug report. We recommend to include all the files providing maximum information. Note: all the data you send will be kept private.' Then I can select a 'diagnostic.txt'. There is a section 'file content' where 'rootsChanged' is written. I can report the whole window to Google.
The following step is to configure the 'Fotoapparat' instance. What is an instance? When I search on Google I only find articles talking about making a library.
I'm sorry if these are stupid question but I am a beginner and I would like to learn more about Android-Development. Thanks in advance for your time and help.
Add this line in your build.gradle(Module: app) file ->
dependecies {
//Your other dependencies...
implementation 'io.fotoapparat:fotoapparat:2.3.3'
}
And start using your code. Library is working fine.
EDIT - >
You need to learn basics of java.
To setup instance of the object you need to create a variable.
Hence in your case:
Fotoapparat yourVariableName = new FotoapparatFotoapparat
.with(context)
.into(cameraView) // view which will draw the camera preview
.previewScaleType(ScaleType.CenterCrop) // we want the preview to fill the view
.photoResolution(ResolutionSelectorsKt.highestResolution()) // we want to have the biggest photo possible
.lensPosition(LensPositionSelectorsKt.back()) // we want back camera
.focusMode(SelectorsKt.firstAvailable( // (optional) use the first focus mode which is supported by device
FocusModeSelectorsKt. continuousFocusPicture(),
FocusModeSelectorsKt.autoFocus(), // in case if continuous focus is not available on device, auto focus will be used
FocusModeSelectorsKt.fixed() // if even auto focus is not available - fixed focus mode will be used
))
.flash(SelectorsKt.firstAvailable( // (optional) similar to how it is done for focus mode, this time for flash
FlashSelectorsKt.autoRedEye(),
FlashSelectorsKt.autoFlash(),
FlashSelectorsKt.torch()
))
.frameProcessor(myFrameProcessor) // (optional) receives each frame from preview stream
.logger(LoggersKt.loggers( // (optional) we want to log camera events in 2 places at once
LoggersKt.logcat(), // ... in logcat
LoggersKt.fileLogger(this) // ... and to file
))
.build();
And start using yourVariableName.

Iconize Xamarin with Fontawesome

I'm new to Xamarin and I'm trying to use the Iconize NuGet package for Xamarin, but I'm not having much luck. At the moment I'm working with a simple Android app. I've installed Iconize per the instructions and, save for an error on the line:
FormsPlugin.Iconize.Droid.IconControls.Init(Resource.Id.toolbar, Resource.Id.tabs);
(the compiler didn't like the Resource.Id.toolbar or Resource.Id.tabs so I removed it) everything compiles and runs. However, when I try to add an IconButton with a Fontawesome image, I get "System.NullReferenceException: Object reference not set to an instance of an object." error that points back to
the line LoadApplication( new App() ); in the MainActivity.cs.
I'm trying to add the IconButton to a grid in code (not XAML) using
grid.Children.Add( new IconButton
{
Image = "fa-info-circle"
}, 3, 2 );
Any ideas on how to make this work? The examples on the Iconize page haven't been very useful and I haven't found any examples on Google.
Okay I finally found something that was useful. I found a clue on this page and some other information on the project issues page on Github.
Ultimately, to get the icon to display I used
grid.Children.Add( new IconButton
{
Text = "fa-info-circle",
}, 3, 2 );
It was the Text property and not the Image property that I should have used.
Hope this helpful to someone else.

Android svg parsing , official library issues

Hello im writing an android app using the android svg-android.jar. (I think it's the google official library) I'm trying to parse my svg im getting an error. my svg falls on this line :
<stop offset="0.1774" style="stop-color:#EDEDED;stop-opacity:0.2"/>
the error i get is this :
java.lang.NumberFormatException: Invalid int: "EDEDED;stop-opacity:0.2"
in the google code for the project i saw this :
if (gradient != null) {
float offset = getFloatAttr("offset", atts);
String style = getStringAttr("style", atts);
if (style.startsWith("stop-color:#")) {
int color = Integer.parseInt(style.substring("stop-color:#".length()), 16);
color |= 0xFF000000;
gradient.positions.add(offset);
gradient.colors.add(color);
}
}
it seems that google can't handle the style having opacity too.
the svg itself is generated through a program .
my question is this: is there another android library for svg parsing ? i searched and couldn't find .
does the google code site doens't have the latest code ?(google i heard stopped supporting it)
is there another way to give opacity to the stop element which doesn't include the style attribute ?
or most annoying option should I wait for whoever wrote it to fix it ?(commented on google code project site also but the last comment is from 2014 don't know if i get referrenced at all).
help would be appreciated thanks !
There is another to give opacity to the stop element which doesn't include the style attribute, mapped CSS attributes e.g.
<stop offset="0.1774" stop-color="#EDEDED" stop-opacity="0.2"/>

Categories

Resources