How to reset the style of an edittext? - android

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);
}

Related

My textview is not updating with black text

I am trying to set the text when I press the save button. But when I return to the view the text is faint grey.
Initial view on Message Settings
Updated view on Message Settings before I save
Updated view on Message Settings after I save and return to the view.
The source code for my SetupMessageFragment.java is here https://github.com/jackygrahamez/MayDay/blob/gradle2/app/src/main/java/com/mayday/md/fragment/SetupMessageFragment.java
There is a Log for the text retrieved from the sharedPreferences. I can see the new text vs the current textview text:
Fragment fragment2 = getChildFragmentManager().findFragmentById(R.id.sms_message);
((MessageTextFragment) fragment2).setActionButtonStateListener(bAction);
smsEditText = (EditText) fragment2.getView().findViewById(R.id.message_edit_text);
Log.e(">>>>", "onActivityCreated smsEditText "+smsEditText.getText().toString());
String currentMsg = SMSSettings.retrieveMessage(activity);
Log.e(">>>>", "onActivityCreated currentMsg "+currentMsg);
if(currentMsg != null) {
displaySettings(currentMsg);
}
Then the logcat
02-14 16:55:33.366 29043-29043/com.mayday.md E/>>>>﹕ onActivityCreated smsEditText I need IMMEDIATE help!
02-14 16:55:33.366 29043-29043/com.mayday.md E/>>>>﹕ onActivityCreated currentMsg I need IMMEDIATE help! Foo bar
Define black color in colors.xml (<color name="black" value="#000000"/>)
Set the Edit Text color to black.
smsEditText = (EditText) fragment2.getView().findViewById(R.id.message_edit_text);
smsEditText.setTextColor(getActivity().getResource().getColor(R.color.black));
Yo can edit the color in your colors.xml file, but as you want to use black color, which is already defined by android, you could just do this:
smsEditText = (EditText) fragment2.getView().findViewById(R.id.message_edit_text);
smsEditText.setTextColor(Color.BLACK);

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

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
}

making EditText not editable using if statement

I have to two EditTexts (editText1 and editText2 ) and I want the user to enter value inside editText1 first and then inside editText2, in another word I want to make editText2 not editable until the user enter something in editText1.
I tried but it didn't work:
EditText editText1 ;
EditText editText2 ;
String Str1 = editText1 .getText().toString();
if(Str1.matches(""))
{
editText2 .setKeyListener(null);
}
That might meet your requirements.
if(Str1.matches("")) {
editText2.setEnabled(false);
}else{
editText2.setEnabled(true); }
You should use setEnabled() method
if(Str1 == null || Str1.length() == 0)
{
editText2.setEnabled(false);
} else
{
editText2.setEnabled(true);
}
You should put that code into OnKeyListener for the first EditText. This will then enable and disable the second one based on the length of the text in the first one.
You can initially do editText2.setEnabled(false) and then when the user types something in the first editText, you can do editText.setEnabled(true).
You can disable any view element functionality using .setEnabled(false) method.
So, use
EditText.setEnabled(false);

setHint doesnt work with setInputType

I build EditText dynamically. Among other things, I set 2 properties: hint(.setHint) and inputType(.setInputType). My problem: when I invoke setInputType, setHint has no effect: blank edittexts remain blank with no hint. Once I comment out setInputType, I see all hints. I need both input type and hint. What to do? My code:
private EditText buildTextBox(Property property)
{
EditText control = new EditText(this);
control.setInputType(getInputTypeByPropertyInputType(property.getType()));// android.text.InputType.
control.setHint(property.getDisplayName());
return control;
}
private int getInputTypeByPropertyInputType(String type)
{
if (type.equals("integer"))
{
return android.text.InputType.TYPE_CLASS_NUMBER;
}
else
{
return android.text.InputType.TYPE_CLASS_TEXT;
}
}
#Eugene
Ensure you call control.SetHint() just before you call the control.setGravity() and control.setInputType(); and it works for me verrry much!
column1 = new EditText(this);
column1.setId(i);
column1.setHint("Value");
column1.setInputType(InputType.TYPE_CLASS_NUMBER);
column1.setGravity(Gravity.RIGHT);
I agree with Eugene.
Remove the gravity(just don't use CENTER) and the hint texts will come back as normal.
Nice find!

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!

Categories

Resources