-1

Reququirement :

There are two EditText one is Username and another is Password. I am checking on both EditText has more than one character than button of Signup should enable.

Can anyone pls suggest me ?

etUsername.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                int userName = s.length();
                if(userName >=1){
                    btnSignup.setEnabled(true);
                }

            }
        });
        etPswd = (EditText) findViewById(R.id.etpswd);
        etPswd.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                int pswd = s.length();
                if(pswd >=1) {
                    btnsignup.setEnabled(true);
                }

            }
        });

etUsername.setOnKeyListener(this); 
etPswd.setOnKeyListener(this); 
private void trydisableLoginButton() 
{ 
if(etUsername.getText().toString().length() == 0 && etPswd.getText().toString().length() == 0)
{ btnLogin.setEnabled(false); 
} 
} 

@Override public boolean onKey(View v, int keyCode, KeyEvent event) 
{ 
if(keyCode == KeyEvent.KEYCODE_BACK) 
{
    trydisableLoginButton(); 
} 
   return false;
 }

Req. I am trying to disable the button on Back press of softKeyboard above code not working!

TexGeek
  • 143
  • 2
  • 9
  • Add addTextChangedListener for both and check for condition if EditText1 is blank or not also check EditText2 blank or not , if both are not blank then enable button – IshRoid Mar 11 '15 at 16:38
  • uname.addTextChangedListener(this); pswd.addTextChangedListener(this); and check on @Override public void afterTextChanged(Editable s) { int pswd = s.length(); if(pswd >=1) { signup.setEnabled(true); } Requirement : for two editText have the 1 character than only button should enable. I have to add two textwatcher and do this. Any other way pls suggest me. – TexGeek Mar 11 '15 at 16:46
  • post your current code, for other suggestion, your code will help to look into deep – IshRoid Mar 11 '15 at 16:55
  • I updated my answer replace your onKey code with backpressed i edited – IshRoid Mar 12 '15 at 12:21
  • @Ishrat: I am pressing the back button of softkeyboard Not the hardware key – TexGeek Mar 12 '15 at 13:04
  • means backspace of keyboard??? – IshRoid Mar 12 '15 at 13:48
  • Yes,backspace of keyboard. – TexGeek Mar 13 '15 at 04:27
  • if(keyCode == KeyEvent.KEYCODE_DEL) this will work for the back key of soft keyboard. – TexGeek Mar 13 '15 at 04:47
  • You should try http://stackoverflow.com/a/28719420/1881611 – IshRoid Mar 13 '15 at 05:03
  • http://stackoverflow.com/questions/29027953/back-key-or-delete-key-of-soft-keyboard-not-working-in-4-4-and-5-0?noredirect=1#comment46297608_29027953 @ Isharat can you look at this ? – TexGeek Mar 13 '15 at 09:12
  • I am writing working code you,,for referred question.Please wait – IshRoid Mar 13 '15 at 09:14
  • post the answer on above link @ishrat – TexGeek Mar 13 '15 at 09:43

1 Answers1

0

Create a function to enable signup button

 public void tryEnableSignUpButton(){

    if(etUsername.getText().toString().lenght>=1 && (etPswd .getText().toString().lenght>=1 )){

 btnSignup.setEnabled(true);
}

}

Now after each

            @Override
            public void afterTextChanged(Editable s) {
                int userName = s.length();
                if(userName >=1){
                   tryEnableSignUpButton();
                }

            }

@EDIT

@Override
    public void onBackPressed() {
      trydisableLoginButton(); 
      super.onBackPressed(); 
}
IshRoid
  • 3,696
  • 2
  • 26
  • 39
  • I am glad to know my sol work for you,Thanks for accepting my answer, upvote will be very appreciated. – IshRoid Mar 11 '15 at 17:40
  • onCreate() { etUsername.addTextChangedListener(this);} At this line I am getting the null poniter exception Did you know this ? 03-12 Error :java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginsample/com.example.loginsample.MainActivity}: java.lang.NullPointerException – TexGeek Mar 12 '15 at 10:13
  • You must cast etUsername before adding listener, like etUsername=findViewByID(R.id.et_username); – IshRoid Mar 12 '15 at 10:16
  • etUsername = (EditText) findViewById(R.id.etusername); etUsername.addTextChangedListener(this); used this still problem I facing. – TexGeek Mar 12 '15 at 10:18
  • please post full log of exception or update your latest code of class – IshRoid Mar 12 '15 at 10:21
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUsername = (EditText) findViewById(R.id.etusername); etUsername.addTextChangedListener(this); etPswd.addTextChangedListener(this); etUsername.setOnKeyListener(this); etPswd.setOnKeyListener(this); btnLogin = (Button) findViewById(R.id.btnLogin); } – TexGeek Mar 12 '15 at 10:25
  • Caused by: java.lang.NullPointerException 03-12 10:16:09.680: E/AndroidRuntime(16108): at com.example.loginsample.MainActivity.onCreate(MainActivity.java:23) – TexGeek Mar 12 '15 at 10:26
  • you not initilising password filed add this line also etPswd= (EditText) findViewById(R.id.pwd); – IshRoid Mar 12 '15 at 10:34
  • etUsername.setOnKeyListener(this); etPswd.setOnKeyListener(this); private void trydisableLoginButton() { if(etUsername.getText().toString().length() == 0 && etPswd.getText().toString().length() == 0){ btnLogin.setEnabled(false); } } @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK) { trydisableLoginButton(); } return false; } I am trying to disable the button – TexGeek Mar 12 '15 at 10:54
  • Friend ! It's too much in comments you should come to chat,,i will be there to help you,,,start chat now,,,also not post code in comment just update your question – IshRoid Mar 12 '15 at 11:06
  • I can not talk i have only 13 reputation. can someone upvote the question ? – TexGeek Mar 12 '15 at 11:34
  • Okay then, Update your answer not post your code in comment it is not understandable – IshRoid Mar 12 '15 at 11:41