Reputation: 129
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"
/>
Upvotes: 0
Views: 85
Reputation: 18489
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. :)
Upvotes: 0
Reputation: 662
//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);
}
Upvotes: 1
Reputation: 3751
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
Upvotes: 1
Reputation: 1971
you can do something like this-
boolean isTextNotEmpty=validate();
if(isTextNotEmpty){
btnRegistrarse.setEnabled(true);
}
Upvotes: 0