![]() ![]() Showing that they are compiled down to private static fields of type Float (that’s the F at the end). field private static final IndicatorUnfocusedWidth:F field private static final IndicatorFocusedWidth:F. If we look at the Smali produced for TextFieldImplKt we see these two lines. This can be confirmed by looking at the bytecode directly. If you want to set the values to whatever you want, bear in mind that these variables are actually floats and trying to set a dp value directly will not work. In my case, 2.dp is perfect so I will be taking a slight shortcut by setting the unfocused width to the focused width. I want the unfocused width to be the same as the focused width so that I get a consistent line width throughout. dp private val IndicatorFocusedWidth = 2. #JAVA REFLECTION ANDROID CODE#We can have a quick look over the code and we see that IndicatorUnfocusedWidth and IndicatorFocusedWidth are private variables defined at the bottom of the file. #JAVA REFLECTION ANDROID ANDROID#Of course, if you’re viewing this in Android Studio and you have everything set up correctly, you should be able to see the source directly. If we follow the definition of OutlinedTextField we soon come across .class which contains the JVM byte-code for TextFieldImpl. The first thing to do is find where this width is actually set. So we want to modify the width of the text field outline. Of course, this isn’t an issue as Kotlin interoperates with Java perfectly. ![]() This includes not only public properties but also fields marked internal or private.Īlthough Kotlin has its own additional functionality for working with reflection, we will be using the standard Java reflection functions instead for this particular change as Kotlin specific reflection is still rather limited at the time of writing and the change we want to make isn’t actually possible. Simply put, reflection allows us to introspect our programs at runtime and to modify properties, fields, methods and other language constructs. We will be talking about reflection in the context of the JVM platform but executed on Android using Kotlin and Java. Of course, if there is an alternative way of achieving the same behaviour, prefer that but in this case there isn’t as OutlinedTextField is part of a library. There are many arguments both for and against using reflection which I won’t go in to here, but trivial uses such as the one I’m describing here aren’t dangerous and do not have any significant runtime performance costs. I’ve documented my approach and the final function which I call once at app launch to make the change. Of course, this isn’t a big deal but it didn’t fit the overall theme of my app so I decided to use reflection to fix this little niggle anyway. One such gap I came across was in the OutlinedTextField composable in the library which doesn’t offer a way to modify the default width of the outline. The new Compose system is a significant improvement over the previous View system but it is still rather new and there are missing features. Recently, I decided to redesign an old Android app with Jetpack Compose. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |