Skip to content

Instantly share code, notes, and snippets.

@NunciosChums
Created March 7, 2017 02:56
Show Gist options
  • Save NunciosChums/a45ca534cc109271f34e6c992f69f048 to your computer and use it in GitHub Desktop.
Save NunciosChums/a45ca534cc109271f34e6c992f69f048 to your computer and use it in GitHub Desktop.
EditText validation in MVVM
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="model"
type="kr.changhoonjin.testmvp.view_models.Second"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="kr.changhoonjin.testmvp.views.SecondActivity">
<EditText
android:id="@+id/edit_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textPersonName"
android:text="@{model.name}"
app:addTextChangedListener="@{model.nameWatcher}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
<EditText
android:id="@+id/edit_email"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:text="@{model.email}"
app:addTextChangedListener="@{model.emailWatcher}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_name"/>
<RatingBar
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rating="@{model.score}"
android:stepSize="1"
app:OnRatingBarChangeListener="@{model.ratingBarChangeListener}"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_email"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="@{model.isValid}"
android:onClick="@{()->model.publish()}"
android:text="게시"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rating"/>
</android.support.constraint.ConstraintLayout>
</layout>
package kr.changhoonjin.testmvp.view_models;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.util.Patterns;
import android.widget.RatingBar;
public class Second implements ViewModel {
public final ObservableField<String> name = new ObservableField<>();
public final ObservableField<String> email = new ObservableField<>();
public final ObservableInt score = new ObservableInt();
public final ObservableBoolean isValid = new ObservableBoolean();
@Override public void onCreate() {
score.set(0);
isValid.set(false);
}
@Override public void onResume() {}
@Override public void onPause() {}
@Override public void onDestroy() {}
private void validation() {
boolean isValidName = !TextUtils.isEmpty(name.get());
boolean isValidEmail = !TextUtils.isEmpty(email.get()) && Patterns.EMAIL_ADDRESS.matcher(email.get()).matches();
boolean isValidScore = score.get() > 0;
isValid.set(isValidName && isValidEmail && isValidScore);
}
public void publish() {
Log.w("==== [App] === Second | publish", "|" + "publish");
}
/***************************
* listener
***************************/
public TextWatcher nameWatcher() {
return new TextWatcher() {
@Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
name.set(charSequence.toString());
}
@Override public void afterTextChanged(Editable editable) {
validation();
}
};
}
public TextWatcher emailWatcher() {
return new TextWatcher() {
@Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
email.set(charSequence.toString());
}
@Override public void afterTextChanged(Editable editable) {
validation();
}
};
}
public RatingBar.OnRatingBarChangeListener ratingBarChangeListener = (ratingBar, rating, fromUser) -> {
score.set((int) rating);
validation();
};
}
@Bajranghudda
Copy link

how can i call set error?

@bsvanli
Copy link

bsvanli commented May 23, 2018

Bad code

@anupamchugh
Copy link

Your ViewModel contains Android packages. TextWatchers code! What's the point of MVVM ?

@akshaybhange
Copy link

@anupamchugh I've seen people doing the same stuff for focus change listener too. What's your way of doing this? Please share your ideas on how to maintain MVVM pattern & share your sample code too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment