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

  • SEARCH
  • Home
  • 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 6759629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:58:05+00:00 2026-05-26T13:58:05+00:00

How to make a Toast to pop up when the user tries to insert

  • 0

How to make a Toast to pop up when the user tries to insert text longer than maximum limit?

<EditText
    android:maxLength="28"
    ...
 />

I tried to use TextWatcher but it doesn’t work properly:

public class MyActivity extends Activity implements TextWatcher
{

    // ...

    @Override
    public void afterTextChanged(Editable arg0){}

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3){}

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
    {
        if(etUsername.isFocused() && etUsername.getText().length() == 28)
            Toast.makeText(MyActivity.this, "The username must be at most 28 characters!" , Toast.LENGTH_SHORT).show();
        else if(etPassword.isFocused() && etPassword.getText().length() == 10)
            Toast.makeText(MyActivity.this, "The password must be at most 10 characters!" , Toast.LENGTH_SHORT).show();
    }
}

EDIT:

I also tried to put the code inside afterTextChanged and beforeTextChanged but it doesn’t solve the problem.

EDIT2:

What I want is that the Toast only can be popped up when there are 28 characters in the EditText and the user is trying to add a 29th character. The Toast in my code above will pop up only if there are 27 characters in the TextEdit and the user inserts a 28th character.

  • 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-05-26T13:58:06+00:00Added an answer on May 26, 2026 at 1:58 pm

    The only way to achieve desired functionality is to change android:maxLength to desiredMaxLength + 1 and to check for the length in afterTextChanged(). You will have to manually delete the last character when length == desiredMaxLength + 1 (i.e. manipulate the s parameter in afterTextChanged(Editable s)). Please be careful not to put yourself in infinite loop because after changing s afterTextChanged() will be called again.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I make a user control and then want to include it on a page
hey, im new to android development and trying to make my first application. What
I used Toast to make notification, but it seems it will appear even its
I want to make a toast where it shows Your message message is sent
I have a toast in a slave thread which needs to tell a user
I have a custom toast notification which has an image and text. The custom
I successfuly managed to make push notifications (tile and toast) work on my device.
Im new to android and i am trying to make a program that outputs
I make a program in Eclipse to Android and I have a problem. I
How to make the suggestions in the QSB editable when the user clicks on

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.