title: Show/Hide ProgessBar from ViewModel using Databinding - MVVM
body: <p>While getting a response from web service, I would like to Show/Hide a progressBar using ViewModel and dataBinding. Currently, I am having a function loginOnClicked in the ViewModel bound from my layout XML directly.</p>
  
  <p>I want to create a progress bar and implemented it in function loginOnClicked and update the visibility based on that. Can somebody help me with that? </p>
  
  <p><strong>loginActivity</strong></p>
  
  <pre><code>class loginActivity : AppCompatActivity(),SignupResultCallBack {
  
              override fun onSucces(message: String) {
                  Toast.makeText(this,message, Toast.LENGTH_SHORT)
                          .show()
              }
  
              override fun onError(message: String) {
                  Toast.makeText(this,message, Toast.LENGTH_SHORT)
                          .show()
              }
  
  
              override fun onCreate(savedInstanceState: Bundle?) {
                  super.onCreate(savedInstanceState)
                 // setContentView(R.layout.activity_login)
  
  
  
       val activityLoginBinding=DataBindingUtil.setContentView&lt;ActivityLoginBinding(this,R.layout.activity_login)
         activityLoginBinding.viewModel=of(this,LoginViewModelFactory(this)).get(LoginViewModel::class.java)
  
  
  
     link_signup.setOnClickListener() {
          var intent=Intent(this@loginActivity,signupActivity::class.java)
                      startActivity(intent)
                      finish()
                  }
  
                  var test=activityLoginBinding.viewModel!!.progBar()
                  if(test)
                      progressBar?.visibility=View.VISIBLE
  
                  else
                      progressBar?.visibility=View.GONE
  
              }
          }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>    class LoginViewModel(private var listener: SignupResultCallBack): ViewModel() {
          private val loginUser:UserRequest
  
  
  
          init {
              this.loginUser= UserRequest("","")
          }
  
           fun loginOnCLicked(v:View){
               var editLogin: Int = loginUser.isDataValid()
                           if (editLogin == 0)
                           listener.onError("entrer votre email")
                          else
                           if (editLogin == 1)
                           listener.onError("email invalide")
                          else
                           if (editLogin == 2)
                           listener.onError("mot de passe invalide")
                          else
                           if (editLogin == 3)
                               listener.onError("entrer votre mot de passe")
                           else
                               if (editLogin == 4)
                                   listener.onError("champs vides")
                               else
  
  
                               {
                                   RetrofitClient.instance.loginUser(UserRequest(loginUser.getEmail(), loginUser.getPassword())).enqueue(object : Callback&lt;DefaultResponse&gt; {
                                       override fun onFailure(call: Call&lt;DefaultResponse&gt;, t: Throwable) {
                                           listener.onError("t.message")
  
                                       }
  
                                       override fun onResponse(call: Call&lt;DefaultResponse&gt;, response: Response&lt;DefaultResponse&gt;) {
  
                                           if (response.code() == 400)
                                               listener.onSucces("login or password invalid")
                                           else if (response.code() == 200)
                                               listener.onSucces("success login")
  
  
                                       }
  
                                   })
  
                 }
  
           }
  
  
  
  }
  </code></pre>
  
  <p><strong>login.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
      &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.example.rahma.alerteaccidentapp.vm.login.LoginViewModel"/&gt;
  
          &lt;/data&gt;
  
  
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@color/colorBackground"
               tools:context=".ui.login.loginActivity"&gt;
  
  
              &lt;Button
                  android:id="@+id/btn_connexion"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginTop="30dp"
                  android:background="@drawable/btn_round"
                  android:text="Connexion"
                  android:onClick="@{viewModel::loginOnCLicked}"
  
                  android:layout_marginLeft="20dp"
                  android:layout_marginRight="20dp"
                  android:layout_below="@id/password_Edit"
                  android:textAppearance="@style/MySerifBoldbtn"
                  android:textColor="@android:color/background_light"
                  /&gt;
  
  
  
          &lt;/RelativeLayout&gt;
  
  
      &lt;/layout&gt;  
  </code></pre>