Reputation: 115
Using Kotlin, I would like to be able to have an invisible Textview become visible on my activity when the user clicks a button. Ideally, I would like them to enter a particular code (i.e 1234) into a plain textview field (id PW1) then click the submit button (id sub1), then I would like a hidden textview (id phone1) to appear to allow the user to enter some more data.
Any help greatly appreciated
Many thanks Please see code below...App runs but crashes when I go to the activity with this code.
val sub1 =findViewById<Button>(R.id.sub1)
sub1.setOnClickListener {
val pw1: String = pw1.text. toString()
if (pw1.equals( "1234"))
phone1.visibility = View.VISIBLE
else phone1.visibility = View.INVISIBLE }
Upvotes: 3
Views: 5196
Reputation: 310
Give your view an ID, by adding android:id="@+id/myTextView"
in your XML tag.
Then, all you have to do, is run myTextView.visibility = View.VISIBLE
or myTextView.visibility = View.HIDDEN
or myTextView.visibility = View.GONE
to change its state.
Your example states that you want on button click; add an ID to the button, and in your onCreate function in your Activity, add an onclicklistener:
myButton.setOnClickListener {
// your code here
myTextView.visibility =
if (condition) View.VISIBLE
else View.HIDDEN
}
Some more techniques on how to achieve this in this question : How to set visibility in Kotlin?
Upvotes: 3
Reputation: 277
Load your textview inside a variable. Then textView.visibility = View.VISIBLE
and if you want to hide the textview again textView.visibility = View.GONE
Upvotes: 0