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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:54:41+00:00 2026-05-28T04:54:41+00:00

I just added an Alert Dialog that comes up when the back button is

  • 0

I just added an Alert Dialog that comes up when the back button is clicked. It’s set to the default android alert I believe. Is there anyway to customize how the alert dialog box looks such as change the background or set a drawable to the background? I am new to this so I am not sure what to do. Thanks, and my code is below that I used for the alert dialog.

Alert Dialog:

public boolean onKeyDown(int keyCode, KeyEvent event) {
        //Handle the back button
        if(keyCode == KeyEvent.KEYCODE_BACK) {
            //Ask the user if they want to quit
            new AlertDialog.Builder(this)
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(R.string.quit)
            .setMessage(R.string.really_quit)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {

                    //Stop the activity and pause media player
                     mainSound.pause();
                    MainActivity.this.finish();    
                }

            })
            .setNegativeButton(R.string.no, null)
            .show();

            return true;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }

    }
  • 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-28T04:54:42+00:00Added an answer on May 28, 2026 at 4:54 am

    Like this..

    Create your xml layout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/dialog_layout_root"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:padding="10dp"
              >
    

    And then you can set your layout on the builder with the following:

    LayoutInflater inflater = getLayoutInflater();
    View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup)    findViewById(R.id.dialog_layout_root));
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(dialoglayout);
    

    EDIT:

    You should rearrange your code to something like this…
    Create a AlertDialog.Builder at class level.

    private AlertDialog.Builder builder;
    

    In your onCreate() create your AlertDialog

      LayoutInflater inflater = getLayoutInflater();
      View dialoglayout = inflater.inflate(R.layout.dialog_layout, (ViewGroup)     findViewById(R.id.dialog_layout_root));
    
    
      //Ask the user if they want to quit
            builder
            .setIcon(android.R.drawable.ic_dialog_alert)
            .setTitle(R.string.quit)
            .setMessage(R.string.really_quit)
            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
    
                public void onClick(DialogInterface dialog, int which) {
    
                    //Stop the activity and pause media player
                     mainSound.pause();
                    MainActivity.this.finish();    
                }
    
            })
            .setNegativeButton(R.string.no, null)
            .setView(dailogLayout);
    
    
    
    
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //Handle the back button
        if(keyCode == KeyEvent.KEYCODE_BACK) {
    
         builder.show();
    
            return true;
        }
        else {
            return super.onKeyDown(keyCode, event);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just added yet another 3rd-party component to my .net project that contains a
I programatically create an NSToolbar in my application. I just added a new button
I added a like button to the canvas application page that actually configured to
We just added an autoupdater in our software and got some bug report saying
I just added xUnit to our test project (for the Asserts, we're still using
I just added printing capability to a web site using a style sheet (ie.
I just added this to my web.xml on my JBOSS server. But it had
I've just added the Twitter script to my website, and cannot, despite inexpertly consulting
I have just added a Core Plot view to my application based on a
I have just added a Telerik menu to my MVC application. I also have

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.