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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:37:53+00:00 2026-05-31T20:37:53+00:00

I have a dialog in an Android app that I don’t want the user

  • 0

I have a dialog in an Android app that I don’t want the user to be able to cancel. Using .setCancelable(false) disables the back button, but pressing the search button still cancels the dialog. I saw this question which told me that I should include

public boolean onSearchRequested() {
    return false;
}

But I’m still able to cancel the dialog with the search button. Here’s my code:

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showDialog(0);
    }

    public boolean onSearchRequested() {
        return false;
    }

    protected Dialog onCreateDialog(int id) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Message")
               .setCancelable(false)
               .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // nothing
                   }
               });
         return builder.create();
    }
}
  • 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-31T20:37:55+00:00Added an answer on May 31, 2026 at 8:37 pm

    @Benh You need this code to set for your Key Listener for Dialog

       builder.setOnKeyListener(keylistener);
    

    Add Below code in your Activity Class

      OnKeyListener keylistener=new DialogInterface.OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
                return true; //we stop begin cancel of dialog or Progressbar
            }
            return false; 
        }
    }; 
    

    try this above thing in your dialog hope that will work for you.

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

Sidebar

Related Questions

I have an Android app that begins by displaying a dialog. When I set
I have a class that extends android.app.Dialog, the layout is done in an xml
I have an android app that posts to users wall by using the Facebook
I have a very simple dialog defined as: import android.app.AlertDialog; import android.content.Context; import android.view.LayoutInflater;
I have an extended dialog class that I want to show for 3 seconds
I have an android app that wants to initiate an email. The following code
In my Android app, I have a dialog with a few options, one of
I have an Android app that should open links sent via email. I only
I have a dialog box that allows users to set hotkeys for use in
I have a dialog that resizes. It also has a custom background which 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.