How to make custom dialog with rounded corners in android

前端 未结 16 731
半阙折子戏
半阙折子戏 2020-11-28 02:15

What I am trying to do: I am trying to make a custom dialog in android With rounded corners.

What is happening: I am able to make c

相关标签:
16条回答
  • 2020-11-28 02:34

    Here is the complete solution if you want to control the corner radius of the dialog and preserve elevation shadow

    Dialog:

    class OptionsDialog: DialogFragment() {
    
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup, savedInstanceState: Bundle?): View {
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        return inflater.inflate(R.layout.dialog_options, container)
    }
    
    }
    

    dialog_options.xml layout:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="40dp"
        app:cardElevation="20dp"
        app:cardCornerRadius="12dp">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            id="@+id/actual_content_goes_here"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </androidx.cardview.widget.CardView>
    </FrameLayout>
    

    The key is to wrap the CardView with another ViewGroup (here FrameLayout) and set margins to create space for the elevation shadow.

    0 讨论(0)
  • 2020-11-28 02:36

    I implemented rounded dialog using CardView in the custom layout and setting its corner radius.

    Here's my xml code.

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bottomSheet"
    android:layout_width="match_parent"
    android:layout_margin="@dimen/padding_5dp"
    app:cardCornerRadius="@dimen/dimen_20dp"
    android:layout_height="wrap_content">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/main_gradient_bg"
        android:paddingBottom="32dp">
    
        <TextView
            android:id="@+id/subdomain_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_32dp"
            android:layout_marginLeft="@dimen/margin_32dp"
            android:layout_marginTop="@dimen/margin_50dp"
            android:fontFamily="@font/nunito_sans"
            android:text="@string/enter_subdomain"
            android:textColor="@color/white"
            android:textSize="@dimen/size_18sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_45dp"
            app:layout_constraintLeft_toRightOf="@id/subdomain_label"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/ic_baseline_info_24" />
    
        <EditText
            android:id="@+id/subdomain_edit_text_bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/dimen_20dp"
            android:layout_marginLeft="@dimen/dimen_20dp"
            android:layout_marginEnd="@dimen/dimen_20dp"
            android:layout_marginRight="@dimen/dimen_20dp"
            android:textColor="@color/white"
            android:theme="@style/EditTextTheme"
            app:layout_constraintTop_toBottomOf="@id/subdomain_label" />
    
        <Button
            android:id="@+id/proceed_btn"
            android:layout_width="@dimen/dimen_150dp"
            android:layout_height="@dimen/margin_50dp"
            android:layout_marginTop="@dimen/margin_30dp"
            android:background="@drawable/primary_btn_bg"
            android:text="@string/proceed"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="@dimen/size_18sp"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/subdomain_edit_text_bottom_sheet" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
    

    After that, i called it in Kotlin as follows :-

        val builder = AlertDialog.Builder(mContext)
        val viewGroup: ViewGroup = findViewById(android.R.id.content)
        val dialogView: View = 
        LayoutInflater.from(mContext).inflate(R.layout.subdomain_bottom_sheet, 
        viewGroup, false)
        val alertDialog: AlertDialog = builder.create()
        alertDialog.setView(dialogView,0,0,0,0)
        alertDialog.show()
        val windowParam = WindowManager.LayoutParams()
        windowParam.copyFrom(alertDialog.window!!.attributes)
        windowParam.width = AppConstant.getDisplayMetricsWidth(mContext) - 100
        windowParam.height = WindowManager.LayoutParams.WRAP_CONTENT
        windowParam.gravity = Gravity.CENTER
        alertDialog.window!!.attributes = windowParam
        
       
        alertDialog.window!!.setBackgroundDrawable
        (ColorDrawable(Color.TRANSPARENT))
    

    Where last line is very important. Missing that would cause a color(mostly white) to show behind the corners.

    0 讨论(0)
  • 2020-11-28 02:39

    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    this works for me

    0 讨论(0)
  • 2020-11-28 02:42

    You need to do the following:

    • Create a background with rounded corners for the Dialog's background:

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle" >
      
          <solid android:color="#fff" />
      
          <corners
              android:bottomLeftRadius="8dp"
              android:bottomRightRadius="8dp"
              android:topLeftRadius="8dp"
              android:topRightRadius="8dp" />
      
      </shape>
      
    • Now in your Dialog's XML file in the root layout use that background with required margin:

      android:layout_marginLeft="20dip"
      android:layout_marginRight="20dip"
      android:background="@drawable/dialog_background"
      
    • finally in the java part you need to do this:

      dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
      dialog.setContentView(layoutResId);
      View v = getWindow().getDecorView();
      v.setBackgroundResource(android.R.color.transparent);
      

    This works perfectly for me.

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