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 6842213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:04:19+00:00 2026-05-27T00:04:19+00:00

So I have a dialog in which the user inputs data, here it is

  • 0

So I have a dialog in which the user inputs data, here it is the user’s age. So I would like that when/if the user leaves the edittext blank, the edittext will do the shake animation like that in the api demos. So far I cannot get the dialog from not dismissing when invalid info is inputed. Thanks.

mInflater = (LayoutInflater) Information.this.getSystemService(LAYOUT_INFLATER_SERVICE);
        mLayout = mInflater.inflate(R.layout.agedialog, null);

        mAgeEditText = (EditText) mLayout.findViewById(R.id.AgeEditText);
        mAgeResultTextView = (TextView) findViewById(R.id.AgeResultTextView);
        final Animation shake = AnimationUtils.loadAnimation(Information.this, R.anim.shake);

        mInputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        mInputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

        new AlertDialog.Builder(Information.this).setView(mLayout).setTitle(R.string.EnterYourAge)
                .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mInputManager.hideSoftInputFromWindow(mAgeEditText.getWindowToken(), 0);
                        if (TextUtils.isEmpty(mAgeEditText.getText())) {
                            mAgeEditText.startAnimation(shake);
                            // here the dialog dismisses even if I call startAnimation
                        }
                        else {
                            HelperClass.getInstance().setAge(Integer.parseInt(mAgeEditText.getText().toString()));
                            mAgeResultTextView.setText(HelperClass.getInstance().getAge());
                        }
                    }
                }).setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        mInputManager.hideSoftInputFromWindow(mAgeEditText.getWindowToken(), 0);
                        dialog.cancel();
                    }
                }).show();
  • 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-27T00:04:20+00:00Added an answer on May 27, 2026 at 12:04 am

    Ok so I figured out the answer a long time after this was asked but just in case someone wants this I thought it was wise to go ahead and post the answer.

    Basically this can NOT be done with the Alert Dialog, it has to do with the behavior when the button and negative buttons are clicked. However it can be done with a regular Dialog. You need a few animation xmls, and those can be found in the api demo’s anim folder under res (The original java file is Animation1.java in api demos so you can check that out). I also made it so that the keyboard comes up ready for the user to type. You can rid of that by deleting all the inputmanager stuff. Anyway here’s the code to the dialog.

    public void showAgeDialog() {
        final Dialog dialog = new Dialog(Information.this);
    
        dialog.setContentView(R.layout.age_dialog);
        dialog.setTitle(R.string.InputAge);
    
        final EditText ageEditText = (EditText) dialog.findViewById(R.id.AgeEditText);
    
        inputManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    
        dialog.show();
    
        Button positive = (Button) dialog.findViewById(R.id.OkButton);
        Button negative = (Button) dialog.findViewById(R.id.CancelButton);
    
        positive.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                if (ageEditText.getText().toString().isEmpty()) {
                    Animation shake = AnimationUtils.loadAnimation(Information.this, R.anim.shake);
                    ageEditText.startAnimation(shake);
                    Toast.makeText(Information.this, "Please enter an age", Toast.LENGTH_SHORT).show();
                }
                else {
                    database.updateAge(Integer.parseInt(ageEditText.getText().toString()));
                    inputManager.hideSoftInputFromWindow(ageEditText.getWindowToken(), 0);
                    dialog.dismiss();
                }
            }
        });
    
        negative.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                inputManager.hideSoftInputFromWindow(ageEditText.getWindowToken(), 0);
                dialog.dismiss();
            }
        });
    }
    

    Here is the code to my layout. I made this dialog look exactly like an Alert Dialog with the Ok and Cancel buttons along with the gray background.

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <EditText
            android:id="@+id/AgeEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="18dp"
            android:layout_marginLeft="100dp"
            android:layout_marginRight="100dp"
            android:hint="@string/Years"
            android:inputType="number"
            android:maxLength="2"
            android:textSize="18sp" >
        </EditText>
    
        <LinearLayout
            style="@android:style/ButtonBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
    
            <Button
                android:id="@+id/OkButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/Ok" />
    
            <Button
                android:id="@+id/CancelButton"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/Cancel" />
        </LinearLayout>
    
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form in which user inputs its data along with its image.
In my android application I have a dialog box in which the user inputs
I have a dialog box that the user inserts various data through gui controls,
Introduction I have been so annoyed by applications that have a startup dialog which
I have 2 dialog boxes in which I will display 1 dialogbox at a
I have a dialog that resizes. It also has a custom background which I
I have a form which takes multiple inputs from user. Now I wanna show
I have a jquery-ui-dialog that contains a <form> which contains a <input type=file >
hello all I have a small dialog which I created dynamically, which has a
I'm coding an MFC application in which i have a dialog box with multiple

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.