Android Dialog - Rounded Corners and Transparency

后端 未结 11 532
日久生厌
日久生厌 2020-12-09 16:12

I\'m trying to make a custom android dialog with rounded corners. My current attempts have given me this result.

\"

相关标签:
11条回答
  • 2020-12-09 16:44

    The only solution I have found is here. Use Dialog instead of AlertDialog and set transparent background:
    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    Therefore you can't use the builder. But you can use new Dialog() also in onCreateDialog callback of DialogFragment if you follow to best guidelines.

    This works also for Gingerbread.

    Besides the layered drawable can be simplified to one shape with xml element <stroke> for the border.

    0 讨论(0)
  • 2020-12-09 16:48

    Just try

    myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    
    0 讨论(0)
  • 2020-12-09 16:52
    • UPDATE

    I understood that activity's background makes sense. So use @robert's answer with these changes.

    in DialogFragment layout set width and height or add minimum sizes:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content" // Or match_parent, 300dp.
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:background="@drawable/white_round_corner_background"
        android:gravity="center"
        android:minWidth="300dp"
        android:minHeight="200dp"
        android:orientation="vertical"
        android:padding="15dp"
        >
    ...
    

    Remove <item name="android:background">@color/...</item> from styles of needed activities and set these backgrounds in activity's layouts.

    In DialogFragment write:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // This removes black background below corners.
        setStyle(DialogFragment.STYLE_NO_FRAME, 0);
    }
    
    • Old variant

    According to robert answer, you should apply setStyle(STYLE_NO_FRAME, 0), but there appear new problems. If you have a narrow DialogFragment like in Custom dialog too small, then you should follow this guide.

    Add to styles.xml these 3 lines for dialog size:

    <style name="ErrorDialogTheme" parent="@android:style/Theme.Dialog">
        <item name="android:minWidth" type="dimen">300dp</item>
        <!-- This option makes dialog fullscreen and adds black background, so I commented it -->
        <!-- <item name="android:minHeight" type="dimen">200dp</item> -->
        <!-- This option doesn't work, so I commented it -->
        <!-- <item name="android:layout_width">match_parent</item> -->
    </style>
    

    In layout of your DialogFragment add style:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        ...
        android:minWidth="300dp" // Optional, remove this line.
        android:minHeight="200dp" // Optional, remove this line.
        style="@style/ErrorDialogTheme"
        android:theme="@style/ErrorDialogTheme"
        >
    

    In code of your DialogFragment write:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // This removes black background. If not 0 as a parameter, black background will appear.
        setStyle(STYLE_NO_FRAME, 0)
    }
    
    // If you want a fullscreen dialog, use this, but it doesn't remove a black background.
    override fun onStart() {
        super.onStart()
        dialog.window?.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT)
    }
    

    Look at AndroidManifest.xml and find all activities that can show these dialogs, check android:theme="..." themes and go to styles.xml. Now take a look at <item name="android:background">@color/...</item> items of these themes. There should be a transparent color or these items might not exist. If you have these background items, whole activities will have those backgrounds and dialogs too! So, if you have a camera activity with DialogFragment above it, you will see this.

    Remove background items of needed styles. Also maybe background is set in code, check it.

    In Dialog with transparent background in Android and many pages it is written to add one of these:

    dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    

    probably in onViewCreated() or onCreateDialog(), but it didn't help me, because the background of the Activity was set in styles.

    Tested on Samsung Galaxy S4 running Android 5.0.1.

    0 讨论(0)
  • 2020-12-09 16:54

    Use 9-patch PNG with transparency in those corners.

    0 讨论(0)
  • 2020-12-09 16:56
     public void initDialog() {
        exitDialog = new Dialog(this);
        exitDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = View.inflate(this, R.layout.dialoglayout, null);
        exitDialog.setContentView(view);
        AdSize adSize = new AdSize(300, 250);
    
        dialogAdview = new AdView(this);
        dialogAdview.setAdUnitId(getResources().getString(R.string.banner_id));
        dialogAdview.setAdSize(adSize);
        RelativeLayout adLayout = (RelativeLayout) view.findViewById(R.id.adLayout);
        adLayout.addView(dialogAdview);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        dialogAdview.loadAd(adRequest);
        dialogAdview.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                Log.d("Tag", "adLoaded");
                super.onAdLoaded();
            }
    
    
        });
    
        view.findViewById(R.id.yes_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit = true;
                onBackPressed();
            }
        });
    
        view.findViewById(R.id.no_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exit = false;
                exitDialog.dismiss();
            }
        });
    
    }
    

    dialoglayout.xml

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/custom_dialog_round"
        android:orientation="vertical">
    
    
        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:text="Do you want to exit?"
            android:textColor="#000"
            android:textSize="18dp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/text"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/yes_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/background_draw"
                    android:padding="8dp"
                    android:text="Yes"
                    android:textAlignment="center"
                    android:textColor="#9fa8da"
                    android:textSize="20dp" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">
    
                <TextView
                    android:id="@+id/no_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/background_draw"
                    android:padding="8dp"
                    android:text="No"
                    android:textAlignment="center"
                    android:textColor="#d50000"
                    android:textSize="20dp" />
            </LinearLayout>
    
    
        </LinearLayout>
    
    </LinearLayout>
    `
    

    custom_dialog_round.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid
            android:color="#fff"/>
        <corners
            android:radius="10dp" />
        <padding
            android:left="10dp"
            android:top="10dp"
            android:right="10dp"
            android:bottom="10dp" />
    </shape>
    

    reference http://techamongus.blogspot.com/2018/02/android-create-round-corner-dialog.html

    0 讨论(0)
提交回复
热议问题