On clicking the button, a new activity opens. I am trying to addView to new activity from ActivityMain.kt. I know that this would be easy if I use designer to addView, but I don't know how many views to Add. Therefor I have to do this programmatically.
Here is my activity_numbers code that is another activity which opens when I click on the button.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/activity_numbers_root_view"
tools:context=".NumbersActiviy">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/numbers_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the code in MainActivity.kt file
numbersText.setOnClickListener {
val intent: Intent = Intent(this, NumbersActiviy::class.java)
startActivity(intent)
// Commenting the following code, the application works just fine.
val numbersRootView:ConstraintLayout = findViewById(R.id.activity_numbers_root_view)
val textView = TextView(numbersRootView.context)
textView.text = "This is from " + wordsList.get(0)
numbersRootView.addView(textView)
}
Here is the error I get: Image of the error
I have also tried putting the code outisde the onClickListener but not worked. I have also tried using
import kotlinx.android.synthetic.main.activity_numbers.*