SetError on EditText - android

I am working on an android app. I am having an EditText on which I have applied OnCllickListener().
EditText _input = new EditText(context);
_input.setSingleLine(true);
_input.setFocusable(false);
I am setting OnClickListener() on this EditText:
_input.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// OnClick work here
}
});
I have applied some validations on this field and calling setError() method on this EditText. It shows that red icon of error in the EditTextwhen the validation fails. But when I click on that error icon it executes the OnClickListener() on this EditTextand I am not able to see the error occurred.
Is there any other way to do so, so that I can handle both functions.
Thanks a lot in advanced !!!

You would check if currently the edit text is in an error state, within the onclick handler:
_input.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (view.getError() == null) {
// do not handle the click
return;
}
}
});

You can check for an error like like this:
if(view.getError() == null) return;
See the doc here
getError()
Returns the error message that was set to be displayed with
setError(CharSequence), or null if no error was set or if it the error
was cleared by the widget after user input.
Source

I have solved this with a simple solution. Just set text of editText to empty string before setting error and error message will show again like without onClickListener.

Related

android Custom Dialog title doesnot show

Im new at android app development so i was following a youtuber on making a Database, im doing same as he is, but problem is that i can make db successfully but custom dialog title doesnt show but error mesage & textview text is visible in my app.
Im attaching screenshot of successful code on youtube.
screenshot
btninsert.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean boolln=true;
try {
String firstname = firstnam.getText().toString();
String lstname = lastnam.getText().toString();
db1.insertname(firstname, lstname);
}catch (Exception ex){
boolln=false;
String error=ex.toString();
Dialog dl=new Dialog(Activity_DB.this);
dl.setTitle("Uncuceesful");
TextView tx=new TextView(Activity_DB.this);
tx.setText(error);
dl.setContentView(tx);
dl.show();
}finally {
if (boolln){
Dialog dl1=new Dialog(Activity_DB.this);
dl1.setTitle("succesful? YES");
TextView tx=new TextView(Activity_DB.this);
tx.setText("sucesss");
dl1.setContentView(tx);
dl1.show();
}
}
Solved this problem by adding AlerDialog.builder instead of using Dialog, now it works perfectly.
boolln=false;
String error=ex.toString();
AlertDialog.Builder dl=new AlertDialog.Builder(Activity_DB.this);
dl.setCancelable(true);
dl.setTitle("Uncuceesful");
dl.setMessage("No entry found. \n"+error);
dl.show()

Check if is it empty on edit text is not working

What i have is android app , and in one of activities i have two edit text with button, and i want either if one of them were empty when i click the button to preform a toast to tell the user to enter data, and if it was not empty i want it to open intent and do other thing , here is my code :
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(quantity.getText()==null){
Toast.makeText(FullImageActivity2.this,"please enter quantity",Toast.LENGTH_LONG).show();
}
else if(extra.getText()==null){
Toast.makeText(FullImageActivity2.this,"please enter extra",Toast.LENGTH_LONG).show();
}
else{
Quan=quantity.getText().toString();
name=itemId;
image=R.drawable.products;
Intent cart=new Intent(FullImageActivity2.this,CartList.class);
cart.putExtra("name", name);
cart.putExtra("quantity",Quan);
cart.putExtra("image", image);
Log.e("quan",Quan+"");
Log.e("name",name+"");
startActivity(cart);
}
}
});
But the weird thing that if they were empty , else is working !! which is not logic at all .. the validation on empty text is not empty , Why this is happening?? Help plz
Try to used .equals() method
if(quantity.getText().toString().trim().equals(""))
Try
if(editText.getText().toString().length()<1){
//do something
}
And of course you are mistaking null with "" which means no string but not null.
When Edittext is empty, getText() method returns empty string, not null.
Try:
if(quantity.getText().toString().equals("")){
Of course, if you want to avoid entering only spaces, use
if(quantity.getText().toString().trim().equals("")){

JSON omdb parsing

Movie_search is a text field got from the user and ive referenced it by using
movie_search= (EditText)findViewById(r.id.editText);
and same goes for the button too
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(NEWS,"IMDB LINK->:"+movie_search);
URL="http://www.omdbapi.com/?t="+movie_search+"";
Log.i(NEWS,"IMDB LINK->:"+URL);
new DownloadConfigurationTask().execute(URL);
}
});
the problem is the string doesnt seem to append to the url,
the log i get is
IMDB LINK->:movie_search
IMDB LINK->:http://www.omdbapi.com/?t=
instead of
IMDB LINK->:movie_search
IMDB LINK->:http://www.omdbapi.com/?t=movie_search
any ideas what might be the problem ?
use EditText.getText() for getting text entered by user and append it with URL instead of EditText instance. change it as:
URL="http://www.omdbapi.com/?t="+movie_search.getText()+"";

Android Instrumentation - Assert fails

I have a small activity that contains a textview and a password. I have written a small text to test this activity. But the assert on the last line is failing. Could someone please tell me why ?
loginScreenActivity=startActivity(mStartIntent, null, null);
assertNotNull(loginScreenActivity);
mInstrumentation.waitForIdleSync();
final EditText userName=(EditText)loginScreenActivity.findViewById(R.id.userName);
final EditText password=(EditText)loginScreenActivity.findViewById(R.id.password);
loginScreenActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
userName.requestFocus();
}
});
mInstrumentation.waitForIdleSync();
this.sendKeys(KeyEvent.KEYCODE_R);
this.sendKeys(KeyEvent.KEYCODE_SHIFT_LEFT);
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_F);
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_Q);
this.sendKeys(KeyEvent.KEYCODE_A);
this.sendKeys(KeyEvent.KEYCODE_T);
assertTrue(userName.getText().toString().equals("Rafaqat"));
From the sequence, I guess you are inputing "rAfarqat"?

EditText setError message does not clear after input

Ok so I only have a EditText field and a button, which when pressed triggers an AsyncTask.
EditText playerName = (EditText)findViewById(R.id.playerEditText);
if(playerName.getText().toString().length() == 0 )
playerName.setError("Player name is required!");
else {
// do async task
}
The problem is that the error message seems to stay up even after when I input valid text to search. Is there a way to remove the error as soon as the EditText is not empty?
In your else bracket, put playerName.setError(null), which will clear the error.
API documentation: "The icon and error message will be reset to null when any key events cause changes to the TextView's text."
Though it is not so - and therefore we can regard this as bug.
If you use inputType such as textNoSuggestions, textEmailAddress, textPassword, the error is unset after a character is typed. Nearly as documented but again not exactly - when you delete a character, error stays.
It seems, a simple workaround with addTextChangedListener and setError(null) can attain promised behavior.
Besides there are posts about icon losing on Android 4.2. So use with care.
Try this listener:
playerName.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable edt){
if( playerName.getText().length()>0)
{
playerName.setError(null);
}
}
If you want to hide the error message one way is you apply onclicklistener on the edit box and then
editTextName.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
editTextName.setError(Null)
}
});
Below code worked for me
#OnTextChanged(
value = R.id.editTextName,
callback = OnTextChanged.Callback.TEXT_CHANGED)
public void afterInput(CharSequence sequence) {
editTextName.setError(null);
editTextName.setErrorEnabled(false);
}
'
editTextName.setError(null) Will clear the error message.
editTextName.setErrorEnabled(false) Will remove additional padding.
Add a TextWatcher to your EditText and onError, show your error message using et.setError(errorMessage) else you can remove the error message and error icon like below.
// to remove the error message in your EditText
et.setError(null);
// to remove the error icon from EditText.
et.setCompoundDrawables(null, null, null, null);
This code worked for me.
textInputSetting(binding.emailEdt)
fun textInputSetting(view: TextInputLayout) {
view.apply {
this.editText!!.addTextChangedListener {
if (this.editText!!.text.isNotEmpty()) {
this.error = null
this.isErrorEnabled = false
}
}
}
}

Categories

Resources