Edittext Validation error message in android? - android

In my app i usually show validation error msg like follow :
if(someString.equals("")){
editText.setError("UserName Should not be blank");
}
is there other way to show error message??
i need your suggestion

I think the way you are showing the error message is better than toast. Sometimes toast duration is too short and the user isn't able to see that.
You can also achieve it by doing this:
EditText et11 = (EditText)findViewById(R.id.username);
if(et11.getText().toString().isEmpty())
{
et11.setError("UserName Should not be blank");
}

private EditText edt_firstName;
private String firstName;
firstName = edt_firstName.getText().toString().trim();
private void validateData(){
firstName = edt_firstName.getText().toString().trim();
if (!firstName.isEmpty(){
//here api call for the login or any other...
} else {
if (firstName.isEmpty()) {
edt_firstName.setError("Please Enter First Name");
edt_firstName.requestFocus();
}
}
}

Related

How to Check the empty text fields before uploading image or video in android?

I'm new to stackoverflow and android, sorry if i'm wrong. I am trying to check the text fields is empty or not, when the text field is empty, upload button should show please enter title and when user enters the text then only it should upload the image. After entering title then also it again shows the toast.
btnUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (title1.matches("")){
Toast.makeText(getApplicationContext(), "Please enter the Title and Category", Toast.LENGTH_SHORT).show();
} else if(description1.matches("")){
Toast.makeText(getApplicationContext(), "Please Select Category", Toast.LENGTH_SHORT).show();
} else {
flag = 1;
// uploading the file to server
new UploadFileToServer().execute();
}
}
});
Any help would be appreciated.
To check for empty fields, use
if(TextUtils.isEmpty(editText.getText().toString())) {
// do something
}
or if you want to check if string is empty or null use -
if(TextUtils.isEmpty(stringToCheck) {
// do something
}
try this.
Place this code in your button onClickListener:
String mText = mEditText.getText().toString();
if(TextUtils.isEmpty(mText)){
//Do what you want(Edittext is NULL)
}else{
//Do what you want(Not NULL)
}
Try to get value from your edittext like below:
Globally declare variable : -
String editvalue;
In onCreate() :-
EditText youredittext = (EditText)findViewById(R.id.youredittextid);
editvalue = youredittext.getText.toString().trim();
Now in your condition check :
if(editvalue.isEmpty() || editvalue == null){
// Do what you want when editText's value is null or empty
}else{
}
You can also use
TextUtils.isEmpty(CharSequence str)
for empty and null string check.
Returns true if the string is null or 0-length

Validation for save button

I've difficulty in creating a validation before saving it to sqlite, below is the code:
public void save(View v){
String weight = weightinputid.getText().toString();
String bmi = BMIfinal.getText().toString();
String status = BMIStatus.getText().toString();
long id = data.insertData(weight, bmi, status);
if(id<0){
message.mess(this, "Error");
}
else{
message.mess(this, "BMI has been saved");
}
}
How do I create a validation if all the textfields are empty? my problem right now, even if i pressed the save button, the empty textfields was saved inside the database
You can just try this, to check if value is not entered in the EditText.
if (weight .equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter Value1", Toast.LENGTH_LONG).show();
}
if (bmi.equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter Value2", Toast.LENGTH_LONG).show();
}
if (status.equals(""))
{
Toast.makeText(getApplicationContext(),"Please enter Value3", Toast.LENGTH_LONG).show();
}
Alternatively, you can use .matches("") instead of .equals("")
UPDATE
As #Rajesh mentioned in his comments, you can also use
TextUtils.isEmpty(weightinputid.getText())
to achieve the same functionality.
You can definitely do #Lal suggestion but in case the N fields are empty it's going to show the N toasts, and that's not very useful:
I'll suggest to do the following one of the following options:
a) Implement MaterialEditText:
Check the details here:
https://github.com/rengwuxian/MaterialEditText
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Min Characters"
app:met_minCharacters="1" />
b) Use a TextWatcher and enable the SaveButton only after you have your required fields with values:
http://developer.android.com/reference/android/text/TextWatcher.html
You can see how to it with the following question:
Disable Button when Edit Text Fields empty

Check textfield is empty in Android Studio

I am trying to check whether my textfield is empty for validation purpose but i am getting an error message cannot resolve method isEmpty
This is my partial coding:
private void addMovie(){
DatabaseHandler databaseHandler = new DatabaseHandler(getApplicationContext());
if(getIntent().getExtras()== null){
databaseHandler.insertRow(
mvidEditText.getText().toString(),
mvtitleEditText.getText().toString(),
mvtypeEditText.getText().toString(),
mvstoryEditText.getText().toString(),
mvratingEditText.getText().toString(),
mvlanguageEditText.getText().toString(),
Integer.parseInt(mvruntimeEditText.getText().toString()));
if (mvidEditText.isEmpty() || mvtitleEditText.matc) {
Toast.makeText(this, "You did not enter a username", Toast.LENGTH_SHORT).show();
return;
}
}else {
databaseHandler.updateRow(rowID,
mvidEditText.getText().toString(),
mvtitleEditText.getText().toString(),
mvtypeEditText.getText().toString(),
mvstoryEditText.getText().toString(),
mvratingEditText.getText().toString(),
mvlanguageEditText.getText().toString(),
Integer.parseInt(mvruntimeEditText.getText().toString()));
}
}
Are there any ways to do this? I did some research from stack overflow too.Thank you.
Now in new version getText() is not working directly so use only text
like this
if (enter_name.text.toString().isEmpty()) {
}
As far as my knowledge goes, there is no method isEmpty() in EditText class. you should do like this-
if(!TextUtils.isEmpty(editTextRef.getText().toString())){
///.... your remaining code if the edittext is not empty
}
if(mvidEditText.getText().length() == 0){}
you can try following for checking empty value for edittext
mvidEditText.getText().toString().isEmpty();
where isEmpty returns true if length of this string is 0.
if(mvidEditText.getText().toString().equals("")){print message here}
to check Edittext is empty
if(myeditText.getText().toString().trim().length() == 0)
Or use below function
private boolean isEmpty(EditText editText) {
return editText.getText().toString().trim().length() == 0;
}

