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("[email protected]");
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
Solution: We can display error message in EditText by using setError() method.
You can use the TextInputLayout to display error messages according to the material design guidelines using the setError and setErrorEnabled methods. In order to show the error below the EditText use: TextInputLayout til = (TextInputLayout) findViewById(R. id.
What should I set to disable EditText? It's possible to preserve both the style of the view and the scrolling behaviour. To disable an EditText while keeping this properties, just use UI. setReadOnly(myEditText, true) from this library.
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); } } });
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);
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();
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("[email protected]");
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);
}
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With