Button btnSearch = (Button) pnlView.findViewById(R.id.btnSearch);
btnSearch.setOnClickListener(new OnClickListener() {#
Override
public void onClick(View v) {
results.clear();
if (txtPatientID.getText().equals("") && txtFirstName.getText().equals("")
&& txtLastName.getText().equals("") && txtDOB.getText().equals("")
&& cboGender.getSelectedItem().equals("")) {
Toast.makeText(getActivity(),
"No criteria added", Toast.LENGTH_LONG).show();
return;
}
new Async().execute();
}
});
I have four text field and a spinner, when the user clicks search button, i need to display a message saying no criteria added. Its working, but i am not sure whether i am following the right approach in checking for empty value in textfield and spinner.
You should check the length instead of comparing with empty value. Because it will fail in one or more cases. you should check like this
if(txtPatientID.getText().toString().trim().length()==0)
trim() method will remove one or more spaces in your text
This seems okay. But you can trim() the getText() results to handle empty space characters.
Related
I want to perform many task in one Edittext. Like when I edit the first number in edittext it store in string value and edittext may be null after that when I enter 2nd number in same edittext 2nd number may also save in another string. Then after some action perform answer show in Textview.
Please help me out...
private void save_on_cick_listen(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(save == 0){
// save first string
editText.getText().clear(); //or you can use editText.setText("");
save+;
} else if (save == 1){
// save second string
editText.getText().clear(); //or you can use editText.setText("");
save+;
}
}
});
}
I am trying to disable the button after it is clicked. Th app doesn't crash it just doesn't disable it. wondering could anyone help me out?
here is the on click method for the button i am trying to disable.
//Submit button for answer
final Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
EditText answerA = (EditText) findViewById(R.id.answerA);
String toCompare = answerA.getText().toString();
TextView score = (TextView)findViewById(R.id.score_text_a);
scoreKeeper scoremgr = new scoreKeeper();
//anaswer to input
if(toCompare.matches("Alligator") || toCompare.matches("alligator") ||
(toCompare.matches("Alligator ") || toCompare.matches("alligator "))) {
//adds to score if inout matches one of the above
scoremgr.addToScore();
score.setText("Your score is " +Integer.toString(scoremgr.checkScore()));
//calls the next letter class
Intent intent_b = new Intent(button_a.this, button_b.class);
startActivity(intent_b);
//displays a toast message if correct
Toast.makeText(button_a.this, "Well Done, You Got it Right", Toast.LENGTH_SHORT).show();
submit.setEnabled(false);
}else{
//displays a toast meaasge if wrong
Toast.makeText(button_a.this, "Wrong Answer, Try Again", Toast.LENGTH_SHORT).show();
}
}
});
You put your "setEnabled" under an if statement. Just put it before. If that's a feature, check if the correct statement is executed, the condition is probably invalid, resulting the button not being disabled.
In first place, check if your code are being executed, you are doing it right, also you can try to use to disable your button:
btn.setEnabled(false);
btn.setClickable(false);
Also, change your Button variable to an instance variable without final
and access it inside your onClick method.
If you want to remove the button from your layout, you can change the visibility on it:
btn.setVisibility(View.GONE):
in your xml file set
android:clickable="true"
and now
btn.setEnabled(false);
btn.setClickable(false);
and check your (if condition ) does it returns true or not
I have an EditText and a Button.
I want if an EditText was empty when clicked on my Button. I want to show message as a toast, like "please enter a number".
You can do something like this:
boolean hasValue = editText.getText().length() > 0;
or
boolean hasValue = !editText.getText().toString().isEmpty();
or to make sure it doesn't contain only spaces:
boolean hasValue = !editText.getText().toString().trim().isEmpty();
The cleanest way to do this is TextUtils.isEmpty(editText.getText())
The reason I say this is the cleanest way is because:
You avoid pointless conversion between CharSequence and String. Which creates an object. editText.getText() returns Editable, calling toString() creates an additional object which is not good. This method will also never return null in my experience.
You get a null and a length check out of that. If you look at the code for TextUtils.isEmpty(), it basically checks if the CharSequence is null and length is zero.
It avoids code duplication and the same method can be used with Strings or CharSequence objects and Editable is an implementation of CharSequence.
It's provided as part of the Android framework.
If you want to check the length of the trimmed String. Then use:
TextUtils.isEmpty(editText.getText())
&& TextUtils.getTrimmedLength(editText.getText()) == 0
If you want, you can create your own utility method to do this so you don't have to add such a long condition in your code repeatedly.
I would attached an OnFocusChangeListener to your EditText to check the change in value or a TextWatcher or both depending on how critical your requirement is. If your field had focus and lost it, do your validation with the OnFocusChangeListener, if your field has focus and the user is typing and delete the content or the content is too short, use TextWatcher to let them know.
Use this on click of your button:
EditText editText = (EditText) view.findViewById(EditTextID);
if(editText.getText().toString().length()==0) {
Toast alert = Toast.makeText(context, toast_message, Toast.LENGTH_SHORT);
alert.show();
}
In the onClickListener() of the button:
int length = editText.getText().length();
if(length == 0)
{
Toast.makeText(getActivity(), "Please enter a number",Toast.LENGTH_LONG).show();
}
you probably already found your answer, but for the ones who came here hoping to find an answer here is how its done:
you have to make a String object or Int object first then in your button function Click write this:
#Override
public void onClick(View view) {
String numberValue;
numberValue = yourEditText.getText().toString();
if (emailEtValue.matches("")){
Snackbar sbEmptyValue = Snackbar.make(view, "You must enter an Integer Value", Snackbar.LENGTH_LONG);
sbEmptyValue.show();
} else {
//DO THE THING YOU WANT
}
}
you can also use Toast but i prefer Snackbar because its cooler than Toast.
My intention is to put an Edittext, and a Button below, and when the user put an specific word and click the button, the value is correct, and all the other different words are wrong.. How to identify it with codes?
btn.onClickListiner(this);
#Override
public void onClick(View v) {
if (v == btn) {
// check word
String word = editText.getText().toString(); // don't forget "toString()" .
// checking word below :
}}
I'm making an app for Android, and if you click the calculate button on one of the pages, without anything entered in the text boxes, it force closes. Some users were wondering if this could be fixed, so I was wondering if there was a way to make the onClickListener execute only if there is something inside the EditText.
You have to check it yourseft, such as:
final EditText editText = ...; // your edit
// check in your onClickListener
if (editText.getText().toString().isEmpty){ // Check if your EditText is
}else{ // If your EditTexit is not null
}
Please, search google before asking any question!
You can try following code,
suppose you have a EditText txtNum1 & txtNum2 , so onClickListener() method you can write following condition
public void onClick(View v)
{
if ( v == cmdCalculate )
{
if ( !txtNum1.getText().equals("") && !txtNum2.getText().equals("") )
{
// your calculation code
}
else
{
// post error msg code
}
}
}