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:
This is my xml that i have created
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@drawable/top_bg"
android:orientation="horizontal" >
<ImageView
android:id="@+id/back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:src="@drawable/back_button" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_marginTop="10dp"
android:text="Traveller Details"
android:textColor="@android:color/white" />
</LinearLayout>
<LinearLayout
android:id="@+id/tittleLayout"
android:layout_below="@+id/headerLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/TittleTravellerDetails"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:gravity="left"
android:text="Traveller Details" />
<View
android:layout_width="wrap_content"
android:layout_height="2dip"
android:layout_marginTop="2dp"
android:background="#FF909090" />
</LinearLayout>
<LinearLayout
android:id="@+id/passengerDetails"
android:layout_below="@+id/tittleLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<Spinner
android:id="@+id/Tittle"
android:layout_width="290dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/firstName"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:hint="First Name" />
<EditText
android:id="@+id/LastName"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:hint="Last Name" />
</LinearLayout>
<LinearLayout
android:id="@+id/ContactDetailsLayout"
android:layout_below="@+id/passengerDetails"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/TittleContactDetails"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:gravity="left"
android:text="ContactDetails" />
<View
android:layout_width="wrap_content"
android:layout_height="2dip"
android:layout_marginTop="2dp"
android:background="#FF909090" />
</LinearLayout>
<LinearLayout
android:id="@+id/mobileEmailDetails"
android:layout_below="@+id/ContactDetailsLayout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<EditText
android:id="@+id/mobileNumber"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:inputType="number"
android:hint="Mobile No" />
<TextView
android:id="@+id/emailid"
android:layout_width="284dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="5dp"
android:hint="Email ID" />
</LinearLayout>
<LinearLayout
android:id="@+id/continueBooking"
android:layout_below="@+id/mobileEmailDetails"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/continuebooking"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:src="@drawable/continue" />
</LinearLayout>
</RelativeLayout>
Activity Code
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("gauravthethinker@gmail.com");
setTittle();
continuebooking.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(firstName.getText().toString().trim().equalsIgnoreCase("")){
firstName.setError("Enter FirstName");
}
}
});
}
So ,if the user don’t enter his first name i want to show a error like the image that u have not entered any name something like that.Please help me i am new in android.Thanks
Vadim Kotov
8,0448 gold badges48 silver badges62 bronze badges
asked Aug 14, 2013 at 7:10
1
You can show error as PopUp of EditText
if (editText.getText().toString().trim().equalsIgnoreCase("")) {
editText.setError("This field can not be blank");
}
and that will be look a like as follows
firstName.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (firstName.getText().toString().length <= 0) {
firstName.setError("Enter FirstName");
} else {
firstName.setError(null);
}
}
});
answered Aug 14, 2013 at 7:16
SilentKillerSilentKiller
6,9446 gold badges39 silver badges75 bronze badges
9
private void showError() {
mEditText.setError("Password and username didn't match");
}
Which will result in errors shown like this:
And if you want to remove it:
textView.setError(null);
answered Aug 14, 2013 at 7:17
Waza_BeWaza_Be
39.3k49 gold badges186 silver badges260 bronze badges
1
I know it’s too late, but in case someone still need help. Here is the working solution.
Setting an error
is pretty straight forward. But it will be displayed to user, when he request Focus
on it. So to do the both thing on your own, User this code
.
firstName.setError("Enter FirstName");
firstName.requestFocus();
answered Jul 18, 2018 at 15:14
Nouman GhaffarNouman Ghaffar
3,7801 gold badge29 silver badges37 bronze badges
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);
}
});
}
answered Aug 14, 2013 at 8:16
DeveloperDeveloper
6,28219 gold badges54 silver badges115 bronze badges
if(TextUtils.isEmpty(firstName.getText().toString()){
firstName.setError("TEXT ERROR HERE");
}
Or you can also use TextInputLayout which has some useful method and some user friendly animation
answered Mar 1, 2016 at 9:24
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"
}
}
answered Jan 9, 2019 at 18:41
BIS TechBIS Tech
16.3k10 gold badges93 silver badges143 bronze badges
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
answered Jul 27, 2020 at 13:17
MuktaMukta
1,2991 gold badge14 silver badges17 bronze badges
With youredittext.equals("")
you can know if user hasn’t entered any letter.
answered Aug 14, 2013 at 7:15
LydLyd
2,1063 gold badges26 silver badges33 bronze badges
1
u can use this :
@Override
public void afterTextChanged(Editable s) {
super.afterTextChanged(s);
if (s.length() == Bank.PAN_MINIMUM_RECOGNIZABLE_LENGTH + 10) {
Bank bank = BankUtil.findByPan(s.toString());
if (null != bank && mNewPanEntered && !mNameDefined) {
mNewPanEntered = false;
suggestCardName(bank);
}
private void suggestCardName(Bank bank) {
mLastSuggestTime = System.currentTimeMillis();
if (!bank.getName().trim().matches(getActivity().getString(R.string.bank_eghtesadnovin))) {
inputCardNumber.setError(R.string.balance_not_enmb, true);
}
}
answered Aug 4, 2020 at 9:11
Sana EbadiSana Ebadi
6,5221 gold badge43 silver badges43 bronze badges
you could use an onchange event to trigger a validation and then you can display a toast if this is enough for you
answered Aug 14, 2013 at 7:17
JavaDMJavaDM
8511 gold badge6 silver badges29 bronze badges
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");
}
}
});
answered Aug 14, 2013 at 7:27
Chintan RathodChintan Rathod
25.8k13 gold badges83 silver badges93 bronze badges
8
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.
answered Jul 20, 2015 at 5:29
SagePawanSagePawan
3642 silver badges21 bronze badges
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"match_parent"
android:orientation
=
"vertical"
tools:context
=
".MainActivity"
tools:ignore
=
"HardcodedText"
>
<
EditText
android:id
=
"@+id/firstName"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_marginStart
=
"16dp"
android:layout_marginTop
=
"16dp"
android:layout_marginEnd
=
"16dp"
android:hint
=
"First Name"
android:inputType
=
"text"
/>
<
EditText
android:id
=
"@+id/lastName"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_marginStart
=
"16dp"
android:layout_marginTop
=
"16dp"
android:layout_marginEnd
=
"16dp"
android:hint
=
"Last Name"
android:inputType
=
"text"
/>
<
EditText
android:id
=
"@+id/email"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_marginStart
=
"16dp"
android:layout_marginTop
=
"16dp"
android:layout_marginEnd
=
"16dp"
android:hint
=
"Email"
android:inputType
=
"textEmailAddress"
/>
<
EditText
android:id
=
"@+id/password"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_marginStart
=
"16dp"
android:layout_marginTop
=
"16dp"
android:layout_marginEnd
=
"16dp"
android:hint
=
"Password"
android:inputType
=
"textPassword"
/>
<
LinearLayout
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:layout_marginTop
=
"8dp"
android:gravity
=
"end"
android:orientation
=
"horizontal"
>
<
Button
android:id
=
"@+id/cancelButton"
style
=
"@style/Widget.AppCompat.Button.Borderless"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginEnd
=
"4dp"
android:text
=
"CANCEL"
android:textColor
=
"@color/colorPrimary"
/>
<
Button
android:id
=
"@+id/proceedButton"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_marginEnd
=
"16dp"
android:backgroundTint
=
"@color/colorPrimary"
android:text
=
"PROCEED"
android:textColor
=
"@android:color/white"
tools:ignore
=
"ButtonStyle"
/>
</
LinearLayout
>
</
LinearLayout
>
By
— September, 7th 2011
Problem: How to show error/alert for some cases in EditText?
Description:
Consider a case that we are supposed to collect information from users by filling EditText box and user leaves EditText blank, at that we display alert/error message “Please enter string”. But here is a best way to display alert/error message in EditText for the same case and also for other case.
Solution:
We can display error message in EditText by using setError() method.
Main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_margin="5dp"> <EditText android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_width="fill_parent"> </EditText> <Button android:layout_height="wrap_content" android:id="@+id/button1" android:text="Check" android:layout_width="wrap_content" android:onClick="btnClick"> </Button> </LinearLayout>
MainActivity.java
package com.paresh.edittextseterror; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { /** Called when the activity is first created. */ EditText editText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); editText = (EditText) findViewById(R.id.editText1); } public void btnClick(View v) { if(editText.getText().length()==0) { editText.setError("Field cannot be left blank."); } } }
Download this example: Android – Show error in EditText
CEO & Co-Founder at SolGuruz® | Organiser @ GDG Ahmedabad | Top 0.1% over StackOverflow | 15+ years experienced Tech Consultant | Helping startups with Custom Software Development
When I set error and request focus on edittext then it sets an error and request focus but how can I remove error and remove focus ?
mEditTextSearchOrAdd.setError("Error");
mEditTextSearchOrAdd.requestFocus();
asked Nov 2, 2012 at 13:08
Najib.NjNajib.Nj
3,6861 gold badge24 silver badges37 bronze badges
You should do the following :
mEditText.setError(null);//removes error
mEditText.clearFocus();
answered Nov 2, 2012 at 13:10
Ovidiu LatcuOvidiu Latcu
71.4k15 gold badges76 silver badges84 bronze badges
0
Example
=======
xml
---
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
java
-----
private EditText EditText1;
username = (EditText) findViewById(R.id.name);
username .setError(null);
answered Aug 11, 2017 at 11:55
We will go straight to the code.
package com.coderzheaven.seterror; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn =(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { EditText edittext =(EditText)findViewById(R.id.editText1); if(edittext.getText().length()==0){ edittext.setError("Field cannot be left blank."); } } }); } }
This is the layout containing the EditText and the Button.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" android:text="Check" /> </RelativeLayout>