Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2099)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/widget/EmptyAlertEditText.java

Issue 1187013002: Add a custome edittext that encapsulates the error message logic (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/android/java/res/values/attrs.xml ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/widget/EmptyAlertEditText.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/widget/EmptyAlertEditText.java b/chrome/android/java/src/org/chromium/chrome/browser/widget/EmptyAlertEditText.java
new file mode 100644
index 0000000000000000000000000000000000000000..aa13cfddddd68633a87a456174a1fd5c82330ced
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/widget/EmptyAlertEditText.java
@@ -0,0 +1,69 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.widget;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.support.v7.widget.AppCompatEditText;
+import android.text.Editable;
+import android.text.TextWatcher;
+import android.util.AttributeSet;
+
+import org.chromium.chrome.R;
+
+/**
+ * An EditText that shows an alert message when the content is empty.
+ */
+public class EmptyAlertEditText extends AppCompatEditText {
+
+ private String mAlertMessage;
+
+ /**
+ * Constructor for inflating from XML.
+ */
+ public EmptyAlertEditText(Context context, AttributeSet attrs) {
+ super(context, attrs);
+ final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.EmptyAlertEditText);
+ int resId = a.getResourceId(R.styleable.EmptyAlertEditText_alertMessage, 0);
+ if (resId != 0) mAlertMessage = context.getResources().getString(resId);
+ a.recycle();
+ }
+
+ @Override
+ protected void onFinishInflate() {
+ super.onFinishInflate();
+ 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) {}
+
+ @Override
+ public void afterTextChanged(Editable s) {
+ if (s.length() != 0 && getError() != null) setError(null);
+ }
+ });
+ }
+
+ /**
+ * Sets the alert message to be shown when the text content is empty.
+ */
+ public void setAlertMessage(String message) {
+ mAlertMessage = message;
+ }
+
+ /**
+ * Checks whether the content is empty. If empty, an alert message will be shown.
+ * @return Whether the content is empty.
+ */
+ public boolean validate() {
+ if (getText().length() == 0) {
+ setError(mAlertMessage);
+ return true;
+ }
+ return false;
+ }
+}
« no previous file with comments | « chrome/android/java/res/values/attrs.xml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698