Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:50:10+00:00 2026-06-08T01:50:10+00:00

I have validation for editText . If the editText field is empty it should

  • 0

I have validation for editText. If the editText field is empty it should fail validation and stop the user moving on to another Activity, as a value is required. How to do it? I know this is a basic question, but I can’t figure out how to do this.

My code:

btninsert = (Button)findViewById(R.id.btn_insert);
btninsert.setOnClickListener( new View.OnClickListener() {
    public void onClick(View v) {
        insertValues();
        EditText userName = (EditText) findViewById(R.id.editText1);

        if( userName.getText().toString().length() == 0 )
            userName.setError( "First name is required!" );

        Intent i = new Intent(getApplicationContext(), Login.class);
        startActivity(i);
    }
});

When this Button is clicked the user is redirect to next screen, but I need that if validation fails they stay on the current Activity, and only when validation is successful (i.e. a value has been entered) they go to the next Activity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:50:11+00:00Added an answer on June 8, 2026 at 1:50 am

    It’s easy…check if your EditText is empty as in below example below.

    if( TextUtils.isEmpty(userName.getText())){
       /**
        *   You can Toast a message here that the Username is Empty    
        **/
        
       userName.setError( "First name is required!" );
    
    }else{
       Intent i = new Intent(getApplicationContext(), Login.class);
       startActivity(i);
    }
    

    Here is a Kotlin version:

    userName.takeIf { it.isEmpty() }?.let {
      userName.error = "First name is required!"
    } ?: run {
       startActivity(Intent(applicationContext, this))
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Activity with 3 EditText fields. User, Pass, Confirmation After typing
Currently I have an edittext field that when the user presses enter it does
I have some validation code which should display the max / min of a
I am Planning to implement a form validation in android. I have done empty
I make a login form with dynamic field validation. I have 3 fields username,
I have validation on uniqueness and I want skipping certain value or values(for example
I want to to have some validation for my EditText wherein I want to
I have validation constraints defined in my validation.yml for my User entity, but none
I have the validation for autocomplete working. http://skoizumi.com/autocomplete/index.html var ac = $( #searchtext ).autocomplete({
I have custom validation rule: public function customRule($check) { } Inside this rule I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.