0

So I have this function below and I want to declare a textview inside it, however using findViewById seems not working. What should I do so I can declare a textview inside a function?

fun addingNewText(idt: Int): TextView {
            //val newtext = findViewById(R.id.idt) as TextView        
            val parameter = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)    
            //newtext.setLayoutParams(parameter)      
            //newtext.tag = idt.toString()
            //return newtext
}
  • Not enough information ...Where are you trying to access `findViewById()` - Activity, Fragment etc.. – m3g4tr0n Sep 07 '22 at 07:30
  • Actually you can ignore the findViewById() if you want, that's just for illustration. But my main goal is I want to declare a new textview inside a function that return a textview. @m3g4tr0n – PleaseDon'tBanMe Sep 07 '22 at 07:35
  • Does this answer your question? [Kotlin: How to get and set a text to TextView in Android using Kotlin?](https://stackoverflow.com/questions/44096838/kotlin-how-to-get-and-set-a-text-to-textview-in-android-using-kotlin) – abdo Salm Sep 07 '22 at 09:02

2 Answers2

1

I'm assuming you mean you want to create a new TextView. Like most classes, you can just call its constructor. for example:

val newtext = TextView(this)

(this here is a context, for example an Activity)

Ivo
  • 18,659
  • 2
  • 23
  • 35
  • When I replace `//val newtext = findViewById(R.id.idt) as TextView` with your code, I got error "type mismatched, required : context!, found : words" – PleaseDon'tBanMe Sep 07 '22 at 07:38
0

If you're trying create a new view you should use Ivo's answer, Or if you're trying get view in a layout, you should use val newtext = findViewById<TextView>(R.id.idt). Because in kotlin you have to infer what type of view you are trying retrieve. Refer to this thread for more clearance.

TechDash
  • 7
  • 3
  • 8
  • Actually I've tried `//val newtext = findViewById(R.id.idt)` too and it's not working. I am trying to implement Ivo's answer, but I got error – PleaseDon'tBanMe Sep 07 '22 at 07:41
  • What kind of error are you getting? and explain what are you trying todo in detail. – TechDash Sep 07 '22 at 07:46
  • I comment the error on Ivo's answer and what I'm trying to do is to call addingNewText function whenever I need to create a new textview. Let's say I want to add multiple textview so I just need to call the function inside a for loop. – PleaseDon'tBanMe Sep 07 '22 at 07:49
  • What are passing to the TextView's constructor? @PleaseDon'tBanMe – TechDash Sep 07 '22 at 07:55
  • That's the question I also looking for. Because I don't know what should I pass to the TextView's constructor (e.g. what value should I replace for `this` to avoid the error) – PleaseDon'tBanMe Sep 07 '22 at 08:03
  • You don't have to replace `this`. Just input the word `this`. Because Views require an activity class as a context and you pass your current activity as context using the Kotlin keyword `this`. @PleaseDon'tBanMe – TechDash Sep 07 '22 at 08:08
  • Well then it should not produce the "mismatch error", that's why I confuse why Kotlin detect it as an error – PleaseDon'tBanMe Sep 07 '22 at 08:16
  • Are you extending any Activity class @PleaseDon'tBanMe? – TechDash Sep 07 '22 at 08:20
  • Nope, I only set `TextView` as the return type of my `addingNewText` function. `fun addingNewText(idt: Int): TextView {` – PleaseDon'tBanMe Sep 07 '22 at 08:25
  • Can you edit your question to include the entire kotlin class instead of the `addingNewText` function? @PleaseDon'tBanMe – TechDash Sep 07 '22 at 08:28
  • Turns out I just need to declare it outside the function, and use it inside the function – PleaseDon'tBanMe Sep 07 '22 at 08:36