3

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); } } 

3 Answers 3

3

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); } } 
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, Glad to have helped.
1

I would've written a more detailed answer here, but google wrote this tutorial much better than I could: just go to http://developer.android.com/guide/topics/ui/dialogs.html, and go to Creating a Custom Dialog. This is probably one of the best tutorials google wrote for Android.

1 Comment

Thanks, I checked that out it was very helpful.
0

how to create a CustumDialog is explained in the Android Docs here: http://developer.android.com/guide/topics/ui/dialogs.html At the buttom of the page you can find a point called "Creating a Custom Dialog".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.