-1

I have Login Screen in my app in which I made a method which enable and disable EditText button according to Internet availability scenario is something like that:- Description: The login/create an account button should get enabled when internet is available and disabled when internet is not available, this should be done in real time, currently it seems like internet availability is being checked on key press event. Now, considering two scenario

  1. We enter all the details and then enable the internet. Expected Result:login/create an account button should be enabled. Actual Result: login/create an account button remains disabled.

  2. Internet is available, We enter all the required details, the button gets enabled. Now, if the internet gets disabled Expected Result: Button should be disabled Actual Result: Button remains enabled

here is my code:-

public void checkFieldsForEmpty() {// this method check Edit text is empty or not
    m_LoginBtn = (AppCompatButton) findViewById(R.id.btn_Login);// finding Id login botton

    s_szMobileNumber = m_InputMobile.getText().toString().trim();// get mobile number from edit Text
    s_szPassword = m_InputPassword.getText().toString().trim();// get password from edit text
    // check mobile Internet connectivity
    if (NetworkUtil.isConnected(getApplicationContext())) {
        // if mobile number and password are Emoty
        if (s_szMobileNumber.equals("") || s_szPassword.equals("")) {// check if mobile and password is empty ..
            m_LoginBtn.setEnabled(false);// make Login button disabled
            m_LoginBtn.setBackgroundColor(Color.rgb(192, 192, 192));// color of login button
        } else {
            m_LoginBtn.setEnabled(true);// make login button enabled
            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 {
        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));
    }
John
  • 7
  • 7
  • You can try making a service which keeps track on when is internet is available or not. As soon as there is change, the service will notify your activity in turn and you can enable/disable your button accordingly. – Aradhna May 31 '16 at 04:58
  • PROBLEM IS THAT WHEN i DIABLE iNTERNET lOGIN BUTTON IS STILL ENABLE – John May 31 '16 at 05:00
  • check this http://stackoverflow.com/questions/15698790/broadcast-receiver-for-checking-internet-connection-in-android-app – Bharatesh May 31 '16 at 05:32

2 Answers2

1

You should register a broadcast receiver with the filter:
android.net.conn.CONNECTIVITY_CHANGE

Then you will be notified each time your network state changes. Just check if you have connection inside your reciever, and change you button state accordingly.

Check android developer website more for info: Monitor for Changes in Connectivity

Bharatesh
  • 8,943
  • 3
  • 38
  • 67
Shalev Moyal
  • 644
  • 5
  • 12
0

You should use setVisibilty methods in your code, below is the example, you can modify it to your need.

private void updateUI(boolean isOnline) {
    if (isOnline) {
        findViewById(R.id.sign_in_button).setVisibility(View.GONE);
        findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);
    } else {
        mStatusTextView.setText(R.string.signed_out);

        findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);
        findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);
    }
}

you can see how to check internet connection here. http://yasirameen.com/2016/02/check-network-connection-available/