0
public class MainActivity extends Activity {
private EditText emailEditText;
private EditText passEditText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    emailEditText = (EditText) findViewById(R.id.editText_email);
    passEditText = (EditText) findViewById(R.id.editText_password);

    findViewById(R.id.btn_signup).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            final String email = emailEditText.getText().toString();
            if (!isValidEmail(email)) {
                emailEditText.setError("Invalid Email");

            }

            final String pass = passEditText.getText().toString();
            if (!isValidPassword(pass)) {
                passEditText.setError("Invalid Password");
            }

        }
    });
}

I am using this code however it shows the error message below, I want to display the message adjacent to the EditText i.e; right of EditText.

adneal
  • 30,484
  • 10
  • 122
  • 151
Dhruv Gupta
  • 61
  • 2
  • 8

2 Answers2

0

Instead of Using EditText use TextInputLayout like:

<android.support.design.widget.TextInputLayout
    android:id="@+id/text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:errorEnabled="true">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name" />

</android.support.design.widget.TextInputLayout>

When you want to setError just take the reference of TextInputLayout and setError on it and when error is resolved then remove it by passin null in setError

Mohammed Rampurawala
  • 3,033
  • 2
  • 22
  • 32
0

To show error message like you have shown ,Use my code,Its workable code.

    <android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
    android:layout_marginTop="20dp"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/userId"
        android:inputType="text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:hint="User ID" />

</android.support.design.widget.TextInputLayout>


<android.support.design.widget.TextInputLayout
     android:layout_width="match_parent"
    android:inputType="textPassword"
    app:passwordToggleEnabled="true"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:text=""
        android:hint="Password" />

</android.support.design.widget.TextInputLayout>

<medmylife.deliverymedcine.font.Railway_Regular
    android:layout_marginTop="10dp"
    android:gravity="right"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Forget Password?"
    android:textColor="@color/blue"
    android:textSize="16dp"/>

<Button
    android:layout_marginTop="20dp"
    android:textSize="16dp"
    android:id="@+id/loginBtn"
    android:textAllCaps="false"
    android:background="@color/blue"
     android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textColor="@color/white"
    android:text="LOGIN"
    />

Put below code on your Activity.class

 loginBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (utils.haveNetworkConnection()) {
                if (validationDetails()) {
                    jsonRequest();
                }
            }else {
                utils.showtoast("Internet Connection Not Availble");
            }
        }
    });

Put below code outside of the onCreate()

 private boolean validationDetails() {
    if (userId.getText().toString().isEmpty()) {
        userId.setError("Enter User ID");
        return false;
    }
    if (passwd.getText().toString().isEmpty()) {
        passwd.setError("Enter Password");
        return false;
    }
    return true;
}
Abhishek kumar
  • 4,347
  • 8
  • 29
  • 44