Make alert dialog background transparent

蹲街弑〆低调 提交于 2019-12-01 20:40:07
Shobhit Puri

If you haven't read Style attributes of custom styled AlertDialog question, you should go ahead and read. The answer suggests to use Dialog instead of Alert Dialog. Moreover reading Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified? issue with LayoutInflater might clear a bit more. Hope it helps. Try it and let me know if it works.

I found this way that is compatible with AlertDialog.Builder . Using the "Dump View Hierarchy" button in the Android "Devices" tab in Eclipse, I saw many nested FrameLayouts and it seemed like the background was in these, vs the window.

Traversing these Views and setting background to transparent on each actually worked (my custom view then floats over the dimmed app, with no frames or backgrounds, and there is no change in layout). I had a custom view, so traversed down from it, but traversing ViewGroups up from the Window should also work.

I am using an AlertDialog.Builder in my own DialogFragment subclass with a custom view.

   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    configureDialogBuilder(builder);

    final LayoutInflater inflater = getActivity().getLayoutInflater();
    customView = inflater.inflate(customViewId, null);
    builder.setView(customView);

    final AlertDialog dialog = builder.create();
    return dialog;
  }

  @Override
  public void onStart() {
    super.onStart();
    clearBackgrounds(customView);
  }

  private void clearBackgrounds(View view) {
    while (view != null) {
      view.setBackgroundResource(android.graphics.Color.TRANSPARENT);

      final ViewParent parent = view.getParent();
      if (parent instanceof View) {
        view = (View) parent;
      } else {
        view = null;
      }
    }
  }

I had to do something similar for one of my projects. What I did was to create an Activity just for the AlertDialog.

Not sure if this is what you are after but posting it here in case it helps you...

Activity

public class ShowAlertDialogActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_dialog);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        // create the builder

        AlertDialog alert = builder.create();
        alert.show();
    }
}

And I set the background to transparent in the layout (alert_dialog.xml)...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:background="@android:color/transparent">
</LinearLayout>

And added the activity to the Manifest...

<activity
    android:name=".ShowAlertDialogActivity"
    android:label="@string/app_title"
    android:theme="@style/AppTheme"
    android:launchMode="singleTask" />

This did the job for me.

Hope this helps.

simply call,

customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

just before

customDialog.show();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!