1

Im creating a custom Dialog. But it is showing extra space around.

Screenshot

Code:

private void showPushAlert(Context context, String message, int layoutID) {
            // custom dialog
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(layoutID);

            TextView tvPushMessage = (TextView) dialog.findViewById(R.id.tvAlertMessage);
            tvPushMessage.setText(message);

            Button btnPushOk = (Button) dialog.findViewById(R.id.btnAlertOk);
            btnPushOk.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    dialog.dismiss();
                }
            });
            dialog.show();
        }

Layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="275dp"
    android:layout_height="wrap_content"
    android:background="@drawable/background_round_rectangle"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin">
    <TextView
        android:id="@+id/tvAlertMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />;
    <Button
        android:id="@+id/btnAlertOk"
        android:layout_width="65dp"
        android:layout_height="30dp"
        android:layout_below="@id/tvAlertMessage"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/btn_use"
        android:text="@string/ok"
        android:textColor="@color/white" />
</RelativeLayout>

I also tried inflater:

    final Dialog dialog = new Dialog(context);
    View view = getLayoutInflater().inflate(layoutID, null);
    dialog.setContentView(view);

But not the perfect result. Jut the width stretched. I wanted to keep simple, so used just Dialog, instead of AlertDialog, or Dialog fragment.

Abu Ruqaiyah
  • 1,516
  • 1
  • 12
  • 20
  • there may be two different styles trying to modify layout so ... check your styles once again. that space may be from another style or from Relativelayout. try to set relative layout width wrap_content see the changes and try to find out space is from relative layout or style? – sud Nov 06 '15 at 10:03
  • Using 'wrap_content' for width works same like inflater. Width stretches to fit, but there is space above. Didn't notice anything related in 'styles'. and – Abu Ruqaiyah Nov 06 '15 at 12:02

1 Answers1

0

Not sure why that was happening..

Used https://stackoverflow.com/a/6922903/4510869 to do this after dialog.show()

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = 500;
dialog.getWindow().setAttributes(lp);

This showed the custom width, but the top space was still visible. Had to change to AlertDialog.Builder + the above snippet (AlertDialog also showed top space) to finally get the result.

Community
  • 1
  • 1
Abu Ruqaiyah
  • 1,516
  • 1
  • 12
  • 20