3

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 }
5
  • Does this answer your question? How to set visibility in Kotlin? Commented Oct 3, 2020 at 18:12
  • provide some code, all the answers below are valid answers
    – Mohsen
    Commented Oct 3, 2020 at 23:02
  • add the error too
    – Mohsen
    Commented Oct 4, 2020 at 15:24
  • Hi there, I have it working now...incorrectly named button!!! Although the data is disappearing when I move to another activity and then return. I want the data to remain in the activity once the user has entered it. Do I need to do something with onCreate and SavedInstanceState? Thank you Commented Oct 4, 2020 at 15:49
  • To have the application data retained when swapping activities you need to override savedinstancestate with code that stores the state in the bundle, then read that bundle in your OnCreate. Commented Oct 4, 2020 at 18:25

2 Answers 2

3

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.

  • VISIBLE will show the view
  • HIDDEN will hide it but it'll still have the space for that element reserved
  • GONE will hide it as if it was completely unexistant.

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?

1
  • Thank you for this. This is along the lines of what I have been trying...I must add, and I am sure you have realised that I am a complete newbie to this. But I have the following: sub1 =findViewById<Button>(R.id.sub1) sub1.setOnClickListener { if (pw1 = "1234") phone1.visibility = View.VISIBLE else phone1.visibility = View.INVISIBLE } Commented Oct 3, 2020 at 22:13
0

Load your textview inside a variable. Then textView.visibility = View.VISIBLE

and if you want to hide the textview again textView.visibility = View.GONE

1
  • Thank you...struggling a but with this though....as per my comment above Commented Oct 3, 2020 at 22:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.