I want to show error if the user enters blank value in the edittext.But i am not getting the way how could i do this .This is how i want like this:
I got the solution of my problem Hope this will help others also.I have used onTextChnaged it invokes When an object of a type is attached to an Editable, its methods will be called when the text is changed.
public class MainActivity extends Activity {
TextView emailId;
ImageView continuebooking;
EditText firstName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
emailId = (TextView)findViewById(R.id.emailid);
continuebooking = (ImageView)findViewById(R.id.continuebooking);
firstName= (EditText)findViewById(R.id.firstName);
emailId.setText("test@gmail.com");
setTittle();
continuebooking.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(firstName.getText().toString().trim().equalsIgnoreCase("")){
firstName.setError("Enter FirstName");
}
}
});
firstName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
firstName.setError(null);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
firstName.setError(null);
}
});
}
With youredittext.equals("")
you can know if user hasn't entered any letter.
You have written your code in onClick
event. This will call when you click on EditText
. But this is something like you are checking it before entering.
So what my suggestion is, you should use focus changed
. When any view get focus, you are setting no error
and when focus changed, you check whether there is valid input or not like below.
firstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
firstName.setError(null);
if (firstName.getText().toString().trim().equalsIgnoreCase("")) {
firstName.setError("Enter FirstName");
}
}
});
It seems all you can't get is to show the error at the end of editText. Set your editText width to match that of the parent layout enveloping. Will work just fine.
Using Kotlin Language,
EXAMPLE CODE
login_ID.setOnClickListener {
if(email_address_Id.text.isEmpty()){
email_address_Id.error = "Please Enter Email Address"
}
if(Password_ID.text.isEmpty()){
Password_ID.error = "Please Enter Password"
}
}
Simple way to implement this thing following this method
1st initial the EditText Field
EditText editText = findViewById(R.id.editTextField);
When you done initialization. Now time to keep the imputed value in a variable
final String userInput = editText.getText().toString();
Now Time to check the condition whether user fulfilled or not
if (userInput.isEmpty()){
editText.setError("This field need to fill up");
}else{
//Do what you want to do
}
Here is an example how I did with my project
private void sendMail() {
final String userMessage = etMessage.getText().toString();
if (userMessage.isEmpty()) {
etMessage.setError("Write to us");
}else{
Toast.makeText(this, "You write to us"+etMessage, Toast.LENGTH_SHORT).show();
}
}
Hope it will help you.
HappyCoding