title: How to validate mobile number based on retrofit response from API?
body: <p>I have backend api in <code>node.js</code> and making android app from it. In android app there are two text boxes and one submit button. After user fill both fields and clicks submit I want to validate if mobile exist already or not. I am using <code>retrofit2</code> for api call. Now problem is that api call takes some time to validate mobile and retrofit using async call next code runs first and api response comes later. And I want to display a spinner during api working. But I am not able to do it. Please help with code as I am new to android.<br/><br/>
  Here is <b>RegisterActivity.java</b> file:</p>
  
  <pre><code>package com.example.narmail.truck30mint;
  
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.content.SharedPreferences;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ProgressBar;
  import android.widget.Toast;
  import com.example.narmail.truck30mint.Api.services.UserApi;
  import com.example.narmail.truck30mint.Api.services.NetworkStatus;
  
  public class RegisterActivity extends AppCompatActivity {
      EditText registerMobileInput, registerPasswordInput;
      Button registerButton;
      ProgressBar progressBar;
      public static String existmsg;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_register);
  
          progressBar = findViewById(R.id.progressBar);
          registerMobileInput = findViewById(R.id.registerMobileInput);
          registerPasswordInput = findViewById(R.id.registerPasswordInput);
          registerButton = findViewById(R.id.registerButton);
  
          registerButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if (NetworkStatus.isOnline(getApplicationContext())) {
                      if (validate()) {
                          Intent data = new Intent(RegisterActivity.this, OtpActivity.class);
                          data.putExtra("mobile", registerMobileInput.getText().toString());
                          data.putExtra("pass", registerPasswordInput.getText().toString());
                          startActivity(data);
                      }
                  } else {
                      Toast.makeText(getApplicationContext(), "Please connect to internet", Toast.LENGTH_LONG).show();
                  }
              }
          });
      }
  
      public boolean validate() {
          if (registerMobileInput.getText().toString().trim().length() == 0) {
              Toast.makeText(getApplicationContext(), "mobile number is mandatory", Toast.LENGTH_LONG).show();
              return false;
          } else if (registerMobileInput.getText().toString().trim().length() &gt; 0 &amp;&amp; registerMobileInput.getText().toString().trim().length() != 10) {
              Toast.makeText(getApplicationContext(), "Enter correct mobile number", Toast.LENGTH_LONG).show();
              return false;
          } else if (registerMobileInput.getText().toString().trim().length() == 10) {
              UserApi.mobileExist(registerMobileInput.getText().toString());
  
              while (existmsg == null) {
                  progressBar.setVisibility(View.VISIBLE);
                  System.out.println("in while loop");
              }
              progressBar.setVisibility(View.GONE);
              System.out.println("just below method called");
              if (existmsg != null &amp;&amp; existmsg.equalsIgnoreCase("exist")) {
                  Toast.makeText(getApplicationContext(), "mobile number already exist please login", Toast.LENGTH_LONG).show();
                  return false;
              } else if (existmsg != null &amp;&amp; existmsg.equalsIgnoreCase("notExist")) {
                  return true;
              } else {
                  Toast.makeText(getApplicationContext(), "Some error occured plz try again", Toast.LENGTH_LONG).show();
                  return false;
              }
          } else if (registerPasswordInput.getText().toString().trim().length() == 0) {
              Toast.makeText(getApplicationContext(), "Password is mandatory", Toast.LENGTH_LONG).show();
              return false;
          } else if (registerPasswordInput.getText().toString().trim().length() &lt; 6) {
              Toast.makeText(getApplicationContext(), "Please enter Min 6 chars", Toast.LENGTH_LONG).show();
              return false;
          }
          return true;
      }
  }
  </code></pre>
  
  <p>Here is java model class</p>
  
  <pre><code>package com.example.narmail.truck30mint.Api.models;
  import com.google.gson.annotations.Expose;
  import com.google.gson.annotations.SerializedName;
  
  public class mobileExist {
      @SerializedName("status")
      @Expose
      private String status;
  
      public String getStatus() {
          return status;
      }
      public void setStatus(String status) {
          this.status = status;
      }
  }
  </code></pre>
  
  <p>Here is Api</p>
  
  <pre><code>package com.example.narmail.truck30mint.Api.services;
  
  import android.util.Log;
  
  import com.example.narmail.truck30mint.Api.interfaces.UserClient;
  import com.example.narmail.truck30mint.Api.models.mobileExist;
  import com.example.narmail.truck30mint.RegisterActivity;
  import com.google.gson.GsonBuilder;
  
  import java.util.concurrent.Executors;
  
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  import retrofit2.Retrofit;
  import retrofit2.converter.gson.GsonConverterFactory;
  
  public class UserApi {
      private static String mobile_exist_url = "http://10.0.2.2:3002/";
      private static Retrofit retrofit;
      private static UserClient apiInterface;
      private static String mobileNoStatus;
      private static String signupStatus;
      private static Retrofit getMobileExist(){
          if (retrofit == null) {
              retrofit = new Retrofit.Builder().baseUrl(mobile_exist_url).callbackExecutor(Executors.newSingleThreadExecutor()).
                      addConverterFactory(GsonConverterFactory.create()).build();
          }
          return retrofit;
      }
      /*activity method start*/
      public static void mobileExist(String mobile){
          apiInterface = UserApi.getMobileExist().create(UserClient.class);
          Call&lt;mobileExist&gt; call = apiInterface.getMobileExist(mobile);
          call.enqueue(new Callback&lt;mobileExist&gt;() {
              @Override
            public void onResponse(Call&lt;mobileExist&gt; call, Response&lt;mobileExist&gt; response) {
                  if (response.isSuccessful()) {
                      System.out.println("response is successfull");
                      RegisterActivity.existmsg = response.body().getStatus();
                  } else {
                      RegisterActivity.existmsg = "error";
                       Log.w("status ",new GsonBuilder().setPrettyPrinting().create().toJson(response));
                  }
                  // Log.w("status ",new GsonBuilder().setPrettyPrinting().create().toJson(response));
                 // System.out.println("in response is now" + mobileNoStatus);
              }
                  @Override
                  public void onFailure (Call &lt; mobileExist &gt; call, Throwable t){
                      RegisterActivity.existmsg = "error";
                      Log.v("error", "is", t);
                  }
          });
        // return false;
      }
  }
  </code></pre>
  
  <p>Here is layout file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.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"
      tools:context=".RegisterActivity"&gt;
  
      &lt;TextView
          android:id="@+id/textView3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:textSize="20sp"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:text="@string/pleaseRegister"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.386"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.128" /&gt;
  
  
      &lt;EditText
          android:id="@+id/registerMobileInput"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:ems="10"
          android:inputType="phone"
          android:hint="@string/mobileHintRegister"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.503"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.358" /&gt;
  
      &lt;EditText
          android:id="@+id/registerPasswordInput"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:ems="10"
          android:hint="@string/passwordHintRegister"
          android:inputType="textPassword"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.503"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.498" /&gt;
  
      &lt;Button
          android:id="@+id/registerButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:text="@string/register"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.453"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.653" /&gt;
  
      &lt;ProgressBar
          android:id="@+id/progressBar"
          style="?android:attr/progressBarStyle"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginLeft="8dp"
          android:layout_marginRight="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:visibility="invisible"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.459"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="0.223" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>