Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom alert dialog getting unwanted padding

I have tried to make a custom alert dialog and it mostly works very well. the functionality is working perfectly but the view is behaving in a weird way. The dialog layout consists of a button and a listview. as you will see in the screenshot below the button is getting a margin on top and bottom.

i dont see any reason for these margins, and i would greatly appreciate some help :)

for some reason i cant post my xml layout but i can asure you that it contains no paddings or margins of any kind

Java Code :

View dialogView = ((LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.choose_catagory_dialog_layout, null, false);
Button footerButton = (Button) dialogView.findViewById(R.id.choose_catagory_dialog_footer_button);
footerButton.setOnClickListener(ButtonClickEvent);

builder = new AlertDialog.Builder(mContext);
builder.setView(dialogView);
builder.setTitle(R.string.choose_catagory);

builder.setAdapter(spinnerAdapter, ListclickEvent);
alert = builder.create();
alert.getListView().setVerticalFadingEdgeEnabled(false);
alert.setOwnerActivity((Activity) mContext);

Screenshot:

http://cl.ly/3S2y3p3E0e3H2o1I272m

like image 400
Emil Sjölander Avatar asked Mar 16 '11 17:03

Emil Sjölander


3 Answers

The CustomPanel of an AlertDialog has a 5dp top and bottom padding. You can override these using:

alert.setView(dialogView, 0, 0, 0, 0);
like image 86
pcans Avatar answered Oct 17 '22 11:10

pcans


For me, it seemed that builder.setView(dialogView, 0, 0, 0, 0); is set to @hide and not available. In the end, I had to set the padding of my custom view's parent after calling show()

For example:

// Inflate custom dialog view
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
CustomDialogView dialogView = (CustomDialogView)inflater.inflate(R.layout.dialog_view, null);

// Create and show custom alert dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.dialog_title);
builder.setView(dialogView);
builder.setPositiveButton(R.string.dialog_ok, this);
builder.setNegativeButton(R.string.dialog_cancel, this);
builder.show();

// Remove padding from parent
ViewGroup parent = (ViewGroup)dialogView.getParent();
parent.setPadding(0, 0, 0, 0);
like image 10
Chase Avatar answered Oct 17 '22 12:10

Chase


Here is code that will give you full control over the dialog view:

final Dialog dlg = new Dialog(this);
View action_layout = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.contacts_action, null);
dlg.requestWindowFeature(Window.FEATURE_NO_TITLE);  
Window window = dlg.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.FILL_HORIZONTAL;
window.setAttributes(wlp);
dlg.setCancelable(true);
dlg.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dlg.setContentView(action_layout);
dlg.show();
like image 2
Gal Rom Avatar answered Oct 17 '22 11:10

Gal Rom