8

I have a dialog Fragment which look like that.

enter image description here

AlertDialog ad = builder.create();
Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
ad.getWindow().setBackgroundDrawable(d);

This code get background semi transparent. But I still got a white part on the bottom. I want to get rid of the white to just have semi transparent background

I already tried a lot of stuff that I saw in other posts.

I don't know what is the object that I must change between the DialogFragment, the AlertDialog and the LinearLayout.

It may not be the LinearLayout, because when I increase margin, nothing is moving.

Here is my code :

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//      setStyle(DialogFragment.STYLE_NORMAL, 0);
//      setStyle(STYLE_NO_FRAME, R.style.CustomDialog);
//      setStyle(STYLE_NO_FRAME, 0);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    View view = getActivity().getLayoutInflater().inflate(
            R.layout.share_or_die, null);

    AlertDialog ad = builder.create();
    Drawable d = new ColorDrawable(Color.BLACK);
    d.setAlpha(130);
    ad.getWindow().setBackgroundDrawable(d);
    ad.setCanceledOnTouchOutside(true);
    ad.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    ad.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      

    return ad;

}

I just call it in the mainActivity when user click back button:

@Override
public void onBackPressed() {
        if (isUserConnected && !hasShared) {
        shareOnExitDialog = new ShareOnExitDialog();
            shareOnExitDialog.setCancelable(true);
            shareOnExitDialog.show(getSupportFragmentManager(), "Exit");
        } else {
            finish();
        }


}

Any help would be appreciated !

Juliatzin
  • 18,455
  • 40
  • 166
  • 325

4 Answers4

25

The issue is in default dialog theme. Based on this and this answers it's much easier to achieve your target.

The Activity should be like the following:

public class MyActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
    }

    @Override
    public void onBackPressed() {
        MyDialogFragment.newInstance("title title").show(getSupportFragmentManager(), "dialog");
    }
}

And fragment:

public class MyDialogFragment extends android.support.v4.app.DialogFragment {

    /**
     * Create a new instance of MyDialogFragment, providing "title"
     * as an argument.
     */
    static MyDialogFragment newInstance(String title) {
        MyDialogFragment frag = new MyDialogFragment();
        Bundle args = new Bundle();

        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
        final View view = getActivity().getLayoutInflater().inflate(R.layout.share_or_die, null);

        final Drawable d = new ColorDrawable(Color.BLACK);
        d.setAlpha(130);

        dialog.getWindow().setBackgroundDrawable(d);
        dialog.getWindow().setContentView(view);

        final WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.gravity = Gravity.CENTER;

        dialog.setCanceledOnTouchOutside(true);

        return dialog;
    }
}

Also, be sure to do the following: before setContentView() in the activity, getWindow().requestFeature(Window.FEATURE_NO_TITLE) should be added in order to use *NoTitleBar style.

The result is (I've used LinearLayout with single TextView inside): enter image description here

Community
  • 1
  • 1
sandrstar
  • 12,503
  • 8
  • 58
  • 65
  • I get a : AndroidRuntimeException: requestFeature() must be called before adding content. and I never call explicitly requestFeature()... – Juliatzin Dec 17 '13 at 15:39
  • Still the same exception:( It might be usefull mention I use compatibility FragmentDialog (v4) – Juliatzin Dec 18 '13 at 02:06
  • And then do You ttri to show the dialog (e.g. I've tried to show it from onResume())? – sandrstar Dec 18 '13 at 02:22
  • I'm showing the dialog when user click Back button. I've updated my code to show how I show it. – Juliatzin Dec 18 '13 at 02:32
  • That's strange. I've just tried mine code with v4 support library and it worked fine. Please, refer to my updated answer. Also, if You use 'Fullscreen' style, that feature should be also requested in the activity. I've tried it also with API 10 version and observed no issues. – sandrstar Dec 18 '13 at 03:05
  • It works ! The only thing I changed is using a Dialog instead of alertDialog. Tx for help !!! – Juliatzin Dec 18 '13 at 03:20
  • 1
    This doesn't work, even If I did: requestWindowFeature(Window.FEATURE_NO_TITLE); on activity's onCreate, still throwing requestFeature() must be called – Jacek Kwiecień Jul 11 '14 at 07:13
12

I hope it can help you

@Override
public void onStart() {
    super.onStart();

    Window window = getDialog().getWindow();
    WindowManager.LayoutParams windowParams = window.getAttributes();
    windowParams.dimAmount = 0.0f; 

    window.setAttributes(windowParams);
}
dinus
  • 141
  • 1
  • 2
  • thank you very much for this answer, it really helped) this is a must - have step to fully remove background – Andrii Bas Apr 18 '16 at 07:04
2

None of the above solutions worked for me. What i found to work was to make sure that I had imported android.support.v7.app.AlertDialog instead of the generic app.alertdialog.

Hope this helps others too.

user6388032
  • 125
  • 8
1

It works for me:

getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);