Android setError(): error message visually pointing to the wrong field - android

I use setError() to show validation errors. In one case, the field is close to the left side of the screen, when the error message showed up, it visually pointed to the wrong EditText field - instead of pointing to the field showing 1234, it should really point to the blank field on the left, see attached screenshot. Does anyone know a solution to this? Thanks!
The screenshot was taken on a Galaxy 3 phone running Android 4.4.2.

I got one workaround, add \n to format the message and reduce the width of each line:
text1.setError("this field must\nbe 1 character\nin length");

This is the solution for your problem..
Try this..
EditText edt1=(EditText) findViewById(R.id.editText2);
EditText edt2=(EditText) findViewById(R.id.editText3);
EditText edt3=(EditText) findViewById(R.id.editText1);
// Reset errors.
edt1.setError(null);
edt2.setError(null);
edt3.setError(null);
String txt3 = edt3.getText().toString().trim();
String txt1 = edt1.getText().toString().trim();
String txt2=edt2.getText().toString().trim();
boolean cancel = false;
View focusView = null;
if (TextUtils.isEmpty(txt3)) {
edt3.setError(getString(R.string.error_field_required));
focusView = edt3;
cancel = true;
}
if (TextUtils.isEmpty(txt2)) {
edt2.setError(getString(R.string.error_field_required));
focusView = edt2;
cancel = true;
}
if (TextUtils.isEmpty(txt1)) {
edt1.setError(getString(R.string.error_field_required));
focusView = edt1;
cancel = true;
}
if (cancel) {
focusView.requestFocus();
} else {
//do the operations you want do here
}

Related

Do not change focus if edit text is empty

I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.
but in my case of 5 edit text its not working properly.
Please Help, Thanks.
Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1=editText1.getText().toString().trim();
String val2=editText2.getText().toString().trim();
String val3=editText3.getText().toString().trim();
String val4=editText4.getText().toString().trim();
String val5=editText5.getText().toString().trim();
if(TextUtils.isEmpty(val5)){
editText5.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText4.setFocusable(true);
}
if(TextUtils.isEmpty(val4)){
editText4.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
}else {
editText3.setFocusable(true);
editText1.setFocusable(false);
editText2.setFocusable(false);
editText4.setFocusable(false);
}
if(TextUtils.isEmpty(val3)){
editText3.requestFocus();
editText1.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText2.setFocusable(true);
}
if(TextUtils.isEmpty(val2)){
editText2.requestFocus();
}else {
editText1.setFocusable(true);
}
if(TextUtils.isEmpty(val1)){
editText1.requestFocus();
}
if(!TextUtils.isEmpty(val1) &&
!TextUtils.isEmpty(val2) &&
!TextUtils.isEmpty(val3) &&
!TextUtils.isEmpty(val4) &&
!TextUtils.isEmpty(val5) ){
Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
}
}
});
First check EditText empty,null or matches to some specific text
based on your requirement.
Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which
you want to do.
In else part, set things you want to set if, if or else if conditions false.
According to my understanding in your question, I wrote a code. try it.
final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);
if (et1.getText().toString().length() < 1) { //check EditText empty or not.
//do what you want to edit/change in EditText. like below.
et1.setFocusableInTouchMode(true);
et1.requestFocus();
} else if (et2.getText().toString().length() < 1) {
//Check for second EditText empty or not. Do this for all 5 edit texts.
et2.setFocusableInTouchMode(true);
et2.requestFocus();
}
else{
//if All EditTexts not empty. do what you want to edit/change in edittext.
et1.setFocusableInTouchMode(false);
et2.setFocusableInTouchMode(false);
}

conditional statement for getting TextView

