Can't get transparent DialogFragment
Asked Answered
T

4

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 !

Trifurcate answered 11/12, 2013 at 19:47 Comment(6)
Are You sure that there's no white background in R.layout.share_or_die ?Statute
yes ! I just have a linearLayout inside, but I change linearlayout properties, the white is still unchanged. Tx for help!Trifurcate
Have You tried to put single TextView instead of LL into the dialog?Statute
Also, is where any place You're using that inflated view? Looks like the code in the question is incomplete.Statute
I'm never using my inflated view. When I put a single textview, it still appear white :(Trifurcate
oneliner: #8046056Butta
S
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

Statute answered 17/12, 2013 at 5:58 Comment(7)
I get a : AndroidRuntimeException: requestFeature() must be called before adding content. and I never call explicitly requestFeature()...Trifurcate
Still the same exception:( It might be usefull mention I use compatibility FragmentDialog (v4)Trifurcate
And then do You ttri to show the dialog (e.g. I've tried to show it from onResume())?Statute
I'm showing the dialog when user click Back button. I've updated my code to show how I show it.Trifurcate
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.Statute
It works ! The only thing I changed is using a Dialog instead of alertDialog. Tx for help !!!Trifurcate
This doesn't work, even If I did: requestWindowFeature(Window.FEATURE_NO_TITLE); on activity's onCreate, still throwing requestFeature() must be calledSpherulite
Z
13

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);
}
Zsazsa answered 19/11, 2015 at 9:52 Comment(1)
thank you very much for this answer, it really helped) this is a must - have step to fully remove backgroundEdgell
N
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.

Nolin answered 22/6, 2016 at 18:4 Comment(0)
R
1

It works for me:

getDialog().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
Roundabout answered 7/9, 2021 at 9:51 Comment(1)
this is perfect solution i was looking for, Thank youTallu

© 2022 - 2024 — McMap. All rights reserved.