skip to Main Content

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
}

2

Answers


  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)

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search