8

So I see we can have alertdialogs with gray and white (when setinverse...) background colors.

To learn why I checked themes.xml of the sdk, checking it I was led to drawables and there I realized the alertdialog background is not done programatically but via some images. And these images guarantee that there are two gray(or white when inverse color) horizontal lines on top(title area) and bottom(just above button area) of the dialog when we use LayoutInflater to just set a different backgroundcolor.

So my question is, as LayoutInflator is useless and guessing I have to subclass alertdialog, what do you suggest I do to generate an AlertDialog with a different backgroundcolor? What should I override?

3 Answers 3

17

Instead of using AlertDialog, I ended up using a Dialog. To get a custom look:

1-Create the Dialog and remove the title area(Otherwise you'll get a blank gray area on top):

myDialog = new Dialog(this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

2-Design a layout in xml, and set as dialog's content:

myDialog.setContentView(R.layout.mydialog_layout);

3-If the layout is not a rounded rect, it will intersect with the rounded corners of the dialog box. So, design the layout as a rounded rect:

in mydialog_layout.xml:

android:background = "@layout/mydialog_shape"

mydialog_shape.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle" 
     > 
     <gradient android:startColor="#FF0E2E57" 
     android:endColor="#FF0E2E57" 
            android:angle="225" android:paddingLeft="20dip"/> 

    <corners android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp" 
     android:topLeftRadius="5dp" android:topRightRadius="5dp" android:paddingLeft="20dip"/> 
</shape>

4-Add listeners to the buttons in your activity:

Button button = (Button)myDialog.findViewById(R.id.dialogcancelbutton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    myDialog.cancel();
}});

That's about it.

3
  • What's with the 'yasalUyariDialog.' section above? It seems a bit random?
    – andy_spoo
    Jan 29, 2011 at 10:55
  • yeah totally sorry about that.fixed Jan 30, 2011 at 12:40
  • You can also do this with alert... you just have to inflate the layout first with layoutinflator and then it is the same but easier to assign an icon and title. Jan 30, 2011 at 12:44
0

I recall reading that not all Android Dialogs are created equally. Therefore, if you don't want to use the dialog that shipped with the device's Android version; You need to code a completely fresh dialog from the ground up.

Edit:

I think you need to override the onCreateDialog with a custom dialog builder class. Like I said, I've never done it. Remember, to keep with the Android MVC style, you need to define the dialog in XML as well. If I was going to do it; I would probably start with the XML layout, then code a custom dialog class using the same methods as a regular dialog builder class. Sorry to be so vague, I'm still learning Java and Android myself.

1
  • +1 Thx for the answer. So what methods should I override to get a functional custom dialog? Oct 25, 2010 at 8:10
0

so easy..

Dialog d=builder2.create();
...
d.show();
d.getWindow().setBackgroundDrawableResource(R.drawable.mydialog_shape);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.