Questions tagged [kotlin-extension]

Kotlin provides the ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator. This is done via special declarations called extensions.

333 questions
0
votes
1 answer

Kotlin Extension Function On a Generic Interface

I am trying to write an extension function for a Kotlin interface that simply returns an Observable of the object. So far, this is what I have been able to do, but I don't know if it is the best way to implement this functionality. Here is what I…
anthonylawson
  • 761
  • 9
  • 24
0
votes
0 answers

Android lint shows UnusedIds after converting findViewById to kotlin synthetic

I converted findViewById to kotlin synthetic after I did that I get the below error. But everything works the same. I have the exact issue before in fragment. In there I solve it by accessing the id after the onCreateView() i.e onViewCreated(). here…
gouri panda
  • 312
  • 2
  • 11
0
votes
1 answer

Accessing outer this in extension property

I have function like this that works fine: private fun List.toStringArray() = list().apply { for (hasName in this@toStringArray) add(hasName.name) }.toTypedArray() I wanted to convert it to property syntax like…
Renetik
  • 5,887
  • 1
  • 47
  • 66
0
votes
0 answers

How it's possible to extend function type in kotlin?

I just can't understand why it is possible to extend function type in kotlin ? And then why we can't extend String? then, which is also a type. interface CustomFunctionType : () -> Unit { // this is possible, buy how? }
abhi
  • 961
  • 9
  • 13
0
votes
1 answer

Extending Kotlin's extension

I'm trying to create an extension for KLogger/Logger which is an extension for slf4j's logger in order to better handle structured log messages. Could someone explain what's wrong with my code and how to fix it or work around it without passing my…
Robert Gonciarz
  • 363
  • 2
  • 8
  • 15
0
votes
1 answer

Can't use kotlin-extension in Adapter

Android Studio 3.6 build.gradle: apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: "kotlin-kapt" android { dataBinding { enabled = true } …
Alexei
  • 14,350
  • 37
  • 121
  • 240
0
votes
4 answers

How to check if Image Quality is Low

I am developing a PhotoBook app where I need to notify the user if they add a low-resolution image. I just need to show a "Low-Resolution Image, Printing may be affected" warning as chatbooks app do.
Hitesh Kushwah
  • 1,351
  • 13
  • 23
0
votes
1 answer

Kotlin: How to access a field with the same name as an extension?

I have the following field extension import com.google.gson.annotations.SerializedName val Enum<*>.serializedName: String get() = javaClass.getField(name).getAnnotation(SerializedName::class.java).value Now if my enum has the same field name as…
Quang Ngô
  • 45
  • 1
  • 5
0
votes
1 answer

Extension function with different signature is confusing with different extension function in kotlin

class Example fun main(){ val example: Example = Example() // what function i am calling is it fun Example.extensionFun() ? // Or is it Example.extensionFun(string: String) ? example.extensionFun() } // first extension…
user10614828
0
votes
2 answers

Is there any way I can get context from fragmentmanager?

I have an extension function with FragmentManager as receiver. I have used it at atleast 75 places. Now I want to access sharedpreference in that extension. All I need is context inside that function. Any way I can access context? Here is how…
Aziz
  • 1,976
  • 20
  • 23
0
votes
1 answer

Using Kotlin's extension function to suppress a returned value

I am using Kotlin in a spring boot application. Especially in the services, I have found that some of the function need to suppress the returned value from repository. For example, here is a save() that saves an entity without returning the…
Prashant
  • 4,775
  • 3
  • 28
  • 47
0
votes
1 answer

Can I make nice Kotlin extension function?

Here is my code theme?.let { .... titleText.setTextColor(it.pTextColor) contentText.setTextColor(it.pTextColor) } } and the usage of this extension is here cardView.setTheme(theme) How can I have this cardView.theme = theme
0
votes
3 answers

What is the alternative for utils in Kotlin?

I have this piece of code that repeats in 3 places if (orientation == AppConstants.LANDSCAPE) { width = height = }else{ width = height = } imageUrl.let { val options =…
Artur A
  • 257
  • 3
  • 20
0
votes
1 answer

How to make this code nicer should I have utils method or extension?

So this getLocalizedTitle() function I have for this 3 type of classes, should I have as utils and pass titles to this function or there can be nicer solution with extensions? data class Carousel(val id: String, val titles: List) :…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/android" class="post-tag grid--cell" title="show questions tagged 'android'" rel="tag">android</a> <a href="../../questions/tagged/kotlin" class="post-tag grid--cell" title="show questions tagged 'kotlin'" rel="tag">kotlin</a> <a href="../../questions/tagged/kotlin-extension" class="post-tag grid--cell" title="show questions tagged 'kotlin-extension'" rel="tag">kotlin-extension</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Jul 09 '19 at 20:27">asked Jul 09 '19 at 20:27</time> <a href="../../users/9996550/artur-a" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/9996550.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Artur A" /> </a> <div class="s-user-card--info"> <a href="../../users/9996550/artur-a" class="s-user-card--link">Artur A</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">257</li> <li class="s-award-bling s-award-bling__silver" title="3 silver badges">3</li> <li class="s-award-bling s-award-bling__bronze" title="20 bronze badges">20</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="mln24"> <div class="question-summary" id="question-summary-56734857"> <div class="statscontainer"> <div class="stats"> <div class="vote"> <div class="votes"> <span class="vote-count-post"><strong>0</strong></span> <div class="viewcount">votes</div> </div> </div> <div class="status answered-accepted"> <strong>1</strong> answer </div> </div> </div> <div class="summary"> <h3><a href="../../questions/56734857/extension-property-function-declaration-must-have-a-name-unresolved-reference-" class="question-hyperlink">Extension property: Function declaration must have a name Unresolved reference: get</a></h3> <div class="excerpt">I use extension function to extend retrofit2.Response object: Snippet: public class ErrorResponse { private int code; private String message; private Response response; } import okhttp3.MediaType import okhttp3.Protocol import…</div> <div class="grid ai-start jc-space-between fw-wrap"> <div class="grid gs4 fw-wrap tags "> <a href="../../questions/tagged/kotlin" class="post-tag grid--cell" title="show questions tagged 'kotlin'" rel="tag">kotlin</a> <a href="../../questions/tagged/kotlin-extension" class="post-tag grid--cell" title="show questions tagged 'kotlin-extension'" rel="tag">kotlin-extension</a> </div> <div class="started mt0"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="asked Jun 24 '19 at 10:41">asked Jun 24 '19 at 10:41</time> <a href="../../users/6813231/alexei" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/6813231.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Alexei" /> </a> <div class="s-user-card--info"> <a href="../../users/6813231/alexei" class="s-user-card--link">Alexei</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">14,350</li> <li class="s-award-bling s-award-bling__gold" title="37 gold badges">37</li> <li class="s-award-bling s-award-bling__silver" title="121 silver badges">121</li> <li class="s-award-bling s-award-bling__bronze" title="240 bronze badges">240</li> </ul> </div> </div> </div> </div> </div> </div> </div> <div class="s-pagination pager fr"> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=16" rel="prev" title="Go to page 16">Prev </a> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=1" rel="" title="Go to page 1">1</a> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=2" rel="" title="Go to page 2">2</a> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=3" rel="" title="Go to page 3">3</a> <div class="s-pagination--item s-pagination--item__clear">…</div> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=22" rel="" title="Go to page 22">22</a> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=23" rel="" title="Go to page 23">23</a> <a class="s-pagination--item" href="../../questions/tagged/kotlin-extension_page=18" rel="next" title="Go to page 18"> Next</a> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>