Skip to content

Commit 39f605a

Browse files
committed
Fixed Text Validation in SettingsFragment
1 parent 7a7d2ec commit 39f605a

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

source-code/app/src/main/java/org/buildmlearn/toolkit/fragment/SettingsFragment.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Activity;
44
import android.app.ProgressDialog;
5+
import android.content.Context;
56
import android.content.DialogInterface;
67
import android.content.Intent;
78
import android.content.SharedPreferences;
@@ -12,7 +13,6 @@
1213
import android.preference.PreferenceFragment;
1314
import android.preference.PreferenceManager;
1415
import android.support.v7.app.AlertDialog;
15-
import android.text.TextUtils;
1616
import android.view.LayoutInflater;
1717
import android.view.View;
1818
import android.widget.EditText;
@@ -116,27 +116,47 @@ public void resetUserName() {
116116
View dialogView = inflater.inflate(R.layout.dialog_settings_your_name, null);
117117
final EditText editInput = (EditText) dialogView.findViewById(R.id.et_dialog_settings_your_name);
118118
editInput.setText(prefUsername.getSummary());
119+
119120
final AlertDialog dialog =
120121
new AlertDialog.Builder(getActivity())
121122
.setTitle(R.string.title_user_name)
122123
.setView(dialogView)
123124
.setNegativeButton(R.string.dialog_no, null)
124-
.setPositiveButton(R.string.dialog_yes, new DialogInterface.OnClickListener() {
125-
@Override
126-
public void onClick(DialogInterface dialog, int which) {
127-
String enteredName = editInput.getText().toString();
128-
if (TextUtils.isEmpty(enteredName) || enteredName == null || !Character.isLetterOrDigit(((String) enteredName).charAt(0))) {
129-
Toast.makeText(getActivity(), R.string.valid_msg_name,Toast.LENGTH_LONG).show();
130-
}
131-
else {
132-
prefUsername.getEditor().putString(getString(R.string.key_user_name), enteredName).commit();
133-
prefUsername.setSummary(editInput.getText().toString());
134-
dialog.dismiss();
135-
}
125+
.setPositiveButton(R.string.dialog_yes, null).create();
136126

137-
}
138-
}).create();
139127
dialog.show();
128+
129+
dialog.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
130+
@Override
131+
public void onClick(View v) {
132+
133+
if (validated(editInput)) {
134+
String enteredName = editInput.getText().toString();
135+
prefUsername.getEditor().putString(getString(R.string.key_user_name), enteredName).commit();
136+
prefUsername.setSummary(editInput.getText().toString());
137+
dialog.dismiss();
138+
}
139+
140+
}
141+
});
142+
}
143+
144+
private boolean validated(EditText editInput) {
145+
if (editInput == null) {
146+
return false;
147+
}
148+
149+
String authorText = editInput.getText().toString().trim();
150+
Context mContext = getActivity();
151+
152+
if ("".equals(authorText)) {
153+
editInput.setError(mContext.getString(R.string.valid_msg_name));
154+
return false;
155+
} else if (!Character.isLetterOrDigit(authorText.charAt(0))) {
156+
editInput.setError(mContext.getString(R.string.title_valid));
157+
return false;
158+
}
159+
return true;
140160
}
141161

142162
@Override

0 commit comments

Comments
 (0)