How to compare two edittext fields in android

In my application i got two edit boxes,one is editPassword and other is editConfirmpassword.i want to compare data between those two boxes that both values are equal and only if they are equal then only write data in sharedpref file.
Try this:
EditText e1 = (EditText)findViewById(R.id.editPassword);
EditText e2 = (EditText)findViewById(R.id.editConfirmpassword);
if(e1.getText().toString().equals( e2.getText().toString())){
//do things if these 2 are correct.
}
Use string comparison here. You can fetch the EditText value using getText().toString();
Now you can do a compare using compareToIgnoreCase
Try to do this onCreate method to do the comparison. Use a button to confirm.
final EditText pass= (EditText)findViewById(R.id.Password);
final EditText cpass= (EditText)findViewById(R.id.ConfirmPassword);
final Button testButton= (Button)findViewById(R.id.Test);
testButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View arg0)
{
if(pass.getText().toString().equals(cpass.getText().toString())){
//Toast is the pop up message
Toast.makeText(getApplicationContext(), "Password match",
Toast.LENGTH_LONG).show();
}
else{
//Toast is the pop up message
Toast.makeText(getApplicationContext(), "Password does not match!",
Toast.LENGTH_LONG).show();
}
}
});

where to put checking edittext syntax?

i need a favor.. i'm confused to put these codes to check whether the edittext is empty or not:
String input = editText.getText().toString();
if(input == null || input.trim().equals("")){
Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
}
where must i write these codes? is it between these codes?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menuawal);
...
...
...
JmlAhliWarisAnakLK = (EditText) findViewById(R.id.JmlAhliWarisAnakLK);
JmlAhliWarisAnakPR = (EditText)findViewById(R.id.JmlAhliWarisAnakPR);
or in this function after double sisa=0;??
public void cc() {
int JmlWarisAnakPR = Integer.parseInt(JmlAhliWarisAnakPR.getText().toString());
int JmlWarisAnakLK = Integer.parseInt(JmlAhliWarisAnakLK.getText().toString());
int JmlHarta = Integer.parseInt(JmlHartaPeninggalan.getText().toString());
double HasilSuami = 0;
double HasilIstri = 0;
double HasilAnakLK = 0;
double HasilAnakPR = 0;
double sisa = 0;
}
please correct me if i'm wrong.. :D
you are on the right track
After you set the layout using setContentView you need to add your EditText's which you are doing fine as follows.
JmlAhliWarisAnakLK = (EditText)findViewById(R.id.JmlAhliWarisAnakLK);
JmlAhliWarisAnakPR = (EditText)findViewById(R.id.JmlAhliWarisAnakPR);
You then need to store the value you get from the EditText's in some variable,
int JmlWarisAnakPR = Integer.parseInt(JmlAhliWarisAnakPR.getText().toString());
....
....
After you have stored your values you can then call some method that validates your input on click of a button(if you have):
public void validateinput()
{
if(input == null || input.trim().equals(""))
{
Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
}
}
According to me, you should put the check on some event, like if its login screen, then on click of submit button. or other wise on focus change it main instantly provide user with the toast that he left the field empty. or if other case, please provide more information for your query. thanks.
That depends on when you want to validate the editText..You propably have some button which "submits" the EditText so call this code in after onClick event gets fired on the button..
Put the input validation code when you have to navigate away from the current activity, either to go to another activity or to save the input details. That's the least annoying place to shove an error message onto the user.
Another approach is to validate when the focus leaves the EditText. But in this case the error notification should be more subtle (and therefore less annoying) like changing the EditText's background to lightred.
Ur questions does not seem to be clear. Are u asking where do u need to put the validation for empty edittext? If this is ur question then the general case would be to validate during any events such as BUTTON CLICK. Set the onClickListener for ur button and inside ur onclick perform the validation.
String input = editText.getText().toString();
if(input == null || input.trim().equals("")){
Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
}
Your above code is pretty much correct. You Must need to add above code whenever you want to take input from these edittext, Or whenever you want to save these value. make a function which will return true if edit text is empty so u can ask user to enter values
public boolean isETEmpty(){
String input = editText.getText().toString();
if(input == null || input.trim().equals("")){
Toast.makeText(context, "Sorry you did't type anything"), Toast.LENGTH_SHORT).show();
return true;
}
return false; // if not empty
}
call this function Whenever u want to use values from ET, if this function return true, you must let user enter values. Such as on Button Click to save etc

Categories

Resources