1

I have two edit text namely mobile and password,when activity is visible mobile edit text is focusable while finishing Input in mobile number I want to check with validation and on pressing Keyboard back keyboad gets closed and want to be focus on password edit text and do the same process with password also .how can I do that. code:-

private void init() {
    m_MainLayout = (LinearLayout) findViewById(R.id.mainLayout);
    /*check whether SoftKeyPad open or hide*/
    m_MainLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = m_MainLayout.getRootView().getHeight() - m_MainLayout.getHeight();
            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

            } else {/*if hide then clear focus from Password Edit text*/
                m_InputPassword.clearFocus();
                checkFieldsForEmpty();
            }
        }
    });
    m_LoginBtn = (AppCompatButton) findViewById(R.id.btn_Login);// finding Id login botton

    m_InputMobile = (EditText) findViewById(R.id.input_mobile);// finding Id of Mobile Number edit text
    /*set focus change listener on Mobile Number Edit text*/
    m_InputMobile.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {

            /*if mobile Edit text looses focus then....*/
            if (!hasFocus) {
                isValidMobile();// initalize method
            }
        }
    });
    /* set Editor action listener on Mobile number Edit text*/
    m_InputMobile.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            /*if after input enter next keyWord*/
            if (actionId == EditorInfo.IME_ACTION_NEXT) {
                m_InputPassword.requestFocus();// then Request Password Edi text field to focus
            }
            return false;
        }
    });
    m_InputPassword = (EditText) findViewById(R.id.input_password);// finding Id of assword editText
    m_InputPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);// defining password edit Tect Input type
    m_InputPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
    /*set Foucs change listener on password Edit text*/
    m_InputPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            /*if password field loose focus*/
            if (!hasFocus) {

                isValidPassword();

            }

        }
    });
    /*Adding Editor action listener*/
    m_InputPassword.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_NEXT) {
                m_InputPassword.clearFocus();
                checkFieldsForEmpty();
            }
            return false;
        }
    });

}

/*validation for mobile number*/
private void isValidMobile() {// validation for mobile number
    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit text
    if (s_szMobileNumber.length() > 7 && s_szMobileNumber.length() < 15) {
        checkFieldsForEmpty();
    } else {
        m_InputMobile.setError("Mobile number must be between 7 to 15 characters long");
    }

}

/*validation for password*/
private void isValidPassword() {// validation for password
    s_szPassword = m_InputPassword.getText().toString().trim();// get password from edit text
    if (s_szPassword.length() >= 4 && s_szPassword.length() <= 8) {
        checkFieldsForEmpty();
    } else {
        m_InputPassword.setError("Password must be between 4 to 8 characters long");
    }
}

/*This method check Edit text is empty or not*/
private void checkFieldsForEmpty() {// this method check Edit text is empty or not
    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit text
    s_szPassword = m_InputPassword.getText().toString().trim();
    if (NetworkUtil.isConnected(getApplicationContext())) {
        // if mobile number and password are Emoty
        if (s_szMobileNumber.length() > 7 && s_szMobileNumber.length() < 15) {// check if mobile and password is empty ..
            if (s_szPassword.length() >= 4 && s_szPassword.length() <= 8) {
                m_LoginBtn.setEnabled(true);// make Login button disabled
                m_LoginBtn.setBackgroundColor(Color.rgb(0, 80, 147));// set background color on eabled
                m_LoginBtn.setOnClickListener(new View.OnClickListener() {// onclick listener on Login Button
                    @Override
                    public void onClick(View v) {
                        postLoginDataToServer();

                    }
                });
            } else {

                m_LoginBtn.setEnabled(false);// make login button enabled
                m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button

            }

        } else {
            m_LoginBtn.setEnabled(false);// make login button enabled
            m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button
        }

    } else {
        try {
            CSnackBar.getInstance().showSnackBarError(findViewById(R.id.mainLayout), "No Internet Connection Available", getApplicationContext());

        } catch (Exception e) {
            e.printStackTrace();
        }
        m_LoginBtn.setEnabled(false);
        m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));
    }


}
Siddharth
  • 181
  • 2
  • 11

0 Answers0