I have an activity with simple login page in it. On click of submit button i m setting an red border on email edit text and adding a textwatcher so that on textchanged listner i can change email edit text border from red to gray. Everything works fine untill i dont change my screen orientations. when i am in potrait mode, I set red border on email edit text it gets change when i rotate the screen seem textwacther getting called. How to preserve the state of editext so that if i set its border color red it should be set as red irrespective of screen rotation also ontextchanged it should be change back to gray.
below is my Actvity class:
public class MainActivity extends AppCompatActivity {
@BindView(R.id.email)
EditText email;
@BindView(R.id.password)
EditText password;
@BindView(R.id.submit)
Button submit;
private MyViewModel myViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
myViewModel = ViewModelProviders.of(this).get(MyViewModel.class);
setUpTextWatcher(email,password);
myViewModel.setError().observe(this,getErrorObserver());
myViewModel.resetError().observe(this,getResetObserver());
}
private Observer<EditText> getResetObserver() {
return new Observer<EditText>() {
@Override
public void onChanged(@Nullable EditText editText) {
if(editText.getId() == email.getId()){
email.setBackgroundDrawable(getResources().getDrawable(R.drawable.input_shape));
}else{
password.setBackgroundDrawable(getResources().getDrawable(R.drawable.input_shape));
}
}
};
}
private Observer<EditText> getErrorObserver() {
return new Observer<EditText>() {
@Override
public void onChanged(@Nullable EditText editText) {
if(editText.getId() == email.getId()){
email.setBackgroundDrawable(getResources().getDrawable(R.drawable.red_input_shape));
}else{
password.setBackgroundDrawable(getResources().getDrawable(R.drawable.red_input_shape));
}
}
};
}
@OnClick(R.id.submit)
public void autenticate(){
myViewModel.setError().setValue(email);
}
private void setUpTextWatcher(EditText ...editTexts) {
for(final EditText editText: editTexts){
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(editText.hasFocus()){
myViewModel.resetError().setValue(email);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
}
below is Myview model class
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.widget.EditText;
public class MyViewModel extends ViewModel {
private MutableLiveData<EditText> setErrorOnEditText;
private MutableLiveData<EditText> resetErrorOnEditText;
public MutableLiveData<EditText> setError() {
if (setErrorOnEditText == null) {
setErrorOnEditText = new MutableLiveData<EditText>();
}
return setErrorOnEditText;
}
public MutableLiveData<EditText> resetError() {
if (resetErrorOnEditText == null) {
resetErrorOnEditText = new MutableLiveData<EditText>();
}
return resetErrorOnEditText;
}
}
below is my xml file for setting edit text background gray:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="2dip"
android:layout_height="2dip"
android:shape="rectangle" >
<corners
android:radius="2dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#fff"
android:startColor="#fff"
android:endColor="#fff"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="250dp"
android:height="50dp"
/>
<stroke
android:width="1dp"
android:color="@color/input_border"
/>
</shape>
below is my xml file for setting edit text background red:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="2dip"
android:layout_height="2dip"
android:shape="rectangle" >
<corners
android:radius="2dp"
/>
<gradient
android:angle="45"
android:centerX="35%"
android:centerColor="#fff"
android:startColor="#fff"
android:endColor="#fff"
android:type="linear"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="250dp"
android:height="50dp"
/>
<stroke
android:width="1dp"
android:color="@color/theme_color"
/>
</shape>