OTP time counter in android
<p>I need to put a 30sec timer in my otp verfication page after 30secs timer should be gone and the keyboard should pop up. what needs to be done??
Below are my java files.</p>

<p>SignUpFragment.java</p>

<pre><code>SignUpActivity activity;

/**
 * Declaring Variables for Views
 */
TextView SignupAlreadyMember;

EditText SignUpName, SignUpEmail,referCode;
Button SignUpRegister;

VirtualApplication virtualApplication;

// Progress dialog
private ProgressDialog pDialog;


public SignUpFragment() {
}


@Override
public void onAttach(Activity activit) {
    super.onAttach(activit);
    this.activity = (SignUpActivity) activit;
    virtualApplication = (VirtualApplication) activity.getApplicationContext();


}

int bodyId;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        bodyId = getArguments().getInt("output");
    }
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_sign_up, container, false);


       activity.setTempTitle("signup");
         inItViews(view);
    SignUpRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RegisterUser();
            // showVerifyMobileScreen(123);

        }
    });
    SignUpEmail.setOnEditorActionListener(new EditText.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId,
                                      KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                RegisterUser();
            }

            return false;
        }
    });

    SignupAlreadyMember.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            gotoLoginScreen();
        }
    });

    return view;
}

private void gotoLoginScreen() {

    Intent intent = new Intent(activity, LoginActivity.class);
    startActivity(intent);
    activity.finish();
}




public boolean validCellPhone(String number) {
    return Patterns.PHONE.matcher(number).matches();
}

private void RegisterUser() {

    String email = SignUpEmail.getText().toString().trim();
    String name = SignUpName.getText().toString().trim();
    String code=referCode.getText().toString().trim();

    if (email.isEmpty() ||!SystemUtil.isValidMobile(email)) {
        SignUpEmail.setError("Enter Mobile number here");
        return;
    }


    JSONObject jsonObject = new JSONObject();

    try {
        jsonObject.put("name", name);
        jsonObject.put("phone_number", email);
        jsonObject.put("body_id", bodyId);
        jsonObject.put("referral_code",code);//referral_code
        TelephonyManager tm = (TelephonyManager) activity
                .getSystemService(Context.TELEPHONY_SERVICE);
      jsonObject.put("imei", SystemUtil.getIMEINumber(activity, tm))  ;
    } catch (JSONException e) {
        e.printStackTrace();
    }
    SignUpRegister.setEnabled(false);
    requestForRegistration(jsonObject);


}

private void requestForRegistration(JSONObject jsonObject) {
    showpDialog();
    RequestQueue requestQueue = CustomVolleyRequestQueue.getInstance(activity).getRequestQueue();
    final CustomJSONObjectRequest customJSONObjectRequest = new CustomJSONObjectRequest(Request.Method.POST, AppConstants.REGISTER_URL, jsonObject, this, this);
    customJSONObjectRequest.setTag("Register");
    customJSONObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    customJSONObjectRequest.setShouldCache(true);

    requestQueue.add(customJSONObjectRequest);
    //
}

private void showVerifyMobileScreen(int userId) {
    VerificationFragment verificationFragment = new VerificationFragment();
    Bundle bundle = new Bundle();
    bundle.putInt("userId", userId);
    bundle.putString("mobile", SignUpEmail.getText().toString().trim());
    verificationFragment.setArguments(bundle);
    activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment, verificationFragment).commit();
}

private void inItViews(View view) {
    TextView SignUPHeader = (TextView) view.findViewById(R.id.SignUPHeader);
    TextView Signupmessage= (TextView) view.findViewById(R.id.Signupmessage);
    Signupmessage.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    SignUPHeader.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    SignupAlreadyMember = (TextView) view.findViewById(R.id.SignupAlreadyMember);
    SignupAlreadyMember.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    SignUpName = (EditText) view.findViewById(R.id.SignUpName);
    SignUpName.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    SignUpEmail = (EditText) view.findViewById(R.id.SignUpEmail);
    SignUpEmail.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    SignUpRegister = (Button) view.findViewById(R.id.SignUpRegister);
    SignUpRegister.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));

    referCode = (EditText) view.findViewById(R.id.referCode);
    referCode.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));


}

private void showpDialog() {
    pDialog = ProgressDialog.show(activity, null, null);
    final Drawable d = new ColorDrawable(Color.TRANSPARENT);
    pDialog.getWindow().setBackgroundDrawable(d);
    pDialog.setContentView(R.layout.loader);
    pDialog.show();
}

private void hidepDialog() {
    if (pDialog != null &amp;&amp; pDialog.isShowing())
        pDialog.dismiss();
}


@Override
public void onErrorResponse(VolleyError volleyError) {
    hidepDialog();
    SignUpRegister.setEnabled(true);

}


@Override
public void onResponse(JSONObject jsonObject) {
    hidepDialog();

    SystemUtil.sysOut("register::::: " + jsonObject.toString());
    if (JSONUtil.readBoolean(jsonObject, "status")) {
        int userId = JSONUtil.readInt(jsonObject, "user_id");

        showVerifyMobileScreen(userId);
    }else{
        SignUpRegister.setEnabled(true);
    }


    if (jsonObject.has("message"))
        Toast.makeText(activity, JSONUtil.readString(jsonObject, "message"), Toast.LENGTH_LONG).show();


}
</code></pre>

<p>VerificationFragment.Java</p>

<pre><code>SignUpActivity activity;
TextView verifyTitleOne, verifyTitleTwo, verifyChnageNumber, verificationResend;
EditText SignUpMobile, VerficationCode;
Button VerificationRegister;
int userId;
String mobile;
boolean isResend;


public VerificationFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    if (bundle != null) {
        userId = bundle.getInt("userId");
        mobile = bundle.getString("mobile");
    }

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_verfication, container, false);
    activity.setTempTitle("details");
    inItViews(view);
    SignUpMobile.setText("" + mobile);
    VerificationRegister.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!VerficationCode.getText().toString().trim().isEmpty()) {
                Toast.makeText(activity, "Verifying", Toast.LENGTH_LONG).show();
                showpDialog();
                getDefaultData();

            } else {
                VerficationCode.setError("Enter Code");
            }
        }
    });

    verificationResend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!isResend) {
                isResend = true;
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("phone_number", mobile);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                requestServer(jsonObject, AppConstants.RESEND_OTP_URL);
            }else
                Toast.makeText(activity,"Please Wait",Toast.LENGTH_SHORT).show();
        }
    });
   // setvalues();
    return view;
}

private void setvalues() {
    VerficationCode.setText("51692");
    VerificationRegister.performClick();

}

private void getDefaultData() {
    JSONObject jsonObject = new JSONObject();

    try {
        jsonObject.put("user_id", userId);
        jsonObject.put("token", VerficationCode.getText().toString());
    } catch (JSONException e) {
        e.printStackTrace();
    }

   requestServer(jsonObject,AppConstants.VERIFY_NUMBER_URL);

}

private void requestServer(JSONObject jsonObject, String url) {
    RequestQueue requestQueue = CustomVolleyRequestQueue.getInstance(activity).getRequestQueue();


    final CustomJSONObjectRequest customJSONObjectRequest = new CustomJSONObjectRequest(Request.Method.POST, url, jsonObject, this, this);
    customJSONObjectRequest.setTag("verify");
    requestQueue.add(customJSONObjectRequest);

}


private void gotoSelectSizesPage(JSONObject jsonObject) {
    Bundle bundle = new Bundle();
    bundle.putString("output", jsonObject.toString());
    DoneFragment homeKitFragment = new DoneFragment();
    homeKitFragment.setArguments(bundle);
    activity.setTempTitle("");
    activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment, homeKitFragment).commit();
}

private void inItViews(View view) {
    verifyTitleOne = (TextView) view.findViewById(R.id.verifyTitleOne);
    verifyTitleOne.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    verifyTitleTwo = (TextView) view.findViewById(R.id.verifyTitleTwo);
    verifyTitleTwo.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    verifyChnageNumber = (TextView) view.findViewById(R.id.verifyChnageNumber);
    verifyChnageNumber.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    verificationResend = (TextView) view.findViewById(R.id.verificationResend);
    verificationResend.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    SignUpMobile = (EditText) view.findViewById(R.id.SignUpMobile);
    SignUpMobile.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    VerficationCode = (EditText) view.findViewById(R.id.VerficationCode);
    VerficationCode.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
    VerificationRegister = (Button) view.findViewById(R.id.VerificationRegister);
    VerificationRegister.setTypeface(Typefaces.get(activity, AppConstants.FONT_ROBOT_REGULAR));
}


BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle intentExtras = intent.getExtras();
        if (intentExtras != null) {
            Object[] sms = (Object[]) intentExtras.get("pdus");
            String smsMessageStr = "";
            for (int i = 0; i &lt; sms.length; ++i) {
                SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i], "3gpp");

                smsMessageStr = smsMessage.getMessageBody().toString();
               // String address = smsMessage.getOriginatingAddress();

                /*smsMessageStr += "SMS From: " + address + "\n";
                smsMessageStr += smsBody + "\n";*/
            }
            VerficationCode.setText("" + stripNonDigits(smsMessageStr));

            VerificationRegister.performClick();

        }

    }
};


private ProgressDialog pDialog;

private void showpDialog() {
    pDialog = ProgressDialog.show(activity, null, null);
    final Drawable d = new ColorDrawable(Color.TRANSPARENT);
    pDialog.getWindow().setBackgroundDrawable(d);
    pDialog.setContentView(R.layout.loader);
    pDialog.show();
}

private void hidepDialog() {
    if (pDialog != null &amp;&amp; pDialog.isShowing())
        pDialog.dismiss();
}


public static String stripNonDigits(
        final CharSequence input) {
    final StringBuilder sb = new StringBuilder(
            input.length());
    for (int i = 0; i &lt; input.length(); i++) {
        final char c = input.charAt(i);
        if (c &gt; 47 &amp;&amp; c &lt; 58) {
            sb.append(c);
        }
    }
    return sb.toString();
}


@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    this.activity = (SignUpActivity) activity;
    activity.registerReceiver(broadcastReceiver, new IntentFilter(
            AppConstants.BROADCAST_ACTION_SMS));
}

@Override
public void onDetach() {
    super.onDetach();
    activity.unregisterReceiver(broadcastReceiver);
}


@Override
public void onErrorResponse(VolleyError volleyError) {

    hidepDialog();
}

@Override
public void onResponse(JSONObject jsonObject) {
    hidepDialog();
    isResend=false;
    if (jsonObject.has("message"))
        Toast.makeText(activity, JSONUtil.readString(jsonObject, "message"), Toast.LENGTH_LONG).show();
    if (JSONUtil.readBoolean(jsonObject, "status") &amp;&amp;jsonObject.has("user_data")) {
        gotoSelectSizesPage(jsonObject);
    }
  }
}                       
</code></pre>