Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
Is it possible to access an extension function written for TextView with Databinding in XML layout?

my extension function for TextView:

fun TextView.setColorifiedText(text : String) {
 // Some logic to change color of text
 setText(text)
}

my xml file with databinding:

<layout>
   <data>
      <!--some data-->
   </data>
   <LinearLayout>
      <TextView 
        android:setColorifiedText="some text to be colorified"
      />
   </LinearLayout>
</layout>
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Yes, it is possible to access an extension function written for a view in XML.

We have prefix the extension function name with 'set' and annotate it with 'BindingAdapter' annotation., for ex:- if your extension function name is 'colorText' change it to 'setColorText' and access the same in XML with the attribute name 'colorText' from 'app:' namespace. Also, annotate the extension function with @BindingAdapter("colorText")

@BindingAdapter("colorText")
fun TextView.setColorText(text : String) {
   // logic to something with the text
}

in XML :

<TextView
   app:colorText="your String" />

you can also modify the code using the kotlin's setter getter to advantage

In case of java declare the function as static and add an argument to the method which gets reference to the textview you gave the attribute to.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda