Android: Validation symbol in EditText - android

We have the setError method for setting an error message with a red exclamation symbol at the end of the box if validation of an EditText returns false:
I want to set a green tick symbol, just like the red exclamation at the end of the box when my validation returns true:
password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (password.getText().toString().length() < 6) {
password.setError("Password should be greater than 6 characters!");
}
else {
//validation is true, so what to put here?
}
}
}
});
}
EDIT 1
Seemingly impossible, but there's more to the problem. I did this:
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String mail = email.getText().toString();
if(!android.util.Patterns.EMAIL_ADDRESS.matcher(mail).matches()) {
email.setError("Please enter a valid email address");
}
else {
Log.i("yay!","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
}
}
}
});
Though I can see in my logs that yay: Email is valid!!!, I can't see the validation symbol at the end of the EditText.
However, to my surprise when I change the if statement to always false, I can see both the symbol with the log statement.
Any explanations upon why could this be happening?

You can show drawableRight when your validation is true.
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.rightImage, 0);
and set it back to normal when validation is false.
password.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

I just tested it out in my own project using setError(CharSequence error, Drawable icon)
Get a new Icon:
Go to my drawable folder
adding a new vector asset.
I picked "material icon" and browse,
then picked ic_done_24pp.
Color:
Next, I went into the xml and made it green by changing the fillcolor:
android:fillColor="#FF00FF00"
3: Change code:
Drawable myIcon = getResources().getDrawable(R.drawable.ic_done_24dp);
myIcon.setBounds(0, 0, myIcon.getIntrinsicWidth(), myIcon.getIntrinsicHeight());
mPasswordView.setError("Good", myIcon);

Related

setCompoundDrawablesWithIntrinsicBounds is not working properly

I've an email field as EditText. I'm trying to add a green-tick icon at the end of the text field when the validation is true, and setError when it is false.
Here's the piece of code I'm using right now:
email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String mail = email.getText().toString();
if(!android.util.Patterns.EMAIL_ADDRESS.matcher(mail).matches()) {
email.setError("Please enter a valid email address");
}
else {
Log.i("YaY","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);
}
}
}
});
PROBLEM:
Though I can see the log Yay: Email is valid!!!, it seems no icon is set as I can't see it.
But when I change the if-condition to false, which means setError will never be called, I can see the log as well as the icon.
Any explanations upon why I'm seeing this strange behavior? What am I missing?
try removing the icon from xml if you are setting any
and set both images from the code
for some reason the image does not refresh if you set one from xml
and use
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
numTxt.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
} else {
numTxt.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);
}
I am not sure if this is a bug but I am able to workaround this by setting the drawable to zero (0) first before assigning the new drawable.
In your case, you can try the following:
Log.i("YaY","Email is valid!!!");
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
email.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.validated, 0);

Drawable right in edit text not updating after error

Edit text in android doesn't allow to change draw able after setError.
I have used drawable right for password field but if error comes in password field it won't allow to change draw able after it. before error it works fine.
<EditText
android:id="#+id/edt_reg_password"
style="#style/editText_full_view"
android:layout_height="wrap_content"
android:layout_below="#id/edt_reg_email"
android:layout_marginTop="#dimen/padding_normal"
android:drawableLeft="#mipmap/ic_action_password"
android:drawableRight="#mipmap/ic_action_password_visibility"
android:drawablePadding="#dimen/padding_normal"
android:hint="#string/hint_password"
android:inputType="textPassword"
android:maxLength="25"
android:paddingLeft="#dimen/padding_normal"
tools:visibility="visible" />
Java code for changing eye icon run time
private void setPasswordDrawable()
{
final Drawable showpass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility);
final Drawable hidepass_icon = getResources().getDrawable(R.mipmap.ic_action_password_visibility_off);
final Drawable pass_drawable = getResources().getDrawable(R.mipmap.ic_action_password);
pass_drawable.setBounds(0, 0, pass_drawable.getIntrinsicWidth(), pass_drawable.getIntrinsicHeight());
//edtPassword.setCompoundDrawables(pass_drawable, null, showpass_icon, null);
edtPassword.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (edtPassword.getCompoundDrawables()[2] == null) {
return false;
}
if (event.getAction() != MotionEvent.ACTION_UP) {
return false;
}
if (event.getX() > edtPassword.getWidth() - edtPassword.getPaddingRight()
- showpass_icon.getIntrinsicWidth()) {
if (isPasswordVisible) {
runOnUiThread(new Runnable() {
#Override
public void run() {
//edtPassword.setError(null);
edtPassword.setTransformationMethod(
PasswordTransformationMethod.getInstance());
edtPassword.setSelection(edtPassword.getText().length());
showpass_icon.setBounds(0, 0, showpass_icon.getIntrinsicWidth(), showpass_icon.getIntrinsicHeight());
edtPassword.setCompoundDrawables(pass_drawable, null, showpass_icon, null);
}
});
isPasswordVisible = false;
} else {
runOnUiThread(new Runnable() {
#Override
public void run() {
//edtPassword.setError(null);
edtPassword.setTransformationMethod(
HideReturnsTransformationMethod.getInstance());
edtPassword.setSelection(edtPassword.getText().length());
hidepass_icon.setBounds(0, 0, hidepass_icon.getIntrinsicWidth(), hidepass_icon.getIntrinsicHeight());
edtPassword.setCompoundDrawables(pass_drawable, null, hidepass_icon, null);
}
});
isPasswordVisible = true;
}
}
return false;
}
});
}
For setting error
public void setViewError(View view, String message)
{
if (view instanceof EditText) {
((EditText) view).setError(message);
}
}
You may use this like -
if(error=true){
editText.setCompoundDrawablesWithIntrinsicBounds(
0, 0,R.drawable.ic_error, 0);
editText.setCompoundDrawablePadding(5);}
else{
editText.setCompoundDrawablesWithIntrinsicBounds(
0, 0,R.drawable.ic_corrct, 0);
editText.setCompoundDrawablePadding(5);}
It may be very late. Me too faced these kind of issue. And I frustrated to find the solution. Finally I found the solution. It may helpful someone like me. The solution is very simple.
EditText stores the errors whenever we set the error and it didn't remove automatically even after entering the correct value. So you need to manually override the error text and set empty value.
if (!isError) {
edt_reg_password.setError("");
edt_reg_password.setText(edt_reg_password.getText().toString());
} else {
edt_reg_password.setError("Data Invalid");
}
Thanks ! Happy Coding !
The right drawable and error drawable of the TextView (or EditText) are displayed based on the same field.
When the error drawable is showing, the right drawable will be stored in a temporary variable and then restored when the error disappears.
So you can't change the right drawable when the error is showing.
But there is a problem with the code in TextView: the temporary variable (right drawable) will not be clear after restoration.
And once the temporary variable exists, even if you set the right drawable with a new value, the temporary variable will be display anyway.
This is why right drawable not change after setError(): it keep showing the previous value.
All this happens in TextView#Drawables.
Unfortunately there is no direct way to that clear temporary variables, but this can be done indirectly by set TextView#mDrawables to null. Then, you can set the right drawable as you want.
// Clear error first
editText.setError(null);
// Clear TextView#Drawables
editText.setCompoundDrawables(null, null, null, null);
// Set right drawable
editText.setCompoundDrawables(null, null, drawableRight, null);

Android: EditText transparency change on click and text entry

How can I make an EditText (both the underline and android:hint) have a 50% transparency when there is no text inputted and it isn't clicked on, then change to 100% when it is either clicked on or has inputted text?
In addition, how can I change the color of the underline (not hint) to orange when the transparency is 100% (when it is clicked or text has been inputted)?
Use the following code
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If has focus then change the alpha to 1
mEditText.setAlpha(1);
// Set orange color filter
mEditText.getBackground().setColorFilter(Color.argb(255, 255, 165, 0), PorterDuff
.Mode.SRC_IN);
} else {
// When focus is lost, check if some text has been entered.
// If not then set the alpha to 0.5f (50% transparent)
if (TextUtils.isEmpty(mEditText.getText().toString())) {
mEditText.setAlpha(0.5f);
// Clear the color filter
mEditText.getBackground().clearColorFilter();
}
}
mEditText.invalidate();
}
});

Change EditText value from text to password and vice versa on ToggleButton checked

I am studying programming in Android and I'm hitting an issue. I have this toggle button and when I input a text and the button is on, the text is starred (passworded, call it whatever you want) and when the button is off, the text is a text. However, I am hitting an issue and I don't see the problem.
The block of code looks like:
toggleCommand.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(toggleCommand.isChecked()){
input.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
I don't see the problem. Can you tell me what did I do wrong and explain?
When I turn on the application.. I type something in and it's passworded. I press the button to uncheck it and the passworded text turns into a text. I press the button again and instead of getting passworded again, the text stays normal.
Here is the working code:
toggleCommand.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
if (toggleCommand.isChecked())
{
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
else
{
input.setInputType(InputType.TYPE_CLASS_TEXT);
}
}
});
More info: http://thenewboston.org/watch.php?cat=6&number=27
Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa
edit: Further explanation on setInputType here: http://developer.android.com/reference/android/text/InputType.html
Try this:
toggleCommand = (ToggleButton)findViewById(R.id.the_id_of_your_togglebutton) ;
toggleCommand.setOnCheckedChangeListener( new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton toggleButton, boolean isChecked)
{
if(isChecked)
{
input.setTransformationMethod(PasswordTransformationMethod.getInstance());
}
else
{
input.setTransformationMethod(null);
}
}
});
You can read more about it on the documentation.
UPDATE
To make this kind of manipulation, you can use TransformationMethod.
So, to set the text to a hidden password, you must use PasswordTransformationMethod, and to turn it to text again, you just set the TransformationMethod to null, this way, no transformation will be applied to the text.
Hope this helps you.

Dynamically added EditTexts and forcing the user to enter data on focus change

I have dynamically added 3 EditTexts on a TableLayout. Then on focus change through tab on emulator in Eclipse, I check if they are blank, if blank I need to set the focus to the EditText else it should move to the next.
The issue is that, focus moves to the next EditText and cursor allows entry into next field. Also both EditTexts get highlighted. Can someone help me with the proper way to change focus?
I though this would do the trick but it doesn't work. Again both the EditTexts are getting focussed as both are blank, causing focus listeners on both to be activated.
if(arrEdit.size()>0){
for(EditText etLot: arrEdit){
etLot.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
String msg = null;
EditText et = null;
if(!hasFocus){
for(int i=0;i< arrEdit.size();i++){
msg= ((TextView)arrTxtVw.get(i)).getText().toString();
System.out.println("Focus Changed on "+msg);
if((((EditText)v).length()>0)==false && ((EditText)v).getId()== arrEdit.get(i).getId()){
System.out.println("size is zero for "+msg);
(arrEdit.get(i)).requestFocus();
et=(arrEdit.get(i));
break;
}
}
TextView text = (TextView) lyout.findViewById(R.id.text);
text.setTextSize(15);
text.setText(msg+"cannot be blank");
Toast toast = new Toast(ctxref);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(1500);
toast.setView(lyout);
toast.show();
final EditText ettemp = et;
et.post(new Runnable() {
public void run() {
ettemp.requestFocus();
}
});
}
}
});
}
}

Categories

Resources