2

I am trying to make a custom dialog box

LayoutInflater li = LayoutInflater.from(this);
        View view = li.inflate(R.layout.rate_layout, null);

        RelativeLayout rate = (RelativeLayout) view.findViewById(R.id.rateClick);
        RelativeLayout close = (RelativeLayout) view.findViewById(R.id.closeBtn);
        close.setClickable(true);
        rate.setClickable(true);    

       final AlertDialog.Builder  dd = new AlertDialog.Builder(this);          
       dd.setView(view);
       dd.setCancelable(false);

       final AlertDialog d = dd.create();
       d.show();

The background image has 4 types of resolutions so I can do wrap content only.

Here is my xml file

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >



  <RelativeLayout
        android:id="@+id/rateClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/rate_now_button" />

  <RelativeLayout
      android:id="@+id/imageView1"
      android:layout_width="219dp"
      android:layout_height="234dp"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:background="@drawable/pop_up" >

      <RelativeLayout
          android:id="@+id/closeBtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:background="@drawable/close_button" >
      </RelativeLayout>

       <RelativeLayout
        android:id="@+id/rateClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:background="@drawable/rate_now_button" />

  </RelativeLayout>

</RelativeLayout>

This is what I am getting. How am i suppose to remove the dialog layout. The white big borders.

My Image

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193

6 Answers6

5

Don't use AlertDialog.Builder. Try this instead.

Dialog d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);

or

Dialog d = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);

You can use whatever theme meets your criteria, or create your own. See answer to this question and this other question. See also available themes.

See Dialog for documentation on setContentView(), setCancelable(), show() and other methods you might find useful.

Community
  • 1
  • 1
Andy Harris
  • 928
  • 5
  • 10
  • If I use dialog , it extends the image to whole screen.... that's the main problem – Muhammad Umar Sep 14 '12 at 04:46
  • You could always create a CustomDialog with a transparent background to remove the borders as in http://stackoverflow.com/questions/7442450/custom-alertdialog-borders – Andy Harris Sep 14 '12 at 05:05
5

You can try:

myDialog.show();
Window window = myDialog.getWindow();
window.setLayout(300, 300);
Steven.Nguyen
  • 1,034
  • 13
  • 14
  • no, dont ever do this, or anything that hardcodes width and height – Jin Dec 28 '15 at 18:46
  • this works fine. we can set a fixed values, WindowManager.LayoutParams.WRAP_CONTENT or percentage of screen width etc. we need to set this after call show() method. If we need to set background color we need call it before calling show() method. android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams(); lp.copyFrom(deleteDialog.getWindow().getAttributes()); lp.dimAmount = 0.7f; deleteDialog.getWindow().setAttributes(lp); deleteDialog.show(); – Rukmal Dias Nov 29 '17 at 09:37
3
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Double width = metrics.widthPixels*.7;
Double height = metrics.heightPixels*.7;
Window win = dialog.getWindow();
win.setLayout(width.intValue(), height.intValue());
Ankit Adlakha
  • 1,436
  • 12
  • 15
0

Try something like this:

    dialog_item = new Dialog(Pre_Venda_Digitacao_2.this);
    dialog_item.setContentView(R.layout.layout_pre_venda_digitacao_add_produto);
    Util.softKeyboard(dialog_item.getWindow(), false);

    // Recuperar os componentes da janela e disponibilizar para as variáveis globais.
    dialog_Item_getComponentes();

    /***********************OnShow do Dialog*****************************/
    dialog_item.setOnShowListener(new OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            //Recupera a menor fonte dentro do layout passado
            float lower_font_size = AutoResizeTextView_Functions.Get_Lower_Font_Size(ll_add_produto, 1000);

            //Seta menor fonte recuparada anteriormente para os textos do layout passado
            AutoResizeTextView_Functions.Resize_Text(ll_add_produto, lower_font_size);
        }
    });

    dialog_item.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            dialog_item = null;
            preVenda_Item = null;
        }
    });

    Display display = getWindowManager().getDefaultDisplay(); 
    int width       = display.getWidth();

    lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog_item.getWindow().getAttributes());
    lp.width = (int) (width - (width * 0.07) );  //Tira 7% da largura total da tela para deixar um espaço na janela
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

    dialog_item.getWindow().setAttributes(lp);

    dialog_item.show();
William Da Silva
  • 703
  • 8
  • 19
0

Do this in your rate_layout.xml:

<!-- begin snippet: js hide: false -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#0000"
>

You also need to create and call a dialog Fragment if you didnt:

public class AddDialog extends DialogFragment
{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();

        builder.setView(inflater.inflate(R.layout.rate_layout, null));
        return builder.create();
    }
}

Then call the Dialog in a function:

public void SomeFunction()
{
    DialogFragment d = new AddDialog();
    d.show(getSupportFragmentManager(), "some_dialog_refrence");
}
julienc
  • 19,087
  • 17
  • 82
  • 82
Toye_Brainz
  • 100
  • 7
0

we can set width, height of custom dialog like this.

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int screenHeight = displayMetrics.heightPixels;
int screenWidth = displayMetrics.widthPixels;

android.view.WindowManager.LayoutParams lp = new android.view.WindowManager.LayoutParams();
        lp.copyFrom(deleteDialog.getWindow().getAttributes());

// If we need to set background color of activity
// We need to set this before calling show() method
lp.dimAmount = 0.7f;    
deleteDialog.getWindow().setAttributes(lp); 
deleteDialog.show();

// we need to set width, height after calling show() method
Window window = deleteDialog.getWindow();
window.setLayout(screenWidth - screenWidth/2), WindowManager.LayoutParams.WRAP_CONTENT);
Rukmal Dias
  • 3,242
  • 1
  • 34
  • 28