I have to get a EditText object which is nothing but a search bar in app with text visible as Current Location, however if I've already made a search query with myText, there is no Current Location text visible and search bar shows myText.
I am writing the test cases using Robotium solo object.
How can i write a conditional statement to get the EditText despite of what text it shows. Something like
if !found solo.getText("Current Location")
search solo.getText("myText");
This is what I am doing currently
EditText text = (EditText) solo.getText("Current Location");
if(text == null){
text = (EditText) solo.getText("myText");
//my rest of the code goes here....
But this throws exception if Current Location is not present in the search bar.
junit.framework.AssertionFailedError: TextView with text: 'Current Location' is not found!
Please suggest the correct way.
Try with this code:
if(!solo.searchText("Current Location"))
assertTrue(solo.searchText("my Text"))
else
assertTrue(solo.searchText("Current Location"));
EditText view = (EditText) solo.getView(view1);
if(view == null){
view = (EditText) solo.getView(view2)
}
view.getText().toString();

How to reset the style of an edittext?

I have an edit text which I validate. If the data entered does not corespond to the format, I set the background to red, when it coresponds I set it back to light gray, but the rectangle disappears.
I was wondering if I could reset it's properties to their orignal values when the data entered has the correct format.
This is what i am doing now
EditText name = (EditText) findViewById(R.id.Name);
if (name.getText().length() < 1)
{
error = true;
unit.setBackgroundColor(Color.RED);
}
else
{
//instead of this line reset edittext properties
unit.setBackgroundColor(Color.LTGRAY);
}
You can use PorterDuff instead - it has a clear method: http://developer.android.com/reference/android/graphics/PorterDuff.Mode.html.
To set the color:
name.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
To remove the color:
name.getBackground().clearColorFilter();
You can try:
editText.setText("");
I'd recommend using the setError() method, which displays an error message above the EditText and removes it automatically when the contents of the EditText change. This is a standard way to show the user that the validation has failed.
You are changing the EditText background so you are overriding android's default background.
you need to manually change it to some other background.
you can save it by Using the getBackground() method in EditText.
You can save the status of the editText before to change it, and restore when you need:
private Drawable originalDrawable;
EditText name = (EditText) findViewById(R.id.Name);
if (name.getText().length() < 1) {
if (originalDrawable == null) {
originalDrawable = unit.getBackground();
}
error = true;
unit.setBackgroundColor(Color.RED);
}
else {
//reset editText
text.setBackgroundDrawable(originalDrawable);
}

Toast message requiring user to fill in blank "EditText" fields

I am trying to get a message to appear when a button is clicked to tell the user to fill in the blank field. Currently, if the field is blank, it crashes/force closes the app. I tried to do the following code and had zero success. Originally I didn't have the if/else in there, I just ran the calculator(); method and the following imm code.
Could someone point me into the right direction?
public void onClick(View v)
{
if ((EditText)findViewById(R.id.amount1)== null)
{
Context context = getApplicationContext();
CharSequence text = "Enter a number";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
else
{
calculator();
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
Im pretty sure this is the bad code:
if ((EditText)findViewById(R.id.amount1)== null)
Just dont know how to word it the way I want.
Try checking the length of the text in the EditText widget
EditText e = (EditText)findViewById(R.id.amount1));
if(e.getText().length == 0){
//Show Toast
}else{
//continue your code
}
Use this code.
EditText et = (EditText)findViewById(R.id.amount1));
if(et1.getText().length() == 0){
//Display toast here
} else{
//Your code
}
EditText text = (EditText)findViewById(R.id.amount1);
if(TextUtils.isEmpty(text.toString())) {
// show toast
}
Even if the field is blank, the edittext is not null. Use:
EditText editText = (EditText)findViewById(R.id.amount1);
String text = new String(editText.getText());
if (test.equals("")) {
//...
((EditText)findViewById(R.id.amount1)== null is just getting a reference to the EditText with the id amount1, it is not checking to see if that EditText has a valid entry.
To see if the EditText has text, you can get the String it holds by via EditText#getText().toString()
To make this work, first store the reference to the EditText in a var, then perform your checks on the String:
EditText et = (EditText)findViewById(R.id.amount1);
String amount1 = et.getText().toString();
if (amount1.equals("")) {
// Do your stuff here
}
I'm using local variables and just assuming you want the string to have content. You will likely need to do other checks to handle all the cases (like malformed input). Some of this you can reduce by setting the inputType on the EditText. For example, you might set it to numberDecimal if you are trying to handle only decimal numbers.
You actually want to check if the contents of the EditText are null or an empty string.
The line in question should look something like this:
if("".equals(((EditText)findViewById(R.id.amount1)).getText().toString()))
Of course you may want to break that statement up into more lines to make it a bit more readable!

how to take input from user in android

i have a EditText in android in which i want the user to enter the text and checks for the condition "BYE"
Code sample:
EditText text = (EditText)findViewById(R.id.EditText01);
String abc= text .getText().toString();
while( !(abc).equals("bye")){
abc = text.getText().toString();//user should enter the text from keyboard and the while loop should go and chech the condition.but not able to enter the text
//do some operation with abc
}
How can i make user to enter the text??The UI should wait for the text to be entered(something like we have InputStreamReader in java applications).
Very Simple:-
EditText text = (EditText)findViewById(R.id.EditText01);
String str = text.getText().toString();
now in str u will get string which is entered in EditText
I don't think you need a loop to do this. From your comment it looks like you also have an "Enter" button or something that you click to do the checking. Just set an onclicklistener and onclick you can make the edittext invisible (or un-editable), check is the edittext is equal to "BYE" and then do your actions might look something like this:
final EditText ET = (EditText) findViewById(R.id.EnterText);
Button B1 = (Button) findViewById(R.id.EnterButton);
B1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ET.setVisibility(View.INVISIBLE);
if(ET.getText().toString() == "BYE")
{
//do something if it is "BYE"
} else {
Context context = getApplicationContext();
CharSequence text = "Please enter BYE";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
ET.setVisibility(View.VISIBLE);
} });
Instead of running this check in an infinite loop, only run it on every onKeyUp of the EditText. You know, anyway, that the condition will only ever be fulfilled if the user actually enters something.

Categories

Resources