Search code examples
androidandroid-layoutandroid-cardviewcustomdialog

Custom Dialog Corners are not round - CardViewAndroid


I am working on an Android App ,In the App I have created a Custom Dialog box , I have created the design of Dialog box using CardView but I am not able to make round corners of the dialog .

I have visited many posts on Stackoverflow for the help , but none of them was helpfull .

Here is my code :

SKCDialog.java

public class SKCDialog  extends Dialog implements android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
    public Button yes, no;
    RelativeLayout closeAcessCodeDialog ;
    private EditText accessCodeText;
    private Button verifyAccessCode;
    private ImageButton closeDialog;

    public SKCDialog(Activity a) {
        super(a);
        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.access_code_dialog);


        closeAcessCodeDialog = (RelativeLayout)findViewById(R.id.closeAcessCodeDialog) ;
        accessCodeText = (EditText)findViewById(R.id.accessCodeText) ;
        verifyAccessCode = (Button)findViewById(R.id.verifyAccessCode) ;
        closeDialog = (ImageButton)findViewById(R.id.closeDialog) ;


        closeAcessCodeDialog.setOnClickListener(this);
        verifyAccessCode.setOnClickListener(this);
        closeDialog.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.closeAcessCodeDialog:
                dismiss();
                break;

            case R.id.verifyAccessCode:

                dismiss();

                break;
            case R.id.closeDialog:

                dismiss();

                break;
            default:
                break;
        }
       // dismiss();
    }

    public void closeDialog(View v){

    }
}

access_code_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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"
    app:cardCornerRadius="30dp"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ececec"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Access Code"
            android:hint="Enter Your Access Code"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:textSize="18sp"
            android:textStyle="bold"
            />

        <RelativeLayout
            android:id="@+id/closeAcessCodeDialog"
            android:layout_height="40dp"
            android:layout_width="40dp"
            android:layout_alignParentRight="true"
            android:gravity="center"
            >
            <ImageButton
                android:id="@+id/closeDialog"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:background="@drawable/cross"
                />

        </RelativeLayout>

        <EditText
            android:id="@+id/accessCodeText"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_below="@+id/closeAcessCodeDialog"
            android:layout_marginTop="20dp"
            android:background="@drawable/round_corner_shape"
            />


        <Button
            android:id="@+id/verifyAccessCode"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:layout_below="@+id/accessCodeText"
            android:paddingLeft="25dp"
            android:paddingRight="25dp"
            android:layout_marginTop="20dp"
            android:text="Submit"
            android:background="#007AFF"
            android:textColor="#fff"
           android:layout_centerHorizontal="true"
            android:layout_marginBottom="10dp"
            />

    </RelativeLayout>

</android.support.v7.widget.CardView>

How I am calling SKCDialog :

SKCDialog cdd=new SKCDialog(screen);
cdd.setCancelable(false);
cdd.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
cdd.show();

Please suggest me what I am doing wrong .


Solution

  • Try this:

    remove

    android:background="#ececec"
    

    from relative layout and add

    app:cardBackgroundColor="#ececec" 
    

    in cardview