0

I'm trying to enable a button but first I want to check that the editText have content and then enable the button. Here is my code. What should be the best solution?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView =inflater.inflate(R.layout.fragment_sign_up_part_two, container, false);
    app();
    return rootView;
}

public void app(){
    etCorreo = (EditText)rootView.findViewById(R.id.tvCorreoUsuario);
    etPassword = (EditText)rootView.findViewById(R.id.tvPassword);
    btnRegistrarse = (Button)rootView.findViewById(R.id.btnRegistro);
    events();
}

public void events(){
    if(validate()){
        btnRegistrarse.setEnabled(true);
        btnRegistrarse.setClickable(true);
    }
}

public Boolean validate(){
    correo = etCorreo.getText().toString().trim();
    pass = etPassword.getText().toString().trim();

    if(correo.isEmpty()) return false;
    if(pass.isEmpty()) return false;

    return true;
}

And this is my xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_registrarse"
    android:id="@+id/btnRegistro"
    android:enabled="false"
    android:layout_gravity="center_horizontal"
    android:background="@color/background_color"
    android:layout_margin="10dp"
    />
Luis Sotelo
  • 129
  • 6

4 Answers4

1

You can use TextWatcher to monitor the changing of Text in EditText. Example :

textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        // check the changing of text and decide to enable/disable button here
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 

This question maybe is helpful

Community
  • 1
  • 1
ThaiPD
  • 3,503
  • 3
  • 30
  • 48
1
//try this code, you have enter any value edittext then button visible
@Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

        //check data equal or not or any other logic....
            login.setEnabled(true);



    }
Ramesh Prajapati
  • 654
  • 6
  • 10
0

Try to use afterTextChanged Method of edittext after adding addTextChangedListener. For example :

EditText editText = (EditText)findViewById(R.id.yourEditText);
    editText.addTextChangedListener(new TextWatcher(){
        public void afterTextChanged(Editable s) {
            if(s.toString().length() > 0)
                your_button.setEnable(true);
            else
                your_button.setEnable(false);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after){}
        public void onTextChanged(CharSequence s, int start, int before, int count){}
    });

Hope, it will help you. :)

Android Killer
  • 18,174
  • 13
  • 67
  • 90
0

you can do something like this-

boolean isTextNotEmpty=validate();
if(isTextNotEmpty){
btnRegistrarse.setEnabled(true);
}
RajSharma
  • 1,941
  • 3
  • 21
  • 34