title: How can I change the visibility of an xml object in ViewModel?
body: <p>I have a question about how the visibility of an object in the ViewModel will change.</p>
  
  <p>I'll tell you my case: I have a login interface that has two Edittexts and two buttons, a button and an Edittext are invisible by default, and I want the button that is visible to make the first Edittext and the button that I clicked invisible and make the second button and the second Edittext visible. And here's the problem, I could do all this in Activity, but I need to do it in ViewModel and I have no idea how to access the xml components from there.</p>
  
  <p>I know all this is messy so I'll send the classes and if someone could tell me how to do this I'd appreciate it. Thank you. Thank you.</p>
  
  <p>Login XML:</p>
  
  <pre><code>&lt;layout 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"&gt;
  
  &lt;data&gt;
      &lt;variable
          name="viewModel"
          type="com.quobis.sippo.ecco.viewmodel.LoginViewModel"/&gt;
  &lt;/data&gt;
  &lt;android.support.constraint.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context="LoginUser"&gt;
  
      &lt;ImageView
          android:layout_width="250dp"
          android:layout_height="200dp"
          android:src="@drawable/logom"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.2"
      /&gt;
  
  
      &lt;EditText
          android:id="@+id/usr"
          android:layout_width="200dp"
          android:layout_height="50dp"
          android:hint="@string/hint_user"
          android:textSize="18sp"
          android:textColorPrimary="@color/colorLetterLogin"
          android:backgroundTint="@color/colorBackButtLogin"
          android:elevation="20dp"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginLeft="60dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.6"
          app:addTextChangedListener="@{viewModel.emailTextWatcher}"
      /&gt;
  
      &lt;Button
          android:id="@+id/btn_usr"
          android:layout_width="55dp"
          android:layout_height="50dp"
          android:background="@color/colorBackButtLogin"
          android:drawableBottom="@drawable/ic_keyboard_arrow_right"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginLeft="260dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.6"
          android:onClick="@{viewModel::onUserClicked}"
      /&gt;
  
      &lt;EditText
          android:id="@+id/pass"
          android:layout_width="200dp"
          android:layout_height="50dp"
          android:hint="@string/hint_pass"
          android:textSize="18sp"
          android:shape="rectangle"
          android:inputType="textPassword"
          android:textColorPrimary="@color/colorLetterLogin"
          android:backgroundTint="@color/colorBackButtLogin"
          android:elevation="20dp"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginLeft="60dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.6"
          android:visibility="invisible"
          android:onClick="@{viewModel::onLoginClicked}"
          /&gt;
  
      &lt;Button
          android:id="@+id/btn_pass"
          android:layout_width="55dp"
          android:layout_height="50dp"
          android:background="@color/colorBackButtLogin"
          android:drawableBottom="@drawable/ic_keyboard_arrow_right"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginLeft="260dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.6"
          android:visibility="invisible"
          /&gt;
  
      &lt;Spinner
          android:id="@+id/spinner_usr"
          android:layout_width="120dp"
          android:layout_height="40dp"
          app:layout_constraintBottom_toBottomOf="parent"
          android:layout_marginLeft="40dp"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.85"
      /&gt;
  &lt;/android.support.constraint.ConstraintLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>class LoginViewModel(private val listener: LoginResultCallbacks) : 
  ViewModel() {
  private val user: UserModel
  var userp ="jorge"
  
  
  
  
  init {
      this.user= UserModel(email = "")
  }
  
  
  fun emailTextWatcher(): TextWatcher {
      return object : TextWatcher {
          override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
              user.setEmail(s.toString())
          }
  
          override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
  
          }
  
  
          override fun afterTextChanged(s: Editable) {
  
          }
      }
  
  }
  
  fun onUserClicked(v: View) {
  
  
  }
  
  fun onLoginClicked(v:View) {
      if (user.getEmail() == userp)
          listener.onSucces("Correcto")
      else
          listener.onError("Fallo")
  }
  }
  </code></pre>
  
  <p>Note: the method to change the visibility would be the onUserClicked.</p>
  
  <p>Main activity:</p>
  
  <pre><code>class LoginUser : AppCompatActivity(), LoginResultCallbacks {
  lateinit var binding: ActivityLoginUserBinding
  
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      binding = DataBindingUtil.setContentView(this, R.layout.activity_login_user)
      binding.viewModel = ViewModelProviders.of(this, LoginViewModelFactory(this)).get(LoginViewModel::class.java)
  
  
      var languages = arrayOf("English", "Español", "Galego")
      val spinner = binding.spinnerUsr
      if (spinner != null) {
          val arrayAdapter = ArrayAdapter(this, R.layout.spinner_item, languages)
          spinner.adapter = arrayAdapter
      }
  
  }
  
  
  override fun onSucces(message: String) {
      Toast.makeText(this,"Login bueno", Toast.LENGTH_SHORT).show()
  }
  
  @SuppressLint("ResourceAsColor")
  override fun onError(message: String) {
      binding.btnUsr
      btn_usr.setBackgroundColor(R.color.colorFailLogin)
  }
  }
  </code></pre>